Skip to content
Back to blog

Telegram Webhook Tester & Mini App initData Verifier

5 min read
Telegram Webhook Tester & Mini App initData Verifier

Two problems show up on every Telegram bot that grows a Mini App attached to it. The first is figuring out what a webhook Update actually contains — the object has more than twenty optional payload fields and exactly one of them is populated. The second is proving that a Mini App's initData genuinely came from Telegram, rather than from someone who opened your app URL and typed a different user ID.

Both are easy to get subtly wrong in ways that still look like they work.

What Telegram Webhook Tester does

Telegram Webhook Tester is a browser tool that covers those two jobs side by side:

  • Webhook Tester — detects the update type, decodes entities with correct offset handling, visualises reply_markup, and replays the payload to your own endpoint with an optional secret token.
  • initData Verify & Sign — recomputes the HMAC-SHA256 signature with a full algorithm trace, and can also produce correctly signed strings for local test fixtures.

Nothing is uploaded. The bot token is used with the Web Crypto API in the page and never leaves it.

Webhook testing and replay

An Update is a wrapper. Your handler branches on which field is present — message, callback_query, pre_checkout_query, my_chat_member, and so on — and the decoder tells you immediately which one you got, along with what it means. Some of those matter more than others: an unanswered callback_query leaves a spinner on the user's button, and a pre_checkout_query must be answered within ten seconds or the payment fails.

The part that quietly breaks production code is entities. Telegram sends formatting as a separate array of {offset, length, type} records, and those offsets are counted in UTF-16 code units, not characters.

Consider the message 🚀 Deploy /status now — see docs. The word docs starts at offset 28, not 27, because the rocket emoji is a surrogate pair and occupies two units on its own. JavaScript strings are natively UTF-16, so slice() matches Telegram's numbering exactly. Python and Go do not: slicing by code point puts you two characters off for every emoji earlier in the string, and the bug only appears once a user sends one.

The decoder shows the exact substring each entity selects, so a wrong offset is visible instead of theoretical.

Replay sends the same payload to a URL you specify, with X-Telegram-Bot-Api-Secret-Token attached if you provide it. That lets you re-trigger a handler against a real captured update instead of waiting for a user to reproduce the bug. Because the request originates from the browser it is subject to CORS — your endpoint receives it either way, but the response is only readable if the endpoint allows it.

Verifying and signing initData

When a Mini App launches, Telegram hands the frontend a query string in window.Telegram.WebApp.initData containing the user object, auth_date, and a hash. Your backend must recompute that hash before believing anything inside it. Skip the check and any visitor can open the app URL with a hand-written user field and be whoever they like.

The algorithm has two steps people invert:

secret_key = HMAC_SHA256(key="WebAppData", msg=bot_token)
hash       = HMAC_SHA256(key=secret_key,   msg=data_check_string)

Note the first line carefully: the literal string WebAppData is the key and your bot token is the message. Swapping them produces a perfectly plausible-looking digest that never matches.

The data_check_string is every field except hash, formatted as key=value, sorted alphabetically, joined with newlines. Sorting is not cosmetic — Telegram signed that exact ordering, so hashing the fields in arrival order fails against a legitimate payload.

One property deserves emphasis: a valid signature never expires by itself. It stays valid forever. auth_date is the only thing standing between you and an initData string captured months ago and replayed today, so the backend has to enforce a maximum age explicitly. The tool reports freshness separately from signature validity for that reason — an archived fixture is correctly signed and correctly stale, and conflating the two hides a real vulnerability.

Signing exists for testing. A string signed here is only valid against the bot token that produced it, which makes it useful for fixtures and integration harnesses and useless as an attack: without the real token, no forged initData will ever verify against a real bot. Every signed string is immediately verified back, so a signer that produced something its own verifier rejects would be caught on screen.

Login Widget: the same HMAC, a different key

Telegram has a second authentication scheme, and it is the one people implement wrong most often. The Login Widget is the "Log in with Telegram" button on an ordinary website: the user taps it and Telegram redirects back — or calls your data-onauth handler — with id, first_name, auth_date and a hash.

It is tempting to assume this is a different algorithm. It is not. Both schemes build the same kind of data-check string (every field except hash, sorted alphabetically, joined with newlines) and both finish with HMAC-SHA256 over it. The only difference is how the secret key is derived:

Login Widget    secret_key = SHA256(bot_token)
Mini App        secret_key = HMAC_SHA256(key="WebAppData", msg=bot_token)

For the Login Widget the key is a plain SHA-256 hash of the token — no HMAC at that step. For initData it is an HMAC in which the literal string WebAppData is the key and the token is the message.

That is the whole difference, and it is enough to make an otherwise correct implementation never match a real callback. Both derivations produce a perfectly plausible 64-character hex digest, so the failure mode is not an exception or a malformed value — it is a signature that simply never equals the one Telegram sent, with nothing on screen to say why. Two schemes that agree on ninety percent of their steps are exactly where a copy-pasted verifier silently inherits the wrong line.

Both live on the same page here for that reason: one HMAC implementation, one constant-time comparison, one set of known-answer vectors — and a guard that asserts each scheme's payload is rejected under the other's key derivation, so the two can never quietly converge.

The auth_date rule carries over unchanged. Telegram's own reference implementation treats a Login Widget callback older than 86400 seconds as stale, and for the same reason as initData: the signature itself never expires, so the age limit is yours to enforce.

Workflow

  1. Capture a real update from your webhook log, or press Load sample.
  2. Read the detected type and check the entity slices against what you expected.
  3. Point Replay at your staging endpoint and re-fire the update as often as you need.
  4. Switch to initData, paste your bot token, and verify a real launch string.
  5. If you use the "Log in with Telegram" button anywhere, verify one of those callbacks on the Login Widget tab too — it is the same token, a different key derivation.
  6. Copy the server-side verification function for JavaScript, Python, PHP or Go and paste it into your backend.

The exported code enforces both halves of the check — the signature and the age limit — because a verifier that only does the first is the common production mistake.

Frequently asked questions

How do I test a Telegram webhook without deploying a bot?

Paste the raw JSON update your endpoint received (or one from getUpdates) into the decoder. It detects the update type, decodes message entities and reply_markup, and can replay the same payload to any URL you choose.

Why does my entity offset cut the text in the wrong place?

Telegram counts entity offsets in UTF-16 code units, not characters. A single emoji is a surrogate pair and occupies two units, so every offset after it shifts by two. Python and Go slice by code point by default, which is what breaks.

How is Telegram Mini App initData verified?

The secret key is HMAC-SHA256 with the literal string WebAppData as the key and your bot token as the message. The hash is then HMAC-SHA256 of the data check string using that secret key. Fields are sorted alphabetically and the hash field is excluded.

Can someone forge initData if they know my user ID?

No. Forging a valid hash requires the bot token. That is exactly why the backend must recompute the signature instead of trusting the user object inside initData.

Does a valid initData signature ever expire?

Not on its own. The signature stays valid indefinitely, so you must enforce a maximum age against the auth_date field yourself, otherwise a captured string can be replayed months later.

How does Telegram Login Widget verification differ from Mini App initData?

Only in the key derivation. Both build the same sorted data check string and both finish with HMAC-SHA256 over it. For the Login Widget the secret key is a plain SHA-256 hash of the bot token. For Mini App initData it is HMAC-SHA256 with the literal string WebAppData as the key and the token as the message. Using the wrong one produces a valid-looking digest that never matches.

What is the secret_token on setWebhook for?

Telegram echoes it back verbatim in the X-Telegram-Bot-Api-Secret-Token header on every delivery. Comparing it proves the request came from Telegram. Note that it authenticates the sender, not the body — nothing is signed.

Is my bot token sent anywhere?

No. All HMAC work runs through the Web Crypto API in your browser. The optional Remember checkbox uses sessionStorage, which is cleared when the tab closes.

Author

Fullstack Engineer

In commercial development since 2011 — over 15 years. Author of two SaaS products, TeleGo.io and LingvoHabit.com. The articles here come out of real client and product work.