Init Data
The 💠component responsible for the Telegram Mini Apps init data.
Restoring
To restore the component state, use the restore
method.
import { initData } from '@telegram-apps/sdk';
initData.restore();
import { restoreInitData } from '@telegram-apps/sdk';
restoreInitData();
Parsing
To parse the value as init data, use the parseInitData
function.
import { parseInitData } from '@telegram-apps/sdk';
const initData = parseInitData();
// {
// user: {
// id: 99281932,
// firstName: 'Andrew',
// lastName: 'Rogue',
// username: 'rogue',
// languageCode: 'en',
// isPremium: true,
// allowsWriteToPm: true,
// },
// hash: 'abcedef123',
// authDate: Date(1716922846000),
// startParam: 'debug',
// chatType: 'sender',
// chatInstance: '8428209589180549439',
// };
The function returns the init data object with deeply camel-cased properties.
Signals
This section provides a complete list of signals related to the init data.
authDate
Return type: Date | undefined
The date the initialization data was created.
initData.authDate(); // Date(1727368894000)
import { initDataAuthDate } from '@telegram-apps/sdk';
initDataAuthDate(); // Date(1727368894000)
canSendAfter
Return type: number | undefined
The number of seconds after which a message can be sent via the method answerWebAppQuery.
initData.canSendAfter(); // 3600
import { initDataCanSendAfter } from '@telegram-apps/sdk';
initDataAuthDate(); // 3600
canSendAfterDate
Return type: Date | undefined
canSendAfter but as a Date.
initData.canSendAfterDate(); // Date(1727368897600)
import { initDataCanSendAfterDate } from '@telegram-apps/sdk';
initDataCanSendAfterDate(); // Date(1727368897600)
chat
Return type: undefined
or Chat
with camel-cased properties.
An object containing data about the chat where the bot was launched via the attachment menu.
NOTE
Returned for supergroups, channels and group chats – only for Mini Apps launched via the attachment menu.
initData.chat();
// {
// id: 7728725378876215,
// type: 'group',
// title: '@BotFather',
// photoUrl: 'https://example.com/image.png',
// username: 'botfather'
// }
import { initDataChat } from '@telegram-apps/sdk';
initDataChat();
// {
// id: 7728725378876215,
// type: 'group',
// title: '@BotFather',
// photoUrl: 'https://example.com/image.png',
// username: 'botfather'
// }
chatType
Return type: string | undefined
The type of chat from which the Mini Apps was opened. Values:
sender
private
group
supergroup
channel
NOTE
Returned only for applications opened by direct link.
initData.chatType(); // 'group'
import { initDataChatType } from '@telegram-apps/sdk';
initDataChatType(); // 'group'
chatInstance
Return type: string | undefined
A global identifier indicating the chat from which the Mini Apps was opened.
WARNING
Returned only for applications opened by direct link.
initData.chatInstance(); // 'group'
import { initDataChatInstance } from '@telegram-apps/sdk';
initDataChatInstance(); // '899667289674387257'
hash
Return type: string | undefined
Initialization data signature.
initData.hash(); // 'group'
import { initDataHash } from '@telegram-apps/sdk';
initDataHash(); // 'sgbbc62g3bvdhg3djsaasd'
queryId
Return type: string | undefined
The unique session ID of the Mini App. Used in the process of sending a message via the method answerWebAppQuery.
initData.queryId(); // 'group'
import { initDataQueryId } from '@telegram-apps/sdk';
initDataQueryId(); // 'ssVXZ231ger'
raw
Return type: string | undefined
A raw string representation of the initialization data.
initData.raw(); // 'user=...&chat=...&...'
import { initDataRaw } from '@telegram-apps/sdk';
initDataRaw(); // 'user=...&chat=...&...'
receiver
Return type: undefined
or User
with camel-cased properties.
An object containing data about the chat partner of the current user in the chat where the bot was launched via the attachment menu.
NOTE
Returned only for private chats and only for Mini Apps launched via the attachment menu.
initData.user();
// {
// addedToAttachmentMenu: false,
// allowsWriteToPm: true,
// isPremium: true,
// firstName: 'Pavel',
// id: 78262681,
// isBot: false,
// lastName: 'Durov',
// languageCode: 'ru',
// photoUrl: 'https://example.com/image.png',
// username: 'durove',
// }
import { initDataUser } from '@telegram-apps/sdk';
initDataUser();
// {
// addedToAttachmentMenu: false,
// allowsWriteToPm: true,
// isPremium: true,
// firstName: 'Pavel',
// id: 78262681,
// isBot: false,
// lastName: 'Durov',
// languageCode: 'ru',
// photoUrl: 'https://example.com/image.png',
// username: 'durove',
// }
state
Return type: undefined
or InitData
with deeply camel-cased properties.
An object containing the initialization data in object format.
initData.state();
import { initDataState } from '@telegram-apps/sdk';
initDataState();
startParam
Return type: string | undefined
The value of the startattach
or startapp
query parameter specified in the link.
initData.startParam(); // 'my-value'
import { initDataStartParam } from '@telegram-apps/sdk';
initDataStartParam(); // 'my-value'
user
Return type: undefined
or User
with camel-cased properties.
An object containing information about the current user.
initData.user();
// {
// addedToAttachmentMenu: false,
// allowsWriteToPm: true,
// isPremium: true,
// firstName: 'Pavel',
// id: 78262681,
// isBot: false,
// lastName: 'Durov',
// languageCode: 'ru',
// photoUrl: 'https://example.com/image.png',
// username: 'durove',
// }
import { initDataUser } from '@telegram-apps/sdk';
initDataUser();
// {
// addedToAttachmentMenu: false,
// allowsWriteToPm: true,
// isPremium: true,
// firstName: 'Pavel',
// id: 78262681,
// isBot: false,
// lastName: 'Durov',
// languageCode: 'ru',
// photoUrl: 'https://example.com/image.png',
// username: 'durove',
// }