Telegram WebApp SDK
telegram-webapp-sdk provides a type-safe and ergonomic wrapper around the Telegram Web Apps JavaScript API.
Features
- Comprehensive coverage of Telegram Web App JavaScript APIs.
- Framework integrations for Yew and Leptos.
- Optional macros for automatic initialization and routing.
Macros
The macros are available with the macros feature. Enable it in your Cargo.toml:
= { = "0.2.4", = ["macros"] }
Reduce boilerplate in Telegram Mini Apps using the provided macros:
telegram_page!;
telegram_app!;
When running outside Telegram in debug builds, telegram_app! loads mock
settings from telegram-webapp.toml.
- Configurable mock
Telegram.WebAppfor local development and testing. - API helpers for user interactions, storage, device sensors and more.
Router
The macros feature ships with a minimal in-memory Router
that collects pages registered via telegram_page!. The
telegram_router! macro builds this router and runs all page
handlers:
telegram_page!;
// Uses the default Router
telegram_router!;
Provide a custom router type to the macro if additional behavior is required:
;
telegram_router!;
Table of contents
- Installation
- Quick start
- Mock environment
- User interactions
- Keyboard control
- API coverage
- Changelog
- License
Installation
Add the crate to your Cargo.toml:
[]
= "0.2"
Enable optional features as needed:
= { = "0.2.4", = ["macros", "yew", "mock"] }
macros— enablestelegram_app!,telegram_page!, andtelegram_router!.yew— exposes ause_telegram_contexthook and aBottomButtoncomponent.leptos— integrates the context into the Leptos reactive system.mock— installs a configurable mockTelegram.WebAppfor local development.
Quick start
Yew
use use_telegram_context;
use *;
Use BottomButton to control the main button:
use BottomButton;
use *;
Leptos
use *;
use provide_telegram_context;
The SDK also provides a BottomButton component for Leptos to control Telegram bottom buttons:
use *;
use ;
use BottomButton as Btn;
Mock environment
The mock feature simulates a Telegram.WebApp instance, enabling local development without Telegram:
let config = default;
let ctx = install?;
User interactions
Request access to sensitive user data or open the contact interface:
use ;
use TelegramWebApp;
#
These calls require the user's explicit permission before any information is shared.
Keyboard control
Hide the native keyboard when it's no longer required:
use TelegramWebApp;
#
Closing confirmation
Prompt users before the Mini App closes:
use TelegramWebApp;
#
Invoice payments
Open invoices and react to the final payment status:
use TelegramWebApp;
#
Sharing
Share links, prepared messages, or stories and join voice chats:
use Object;
use TelegramWebApp;
#
Settings button
Control the Telegram client's settings button and handle user clicks:
use ;
use Closure;
#
Cloud storage
Persist small key-value pairs in Telegram's cloud using CloudStorage:
use Reflect;
use ;
use JsFuture;
# async
All functions return a Promise and require the Web App to run inside Telegram.
Home screen
Prompt users to add the app to their home screen and check the current status:
use TelegramWebApp;
#
Event callbacks
Callback registration methods return an EventHandle for later deregistration.
use TelegramWebApp;
#
Background events
Some Telegram events may fire while the Mini App is in the background. Register
callbacks for these with on_background_event:
use ;
#
Supported background events:
| Event | Payload |
|---|---|
mainButtonClicked |
none |
backButtonClicked |
none |
settingsButtonClicked |
none |
writeAccessRequested |
bool granted flag |
contactRequested |
bool shared flag |
phoneRequested |
bool shared flag |
invoiceClosed |
status String |
popupClosed |
object { button_id: Option<String> } |
qrTextReceived |
scanned text String |
clipboardTextReceived |
clipboard text String |
Appearance
Customize colors and react to theme or safe area updates:
Fullscreen and orientation
Control the Mini App display and screen orientation:
use TelegramWebApp;
#
Haptic feedback
Trigger device vibrations through Telegram's HapticFeedback API:
use ;
impact_occurred?;
notification_occurred?;
selection_changed?;
# Ok::
Device storage
Persist lightweight data on the user's device:
use ;
# async
Secure storage
Store sensitive data encrypted and restorable:
use ;
# async
Location manager
Retrieve user location and react to related events via Telegram's location manager:
use ;
use Closure;
init?;
let _ = get_location;
open_settings?;
let cb = wrap;
on_location_requested?;
cb.forget;
# Ok::
Device sensors
Access motion sensors if the user's device exposes them.
use ;
start?;
let reading = get_acceleration;
stop?;
# Ok::
Callbacks for sensor lifecycle events are available through on_started,
on_changed, on_stopped, and on_failed functions for accelerometer,
gyroscope, and device orientation sensors.
Init data validation
Validate the integrity of the Telegram.WebApp.initData payload on the server.
The validate_init_data module is re-exported at the crate root and can be
used directly or through the TelegramWebApp::validate_init_data helper:
use ;
let bot_token = "123456:ABC";
let query = "user=alice&auth_date=1&hash=48f4c0e9d3dd46a5734bf2c5d4df9f4ec52a3cd612f6482a7d2c68e84e702ee2";
validate_init_data?;
// For Ed25519-signed data
# use ;
# let sk = from_bytes;
# let pk = sk.verifying_key;
# let sig = sk.sign;
# let init_data = format!;
validate_init_data?;
# Ok::
API coverage
WebApp API coverage: version 9.2 matches the latest Telegram WebApp API release 9.2. Synced in commit 92abbf7 (recorded on 2025-09-20).
See WEBAPP_API.md for a checklist of supported Telegram WebApp JavaScript API methods and features.
Changelog
See CHANGELOG.md for release notes.
License
telegram-webapp-sdk is licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.