Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

⚡ layer

layer — Async Rust MTProto

A modular, production-grade async Rust implementation of the Telegram MTProto protocol

Crates.io docs.rs TL Layer License Rust

layer is a hand-written, bottom-up implementation of Telegram MTProto in pure Rust. Every component — from the .tl schema parser, to AES-IGE encryption, to the Diffie-Hellman key exchange, to the typed async update stream — is owned and understood by this project.

No black boxes. No magic. Just Rust, all the way down.


Why layer?

Most Telegram libraries are thin wrappers around generated code or ports from Python/JavaScript. layer is different — it was built from scratch to understand MTProto at the lowest level, then wrapped in an ergonomic high-level API.

🦀
Pure Rust
No FFI, no unsafe blocks. Fully async with Tokio. Works on Android (Termux), Linux, macOS, Windows.
Full MTProto 2.0
Complete DH handshake, AES-IGE encryption, salt tracking, DC migration — all handled automatically.
🔐
User + Bot Auth
Phone login with 2FA SRP, bot token login, session persistence across restarts.
📡
Typed Update Stream
NewMessage, MessageEdited, CallbackQuery, InlineQuery, ChatAction, UserStatus — all strongly typed.
🔧
Raw API Escape Hatch
Call any of 500+ Telegram API methods directly via client.invoke() with full type safety.
🏗️
Auto-Generated Types
All 2,329 Layer 224 constructors generated at build time from the official TL schema.

Crate overview

layer crate architecture stack
CrateDescriptionTypical user
layer-clientHigh-level async client — auth, send, receive, bots✅ You
layer-tl-typesAll Layer 224 constructors, functions, enumsRaw API calls
layer-mtprotoMTProto session, DH, framing, transportLibrary authors
layer-cryptoAES-IGE, RSA, SHA, auth key derivationInternal
layer-tl-genBuild-time Rust code generatorBuild tool
layer-tl-parser.tl schema → AST parserBuild tool

TIP: Most users only ever import layer-client. The other crates are either used internally or for advanced raw API calls.


Quick install

[dependencies]
layer-client = "0.4.5"
tokio        = { version = "1", features = ["full"] }

Then head to Installation for credentials setup, or jump straight to:


What’s new in v0.4.5

  • StringSessionBackend — portable base64 sessions, no file required
  • LibSqlBackend — libsql/Turso remote database sessions
  • Update::ChatAction and Update::UserStatus — new typed update variants
  • sync_update_state() — force immediate pts/seq reconciliation
  • 7 bug fixes (pagination, memory leaks, chunk alignment, and more)

See the full CHANGELOG.


Acknowledgements

  • Lonami for grammers — the architecture, SRP math, and session design are deeply inspired by this fantastic library.
  • Telegram for the detailed MTProto specification.
  • The Rust async ecosystem: tokio, flate2, getrandom, sha2, and friends.