| name | telegram-mini-apps-sdk |
| description | Comprehensive manual for Telegram Mini Apps SDK. Use when developers need guidance on creating web applications inside Telegram, working with WebApp API, managing user data, handling authentication via initData, implementing buttons and events, working with storage, and integrating with Telegram ecosystem features. |
| license | MIT |
Telegram Mini Apps SDK Manual
Complete guide for building Telegram Mini Apps with JavaScript SDK. Covers initialization, WebApp parameters, button management, events, themes, user authentication, data storage, and practical code examples.
Quick Start
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
tg.BackButton.show();
tg.MainButton.setText('Send').show();
tg.MainButton.onClick(() => {
tg.showAlert('Button pressed!');
});
Core Concepts
1. Initialization
Add SDK script before any other scripts:
<script src="https://telegram.org/js/telegram-web-app.js?59"></script>
Then initialize:
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
2. User Data (initData)
Access user information:
const user = tg.initDataUnsafe.user;
console.log(user.id, user.first_name, user.username);
3. Main Button
const mainBtn = tg.MainButton;
mainBtn.setText('Send');
mainBtn.show();
mainBtn.onClick(() => {
mainBtn.showProgress();
mainBtn.hideProgress();
});
4. Back Button
tg.BackButton.show();
tg.BackButton.onClick(() => {
tg.close();
});
5. Data Storage
tg.CloudStorage.setItem('key', 'value', (error) => {
if (!error) console.log('Saved');
});
tg.CloudStorage.getItem('key', (error, value) => {
console.log('Value:', value);
});
6. Themes
console.log(tg.colorScheme);
console.log(tg.themeParams);
tg.onEvent('themeChanged', () => {
console.log('Theme updated');
});
7. Events
tg.onEvent('activated', () => {});
tg.onEvent('deactivated', () => {});
tg.onEvent('mainButtonClicked', () => {});
tg.onEvent('backButtonClicked', () => {});
tg.onEvent('themeChanged', () => {});
8. Haptic Feedback
const haptic = tg.HapticFeedback;
haptic.impactOccurred('light');
haptic.notificationOccurred('success');
haptic.selectionChanged();
Security Best Practices
- Validate initData on server - Never trust
initDataUnsafe for sensitive operations
- Use HTTPS only - All communication must be encrypted
- Check data freshness - Verify
auth_date is recent
- Store secrets securely - Use
SecureStorage for tokens
- Expose minimal API - Only provide necessary endpoints to frontend
Further Resources
See the references/ directory for complete API documentation.
See the scripts/ directory for ready-to-use utilities.
See the assets/ directory for HTML/React templates.
Official Links