BotNetwork

Log in with Telegram on your website: the OpenID Connect flow

Telegram now documents its website login as a standard OpenID Connect provider. That means you can reuse ordinary OIDC libraries, but you still need a real HTTPS origin, a bot that represents your site and strict server-side token checks.

Published 7 min read

Why add Telegram login to a bot's website

Many bots end up with a companion website: a dashboard for settings, a page to manage a subscription, or an admin panel for a community bot. Asking people to create yet another password for that site is friction, and you already know who they are inside Telegram. Log In With Telegram lets them sign in with the account they use to talk to your bot, and, if they agree, lets your bot message them afterwards.

Telegram's current documentation describes two ways to integrate:

  • The Telegram Login library, a small JavaScript library that shows a login button and returns an ID token to a callback.
  • A standard OpenID Connect (OIDC) provider using the Authorization Code Flow with PKCE, so any OIDC-compatible library or identity broker can use it.

The older iframe-based widget, which used the /setdomain command and a hash check, is still documented but marked as legacy. This article covers the current flow.

Step 1: pick the bot that represents your site

Every login is tied to a bot. Users see a confirmation box with the bot's name and picture, and Telegram recommends that the bot's profile picture match your website's logo and that its name make the connection obvious. If your site is the dashboard for an existing bot, use that bot. If you are testing, create a separate bot so you do not touch production settings; our post on securing bot tokens explains why one bot per environment is a good habit.

Step 2: get a domain and HTTPS

The login flow only works with URLs you register in advance, and production login and Mini App links are expected to use HTTPS (Telegram allows plain HTTP only in its separate test environment). In practice you need a domain you control and a valid certificate.

If you do not have a domain yet, you can register one through Namecheap domains or any other registrar. A subdomain such as app.example.com keeps the login site separate from the webhook endpoint at hooks.example.com. Our step-by-step guide to a custom domain and HTTPS for webhooks covers DNS records and automatic certificates, and the same setup works for the login site.

The website itself does not have to run on the bot's server. A static front end plus a small backend endpoint for the token exchange is enough, and a basic plan such as Namecheap hosting can serve the pages as long as it can also run your server-side code or forward the callback to where that code runs.

Step 3: register Allowed URLs in BotFather

Open the @BotFather Mini App, select the bot and open Login Widget. There you add every Allowed URL:

  • The website origins where the login button is embedded, for example https://app.example.com.
  • The exact redirect URIs used by the OIDC flow, for example https://app.example.com/auth/callback.

The same screen shows the Client ID and Client Secret. Treat the secret like a bot token: keep it in an environment variable or a secret manager, never in front-end code or a repository. Telegram states that it only processes logins and redirects for pre-registered URLs, so a typo here is the most common reason a first test fails.

Step 4: choose the scopes

Request only what the site needs. The documented scopes are:

Scope What it gives you
openid Required. The user's unique identifier (sub) and token timestamps.
profile Basic info: Telegram user ID (id), name, username and photo URL.
phone The verified phone number, only with the user's consent.
telegram:bot_access Permission for your bot to send the user direct messages after login.

For a dashboard, openid profile is usually enough. Ask for phone only if you have a real reason, and say so in your privacy notice. Add telegram:bot_access if the site's purpose includes notifications from the bot; without it, the user still has to start the bot themselves before it can message them.

Step 5: run the authorization code flow

With the JavaScript library, you initialize it with your Client ID and requested scopes, and the callback receives an id_token plus a decoded user object. The decoded object is for display only; the documentation is explicit that the token must be verified on the server. You can also pass a server-generated nonce, which is included in the resulting token and protects against replays.

With a standard OIDC client, use these values from Telegram's discovery document at https://oauth.telegram.org/.well-known/openid-configuration:

  • Authorization endpoint: https://oauth.telegram.org/auth
  • Token endpoint: https://oauth.telegram.org/token
  • Keys (JWKS): https://oauth.telegram.org/.well-known/jwks.json
  • Response type code, with PKCE using S256

The browser is sent to the authorization endpoint with your client_id, redirect_uri, scope, a random state and the PKCE code_challenge. After approval, Telegram redirects to your callback with a code. Your backend checks that state matches what it stored for this session, then exchanges the code at the token endpoint with HTTP Basic authentication (Client ID and Client Secret) and the PKCE code_verifier. The response contains an access token and the id_token. If you are new to state handling, see OAuth state and CSRF.

Telegram does not currently provide a separate UserInfo endpoint, so all user data comes from the ID token. Some OIDC libraries call UserInfo by default; turn that off in their configuration.

Step 6: validate the ID token on the server

The ID token is a signed JWT. Before you trust anything in it, verify the signature against Telegram's published keys and check the claims: iss must be https://oauth.telegram.org, aud must match your Client ID (the bot ID), and the token must not be expired. A minimal Python version with PyJWT:

import jwt
from jwt import PyJWKClient

JWKS = PyJWKClient("https://oauth.telegram.org/.well-known/jwks.json")
CLIENT_ID = "123456789"  # the Client ID shown in BotFather

def verify_id_token(id_token, expected_nonce=None):
    signing_key = JWKS.get_signing_key_from_jwt(id_token)
    claims = jwt.decode(
        id_token,
        signing_key.key,
        algorithms=["RS256"],
        audience=CLIENT_ID,
        issuer="https://oauth.telegram.org",
    )  # also rejects expired tokens
    if expected_nonce is not None and claims.get("nonce") != expected_nonce:
        raise ValueError("nonce mismatch")
    return claims

Pin the algorithm list to what you configured. Telegram signs with RS256 by default; BotFather lets you switch to ES256, EdDSA or ES256K under Login Widget > Advanced, and the two last options are restricted to the openid scope. If you change the algorithm, change the algorithms list too.

After validation, create your own session (an HTTP-only, secure cookie) and store the user by a stable identifier. Do not keep the raw ID token around longer than needed.

Common problems

  • The popup opens but nothing comes back. The library talks to a popup window. If your site sends Cross-Origin-Opener-Policy: same-origin, that communication is blocked. Telegram's documentation recommends removing the header or using same-origin-allow-popups. Review this together with your Content Security Policy.
  • Redirect rejected. The redirect_uri must match a registered Allowed URL exactly, including scheme and path.
  • Signature fails after a settings change. The signing algorithm in BotFather and the one your code accepts no longer match.
  • No phone number in the token. The phone scope needs the user's consent; handle the claim as optional.

Checklist

  1. Choose or create the bot that represents the site, with a recognizable name and picture.
  2. Serve the site over HTTPS on a domain you control (Namecheap is one option for the domain).
  3. Register every origin and redirect URI under Login Widget in BotFather; store the Client Secret as a secret.
  4. Request the smallest set of scopes.
  5. Use state, PKCE and, with the library, a nonce.
  6. Verify signature, iss, aud and exp on the server before creating a session.
  7. Check your Cross-Origin-Opener-Policy header if the popup never returns.

Sources