Init Data
When a Mini App launches, the client passes a signed initialization data string through Telegram.WebApp.initData. This string contains information about the user, chat, and launch parameters.
Telegram.WebApp.initDataUnsafe provides the parsed WebAppInitData object for convenient client-side access.
Security Notice
Data in initDataUnsafe has not been server-side verified and should only be used for client-side display. Business-critical data must be verified on the server using the raw initData string through signature validation.
WebAppInitData
Initialization data object, accessed via Telegram.WebApp.initDataUnsafe.
| Field | Type | Description |
|---|---|---|
query_id | String | Unique query ID for answerWebAppQuery |
user | WebAppUser | Current user information |
receiver | WebAppUser | Other user in the conversation (only available in private chats) |
chat | WebAppChat | Current chat information (only available in groups/channels) |
chat_type | String | Chat type: "sender", "private", "group", "supergroup", "channel" |
chat_instance | String | Unique identifier of the chat instance |
start_param | String | Start parameter passed via link |
can_send_after | Integer | Earliest time to call sendData (Unix timestamp) |
auth_date | Integer | Authentication time (Unix timestamp) |
hash | String | Data signature hash for verification |
signature | String | Data signature for third-party verification. Bot API 8.0+ |
WebAppUser
User information object.
| Field | Type | Description |
|---|---|---|
id | Integer | User ID |
is_bot | Boolean | Whether the user is a bot |
first_name | String | First name |
last_name | String | Last name (optional) |
username | String | Username (optional) |
language_code | String | User language code (e.g., "zh-hans", "en") |
is_premium | Boolean | Whether the user is a Premium user (optional) |
added_to_attachment_menu | Boolean | Whether the user has added the bot to attachment menu (optional) |
allows_write_to_pm | Boolean | Whether the user allows the bot to send private messages (optional) |
photo_url | String | User avatar URL (optional) |
WebAppChat
Chat information object.
| Field | Type | Description |
|---|---|---|
id | Integer | Chat ID |
type | String | Chat type: "group", "supergroup", "channel" |
title | String | Chat title |
username | String | Chat username (optional) |
photo_url | String | Chat avatar URL (optional) |
Usage Example
const tg = Telegram.WebApp;
const initData = tg.initDataUnsafe;
// Get user info
if (initData.user) {
console.log('User ID:', initData.user.id);
console.log('Name:', initData.user.first_name);
console.log('Language:', initData.user.language_code);
console.log('Premium:', initData.user.is_premium);
}
// Get chat info
if (initData.chat) {
console.log('Chat ID:', initData.chat.id);
console.log('Chat title:', initData.chat.title);
}
// Get start parameter
if (initData.start_param) {
console.log('Start param:', initData.start_param);
handleStartParam(initData.start_param);
}
// Send raw data to server for validation
fetch('/api/auth', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ initData: tg.initData })
});initData Query String Format
Telegram.WebApp.initData is a URL-encoded query string containing the following parameters:
query_id=AAHd...&user=%7B%22id%22%3A123...%7D&auth_date=1234567890&hash=abc123...When parsed, it contains the following key-value pairs:
| Key | Value |
|---|---|
query_id | Query ID string |
user | JSON-encoded user object |
receiver | JSON-encoded receiver object |
chat | JSON-encoded chat object |
chat_type | Chat type string |
chat_instance | Chat instance ID |
start_param | Start parameter |
can_send_after | Allowed send timestamp |
auth_date | Authentication timestamp |
hash | HMAC-SHA256 signature |
signature | Ed25519 signature |
For detailed validation methods, see Data Validation.
