TDLib Node.js Bindings (tdl)
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
-
Error: Cannot find tdjson library
cause The `tdjson` shared library (`libtdjson.so`, `libtdjson.dylib`, or `tdjson.dll`) was not found in expected system paths or via configuration.fixEnsure `prebuilt-tdlib` is installed and `tdl.configure({ tdjson: getTdjson() })` is called before `createClient()`, or provide a direct path via `tdl.configure({ tdjson: '/path/to/libtdjson.so' })`. -
node-gyp rebuild error
cause The `tdl` Node.js addon failed to compile from source due to missing C++ compilers, Python, or other build dependencies.fixInstall a C++ compiler (e.g., `build-essential` on Linux, Xcode Command Line Tools on macOS, Visual Studio Build Tools on Windows) and Python. -
updateAuthorizationState: authorizationStateWaitTdlibParameters
cause The client was initialized with incorrect `apiId` or `apiHash` values, preventing proper authorization with Telegram.fixVerify your `apiId` and `apiHash` are correct and obtained from `https://my.telegram.org/`. -
TypeError: Cannot read properties of undefined (reading 'createClient') or TypeError: tdl.createClient is not a function
cause Incorrect import statement or attempting to use `tdl.createClient` in a CommonJS context where `tdl` might not be the expected object.fixFor ESM, use `import { createClient } from 'tdl'`. For CommonJS, use `const { createClient } = require('tdl')` if not using `tdl.configure()` first, or `const tdl = require('tdl')` and then `tdl.createClient()` after `tdl.configure()`.
Warnings
- breaking tdl requires the `tdjson` shared library of TDLib (Telegram Database Library) to be present on the system. This is a separate dependency not bundled with the `tdl` npm package.
- gotcha The `tdjson` shared library must be TDLib version 1.8.0 or newer. Using an older version can lead to crashes or unexpected behavior due to API mismatches.
- gotcha If pre-built Node.js addons for `tdl` are not available for your specific platform/architecture, `npm install` will attempt to build the addon from source using `node-gyp`. This requires a C++ compiler (C++14 capable), Python, and relevant build tools (e.g., MSVS on Windows) to be installed.
- gotcha Valid Telegram API ID and API Hash are mandatory for `tdl.createClient()`. Using incorrect or placeholder values will result in authentication failures.
- breaking tdl requires Node.js v16 or newer. Older Node.js versions are not supported and may lead to installation failures or runtime errors.
Install
-
npm install tdl -
yarn add tdl -
pnpm add tdl
Imports
- createClient
const tdl = require('tdl'); tdl.createClient(...)import { createClient } from 'tdl' - configure
const tdl = require('tdl'); tdl.configure(...)import { configure } from 'tdl' - getTdjson
const { getTdjson } = require('tdl')import { getTdjson } from 'prebuilt-tdlib' - Client (type)
import type { Client } from 'tdl'
Quickstart
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.'));