Telegram User Account Gateway
A production-grade REST API + Webhook gateway for managing Telegram user accounts via TDLib (not Bot API). Supports multi-tenant, multi-account management with independent proxies, message queue, rate limiting, and audit logging.
Architecture
- Runtime: Node.js 20+ with TypeScript
- Framework: NestJS 11.x
- Database: PostgreSQL 16 (TypeORM)
- Cache/Queue: Redis 7 + BullMQ
- TDLib: tdjson via worker_threads
- Auth: JWT (Bearer tokens)
- API Docs: Swagger at
/api/docs
Quick Start
# Copy environment config
cp .env.example .env
# Start infrastructure
docker-compose -f docker/docker-compose.yml up -d postgres redis
# Install dependencies
npm install
# Run migrations
npm run migration:run
# Start development server
npm run start:dev
Docker Compose
# Start all services
docker compose --env-file .env -f docker/docker-compose.yml up -d --build
See DEPLOYMENT.md for production setup, readiness checks, persistent data, and Windows WSL requirements.
When used by 灵犀跨境, also read the repository-level integration guide.
API Endpoints
Admin
GET /api/admin/health- Health check (public)GET /api/admin/metrics- Application metricsGET /api/admin/status- TDLib client status
Auth
POST /api/auth/register- Register user (public)POST /api/auth/login- Login (public)
Tenants
POST /api/tenants- Create tenantGET /api/tenants- List tenantsGET /api/tenants/:id- Get tenantPUT /api/tenants/:id- Update tenantDELETE /api/tenants/:id- Delete tenant
TG Accounts
POST /api/tg-accounts- Create Telegram accountGET /api/tg-accounts- List accountsGET /api/tg-accounts/:id- Get accountDELETE /api/tg-accounts/:id- Remove account
TG Account Authorization
POST /api/tg-accounts/:id/auth/phone- Set phone numberPOST /api/tg-accounts/:id/auth/code- Submit verification codePOST /api/tg-accounts/:id/auth/password- Submit 2FA passwordPOST /api/tg-accounts/:id/auth/logout- Logout
Chats & Messages
GET /api/tg-accounts/:id/chats- List chatsGET /api/tg-accounts/:id/chats/:chatId/history- Chat historyPOST /api/tg-accounts/:id/messages/send- Send message (queued)POST /api/tg-accounts/:id/messages/send-direct- Send message (direct)POST /api/tg-accounts/:id/messages/pin- Pin a messagePOST /api/tg-accounts/:id/messages/unpin- Unpin a messagePOST /api/tg-accounts/:id/messages/action- Send typing/chat actionGET /api/tg-accounts/:id/messages/:chatId/:messageId/link- Get message link
Contacts, Sessions & Groups
GET /api/tg-accounts/:id/contacts/search?q=...- Search by phone, username, or general queryGET /api/tg-accounts/:id/dialogs/sessions- List active Telegram sessionsPOST /api/tg-accounts/:id/dialogs/sessions/:sessionId/terminate- Terminate sessionPOST /api/tg-accounts/:id/dialogs/users/:userId/block- Block userPOST /api/tg-accounts/:id/dialogs/users/:userId/unblock- Unblock userPOST /api/tg-accounts/:id/groups/join-by-invite-link- Join via invite linkPUT /api/tg-accounts/:id/groups/:chatId/title- Change group titlePUT /api/tg-accounts/:id/groups/:chatId/photo- Change/remove group photo
Webhooks
POST /api/tg-accounts/:id/webhooks- Create webhookGET /api/tg-accounts/:id/webhooks- List webhooks
Operations
GET /api/admin/audit-logs- Query tenant-scoped audit logsGET /api/admin/audit-logs/accounts/:accountId- Query account audit logsGET /api/admin/rate-limits/:accountId- Get account rate-limit statistics
Environment Variables
See .env.example for all available configuration options.
License
MIT