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

Installation

Add to Cargo.toml

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

layer-client re-exports everything you need for both user clients and bots.


Getting API credentials

Every Telegram API call requires an api_id (integer) and api_hash (hex string) from your registered app.

Step-by-step:

  1. Go to https://my.telegram.org and log in with your phone number
  2. Click API development tools
  3. Fill in any app name, short name, platform (Desktop), and URL (can be blank)
  4. Click Create application
  5. Copy App api_id and App api_hash

SECURITY: Never hardcode credentials in source code. Use environment variables or a secrets file that is in .gitignore.

#![allow(unused)]
fn main() {
// Good — from environment
let api_id:   i32 = std::env::var("TG_API_ID")?.parse()?;
let api_hash: String = std::env::var("TG_API_HASH")?;

// Bad — hardcoded in source
let api_id = 12345;
let api_hash = "deadbeef..."; // ← never do this in a public repo
}

Bot token (bots only)

For bots, additionally get a bot token from @BotFather:

  1. Open Telegram → search @BotFather/start
  2. Send /newbot
  3. Choose a display name (e.g. “My Awesome Bot”)
  4. Choose a username ending in bot (e.g. my_awesome_bot)
  5. Copy the token: 1234567890:ABCdefGHIjklMNOpqrSTUvwxYZ

Optional features

SQLite session storage

layer-client = { version = "0.2.2", features = ["sqlite-session"] }

Stores session data in a SQLite database instead of a binary file. More robust for long-running servers.

Raw type system features (layer-tl-types)

If you use layer-tl-types directly for raw API access:

layer-tl-types = { version = "0.2.2", features = [
    "tl-api",          # Telegram API types (required)
    "tl-mtproto",      # Low-level MTProto types
    "impl-debug",      # Debug trait on all types (default ON)
    "impl-from-type",  # From<types::T> for enums::E (default ON)
    "impl-from-enum",  # TryFrom<enums::E> for types::T (default ON)
    "name-for-id",     # name_for_id(u32) -> Option<&'static str>
    "impl-serde",      # serde::Serialize / Deserialize
] }
FeatureDefaultWhat it enables
tl-apiAll Telegram API constructors and functions
tl-mtprotoLow-level MTProto transport types
impl-debug#[derive(Debug)] on every generated type
impl-from-typeFrom<types::Message> for enums::Message
impl-from-enumTryFrom<enums::Message> for types::Message
name-for-idLook up constructor name by ID — useful for debugging
impl-serdeJSON serialization via serde

Verifying installation

use layer_tl_types::LAYER;

fn main() {
    println!("Using Telegram API Layer {}", LAYER);
    // → Using Telegram API Layer 223
}

Platform notes

PlatformStatusNotes
Linux x86_64✅ Fully supported
macOS (Apple Silicon + Intel)✅ Fully supported
Windows✅ SupportedUse WSL2 for best experience
Android (Termux)✅ WorksNative ARM64
iOS⚠️ UntestedNo async runtime constraints