Better Auth Telegram Plugin

1.5.0 · active · verified Wed Apr 22

better-auth-telegram is a plugin for the Better Auth framework, providing comprehensive Telegram authentication capabilities. It supports various Telegram login methods including the traditional Login Widget, Mini Apps, and the more modern OpenID Connect (OIDC) flow, leveraging OAuth 2.0 Authorization Code flow with PKCE. The library handles HMAC-SHA-256 verification and is built on the Web Crypto API, ensuring compatibility across diverse JavaScript runtimes like Node.js (>=22.0.0), Bun, and Cloudflare Workers, without relying on Node.js-specific `node:crypto` modules. The current stable version is 1.5.0, with an active release cadence addressing features, fixes, and compatibility. It integrates seamlessly with Better Auth's client and server-side components, and offers explicit guidance for database schema updates, making it a robust solution for integrating Telegram login into applications.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the server-side Telegram authentication plugin for Better Auth using a bot token and username.

import { betterAuth } from "better-auth";
import { telegram } from "better-auth-telegram";

// Ensure TELEGRAM_BOT_TOKEN is set in your environment variables.
// For local development, consider using ngrok for HTTPS as Telegram requires it.
export const auth = betterAuth({
  plugins: [
    telegram({
      botToken: process.env.TELEGRAM_BOT_TOKEN ?? '', // Use environment variable
      botUsername: "your_bot_username", // Replace with your bot's username (without @)
      // Optional: Disable Login Widget if only using OIDC and to avoid schema clutter
      // loginWidget: false,
      // Optional: Enable OIDC and provide client secret (from BotFather > Web Login)
      // oidc: { enabled: true, clientSecret: process.env.TELEGRAM_OIDC_CLIENT_SECRET ?? '' },
    }),
  ],
});

console.log("Telegram plugin initialized for Better Auth. Make sure your environment variables are configured.");
// In a real application, you would now expose 'auth' via an API handler or similar.

view raw JSON →