Telegram WebApp SDK
telegram-webapp-sdk provides a type-safe and ergonomic wrapper around the Telegram Web Apps JavaScript API.
[!NOTE] Comprehensive Coverage
This project achieves comprehensive test coverage for both native and WASM code:
- Native code coverage via
cargo-llvm-cov- WASM code coverage via
wasmcovwith nightly toolchainCoverage reports include all modules (leptos, yew, api, webapp, logger, pages, router) ensuring quality across the entire codebase.
For implementation details, see issue #130.
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Table of contents
- Coverage Graphs
- Features
- Macros
- Router
- Installation
- Quick start
- Mock environment
- User interactions
- Keyboard control
- Closing confirmation
- Invoice payments
- Sharing
- Settings button
- Cloud storage
- Home screen
- Event callbacks
- Appearance
- Viewport
- Fullscreen and orientation
- Haptic feedback
- Device storage
- Secure storage
- Biometric authentication
- Location manager
- Device sensors
- Init data validation
- API coverage
- Changelog
- License
- Metrics
Features
- Comprehensive coverage of Telegram Web App JavaScript APIs.
- Framework integrations for Yew and Leptos.
- Optional macros for automatic initialization and routing.
- Biometric authentication helpers, viewport metrics, and theme utilities in step with the Telegram WebApp API 9.2 feature set.
Macros
The macros are available with the macros feature. Enable it in your Cargo.toml:
= { = "0.2.15", = ["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!;
Installation
Add the crate to your Cargo.toml:
[]
= "0.2"
Enable optional features as needed:
= { = "0.2.15", = ["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;
#
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.
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:
use get_theme_params;
use TelegramWebApp;
#
Viewport
Inspect the Mini App viewport size and subscribe to updates:
use ;
use Closure;
#
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
Guard privileged actions behind the BiometricManager API:
use ;
#
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
Retrieving raw initData
Retrieve the raw URL-encoded initData string for server-side authentication.
The SDK captures this string during initialization and provides convenient
access without requiring JavaScript reflection:
use TelegramWebApp;
#
This eliminates the need for manual Reflect calls and ensures consistency
with the parsed data available in the context.
Validating initData
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-21).
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.