TDLib Node.js Bindings (tdl)

8.1.0 · active · verified Wed Apr 22

tdl is a JavaScript wrapper for TDLib (Telegram Database Library), providing Node.js bindings to create custom Telegram clients or bots. Currently at version 8.1.0, it is actively maintained with releases often following new TDLib versions. A key differentiator is its direct, low-level integration with the native TDLib, allowing full control over Telegram API interactions, unlike higher-level bot libraries. It requires a separate installation or build of the `tdjson` shared library from TDLib itself. tdl ships with TypeScript type definitions, enabling robust development in modern JavaScript environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing `tdl` with `prebuilt-tdlib`, creating a client, handling errors, and observing authorization state changes during login.

import { createClient, configure } from 'tdl';
import { getTdjson } from 'prebuilt-tdlib';

// IMPORTANT: Configure tdl to use the pre-built tdjson library.
// This must be done BEFORE creating any client instances.
configure({ tdjson: getTdjson() });

// Replace with your actual API ID and Hash from https://my.telegram.org/
const apiId = Number(process.env.TELEGRAM_API_ID ?? 12345);
const apiHash = process.env.TELEGRAM_API_HASH ?? '0123456789abcdef0123456789abcdef';

const client = createClient({
  apiId: apiId,
  apiHash: apiHash
});

client.on('error', (error) => {
  console.error('tdl client error:', error);
});

client.on('update', (update) => {
  // console.log('Received update:', update);
  if (update._ === 'updateAuthorizationState') {
    if (update.authorization_state._ === 'authorizationStateWaitPhoneNumber') {
      console.log('Please enter your phone number (e.g., +12345678900):');
      // In a real app, you would prompt the user for input here
      // For demonstration, we'll exit after waiting.
      setTimeout(() => client.destroy(), 5000);
    } else if (update.authorization_state._ === 'authorizationStateReady') {
      console.log('Client is ready!');
      client.destroy(); // Destroy client for quickstart, in real app keep running
    }
  }
});

console.log('Connecting to Telegram...');
client.login(() => console.log('Login initiated.'));

view raw JSON →