TGAuth instance with your project ID, call signIn() or mount(), and the SDK handles the complete authentication flow — from opening Telegram to delivering a verified user object — for you.
Loading the SDK
Choose the installation method that fits your project.- npm
- CDN
Install the package from npm:Then import it in your module:
Constructor
Instantiate aTGAuth client by passing a configuration object. Only projectId is required.
Your Bondify project ID. Find this in the dashboard under Projects.
Override the API base URL. Defaults to
https://tgauth-backend.onrender.com. You only need this if you are self-hosting Bondify on the Business plan.Milliseconds between each status poll while waiting for the user to confirm in Telegram. Defaults to
2000 (2 seconds).Milliseconds before the client-side polling loop gives up and throws a
TIMEOUT error. Defaults to 600000 (10 minutes).Methods
client.signIn() → Promise<User>
Runs the complete authentication flow: creates a session on the server, opens Telegram via a deeplink, then polls for confirmation. Returns a Promise that resolves to the authenticated user object.
client.mount(target, options) → unmount()
Injects a fully styled, interactive Telegram auth button into a DOM element. Handles loading states, status messages, and error display automatically. Returns an unmount function you can call to remove the button.
A CSS selector string (e.g.
'#login-container') or a DOM Element to inject the button into.Button colour scheme. Pass
"light" for a white button with a blue border, or "dark" for a dark navy background. Defaults to the standard Telegram blue.Custom button label. Defaults to
"Sign in with Telegram".Called with the resolved
User object after successful authentication. Receives the same user shape as signIn().Called with a
TGAuthError if the flow fails or is cancelled. Check err.code for the reason.Static helpers
For simple use cases, you can skip instantiation entirely and use the static one-liner methods.TGAuth instance internally using the options you provide and then call the appropriate instance method.
Two additional static properties are available on the TGAuth constructor:
The
TGAuthError class. Use this to instanceof-check caught errors: if (err instanceof TGAuth.Error).The SDK version string, e.g.
'1.0.0'. Useful for debugging and support.Error codes
Every error thrown by the SDK is aTGAuthError instance with a code property. Use it to branch your error-handling logic.
| Code | When it occurs |
|---|---|
CANCELLED | The user tapped ❌ Cancel in the Telegram bot message |
EXPIRED | The session was not confirmed within the sessionTimeout window (default: 10 min) |
TIMEOUT | The client-side polling loop reached the sessionTimeout deadline without a terminal status |
Widget embed (no-build)
For pages where you cannot run JavaScript build steps — landing pages, CMS templates, plain HTML files — the TGAuth Widget script auto-mounts authentication buttons from HTML data attributes. Drop a single<script> tag on the page and annotate your container element; no JavaScript is required to wire it up.
- Automatic (data attributes)
- Custom events
- Auto-submit
Widget data attributes
Your Bondify project ID. Required — the widget logs a warning and skips mounting if this attribute is absent.
Override the API base URL (mirrors the
apiBase constructor option). Omit for the default.Button colour scheme:
"light" or "dark". Omit for the default Telegram blue.Custom button label. Defaults to
"Sign in with Telegram".Name of a global function to call on success. Receives the user object as its only argument. If omitted, the widget dispatches a
tgauth:success Custom Event instead.Name of a global function to call on error. Receives the
TGAuthError as its only argument. If omitted, the widget dispatches a tgauth:error Custom Event instead.URL to POST the user object to on success. The widget sends
Content-Type: application/json.URL to navigate to after a successful
data-submit-to POST. Only used in combination with data-submit-to.Widget Custom Events
Both events bubble up the DOM (bubbles: true) and carry the payload on event.detail.
| Event | event.detail | When fired |
|---|---|---|
tgauth:success | User object (telegramId, name, username, proof, sessionToken) | Authentication succeeded |
tgauth:error | TGAuthError instance | Authentication failed or was cancelled |
The widget uses a
MutationObserver to watch for buttons added dynamically to the DOM after page load, so you can insert widget markup at any time. You can also call window.TGAuthWidget.mountAll() to trigger mounting manually.