Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
"group": "TMA: Telegram Mini Apps",
"pages": [
"ecosystem/tma/overview",
"ecosystem/tma/apps-communication",
"ecosystem/tma/create-mini-app",
{
"group": "Telegram UI",
Expand Down
120 changes: 120 additions & 0 deletions ecosystem/tma/apps-communication.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
---
title: "Apps communication"
---

import { Aside } from '/snippets/aside.jsx';

The Telegram client and a Mini App communicate through a message-passing interface. The exact transport depends on the platform, but the protocol is the same: the Mini App sends **methods** to the Telegram client, and the Telegram client sends **events** back to the Mini App.

## Methods and events

Two terms describe the communication directions:

- **Methods** — calls sent from the Mini App to the Telegram client. A method triggers an action in the native app (for example, showing a popup or expanding the viewport).
- **Events** — signals sent from the Telegram client to the Mini App. An event notifies the Mini App that something happened (for example, the viewport changed or a button was pressed).

Internally both are messages, but "methods" and "events" distinguish the direction throughout the Mini Apps platform.

## Transport by platform

The transport mechanism varies by Telegram client:

| Platform | Transport | Details |
| -------------------------------------------------- | --------------------------------------- | ------------------------------------------------------------------------------------ |
| Web (Telegram Web A, Web K) | `window.parent.postMessage` | The Mini App runs in an `<iframe>`. Messages are JSON strings passed between frames. |
| Desktop and mobile (Android, iOS, macOS, tdesktop) | `window.TelegramWebviewProxy.postEvent` | Telegram injects a global function into the WebView. |
| Windows Phone | `window.external.notify` | Same JSON format as the web version. |

## Calling methods

### Web

The web version displays the Mini App in an `<iframe>`. Communication uses `window.parent.postMessage` with a JSON string:

```typescript
const data = JSON.stringify({
eventType: 'web_app_setup_back_button',
eventData: { is_visible: true },
});

window.parent.postMessage(data, 'https://web.telegram.org');
```

Pass a [target origin](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#targetorigin) to restrict which parent frame can receive the message. Use `https://web.telegram.org` as the default.

### Desktop and mobile

Desktop and mobile clients inject `window.TelegramWebviewProxy.postEvent` into the WebView:

```typescript
window.TelegramWebviewProxy.postEvent(
'web_app_setup_back_button',
JSON.stringify({ is_visible: true })
);
```

### Windows Phone

The Windows Phone client provides `window.external.notify` with the same JSON format as the web version:

```typescript
window.external.notify(JSON.stringify({
eventType: 'web_app_setup_back_button',
eventData: { is_visible: true },
}));
```

## Listening to events

### Web

The parent iframe sends events via `window.postMessage`. Add a `message` listener:

```typescript
window.addEventListener('message', ({ data }) => {
const { eventType, eventData } = JSON.parse(data);
console.log(eventType, eventData);
});
```

<Aside
type="caution"
>
In production, validate that the `message` event originates from the Telegram application and that `data` is a string before parsing.
</Aside>

### Desktop, mobile, and Windows Phone

These platforms deliver events by calling a globally defined function:

| Platform | Function path |
| ---------------------------- | --------------------------------------- |
| Telegram Desktop | `window.TelegramGameProxy.receiveEvent` |
| Telegram for iOS and Android | `window.Telegram.WebView.receiveEvent` |
| Windows Phone | `window.TelegramGameProxy_receiveEvent` |

All functions share the same signature:

```typescript
type ReceiveEvent = (eventType: string, eventData: unknown) => void;
```

To handle events on all platforms, assign a handler function to all three paths.

## Using the SDK

The [@tma.js/sdk](https://www.npmjs.com/package/@tma.js/sdk) package abstracts away platform differences and provides a unified API for calling methods and listening to events:

```typescript
import { postEvent, on } from '@tma.js/sdk';

// Call a method
postEvent('web_app_set_header_color', { color_key: 'bg_color' });

// Listen to an event
const removeListener = on('viewport_changed', payload => {
console.log('Viewport changed:', payload);
});
```

For the full list of available methods and events, see the [@tma.js/sdk documentation](https://docs.telegram-mini-apps.com/packages/tma-js-sdk).
55 changes: 49 additions & 6 deletions ecosystem/tma/overview.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: "TMA: Telegram Mini Apps overview"
title: "Telegram Mini Apps overview"
sidebarTitle: "Overview"
---

Telegram Mini Apps (TMAs) are web applications that run within the Telegram messenger. They are built using web technologies — HTML, CSS, and JavaScript.
Telegram Mini Apps (TMAs) are web applications that run inside the Telegram messenger. Built with standard web technologies — HTML, CSS, and JavaScript — they display in a WebView provided by the Telegram client.

<video
autoPlay
Expand All @@ -15,10 +15,53 @@ Telegram Mini Apps (TMAs) are web applications that run within the Telegram mess
Your browser does not support the \<video> tag.
</video>

Mini Apps run within Telegram and are instantly accessible — without the need for installation or redirects.
Mini Apps are an extension of [Telegram Bots](https://core.telegram.org/bots). Every Mini App is associated with a bot, which serves as the entry point. The bot provides the application URL, and Telegram loads it in a WebView.

## Mini Apps vs default bot interface

Bots are limited to text messages, inline buttons, and menus. Mini Apps remove that constraint: they render arbitrary web interfaces inside a Telegram WebView — forms, dashboards, storefronts, games — while the bot behind them handles authentication, payments, and push notifications through the [Bot API](https://core.telegram.org/bots/api).

## Required technologies

A Mini App is a standard web application. Any frontend stack works: plain HTML/CSS/JavaScript, or frameworks such as React, Vue, or Svelte. Telegram only requires a publicly accessible HTTPS URL that serves the application.

Instead of handling platform differences manually (see [Apps communication](/ecosystem/tma/apps-communication)), use the [@tma.js/sdk](https://www.npmjs.com/package/@tma.js/sdk) package — a typed TypeScript API covering all Mini Apps methods and events.

## Supported Telegram clients

| Client | Platform identifier |
| --------------------------------------------------------------------- | ------------------- |
| [Telegram for Android](https://github.com/DrKLO/Telegram) | `android` |
| [Telegram for iOS](https://github.com/TelegramMessenger/Telegram-iOS) | `ios` |
| [Telegram for macOS](https://github.com/overtake/TelegramSwift) | `macos` |
| [Telegram Desktop](https://github.com/telegramdesktop/tdesktop) | `tdesktop` |
| [Telegram Web A](https://github.com/Ajaxy/telegram-tt) | `weba` |
| [Telegram Web K](https://github.com/morethanwords/tweb) | `web` |

Each client is developed independently. Implementations may vary — report discrepancies to the [Telegram Mini Apps issues tracker](https://github.com/Telegram-Mini-Apps/issues).

## TMA developer tools

- [TMA create CLI](/ecosystem/tma/create-mini-app)
- [Telegram UI Kit](/ecosystem/tma/telegram-ui/overview)
- [Analytics](/ecosystem/tma/analytics/analytics)
<Columns cols={3}>
<Card
title="Create Mini App CLI"
icon="terminal"
href="/ecosystem/tma/create-mini-app"
/>

<Card
title="Telegram UI Kit"
icon="palette"
href="/ecosystem/tma/telegram-ui/overview"
/>

<Card
title="Analytics"
icon="chart-simple"
href="/ecosystem/tma/analytics/analytics"
/>
</Columns>

## Platform fundamentals

- [Apps communication](/ecosystem/tma/apps-communication) — how Mini Apps exchange data with the Telegram client
2 changes: 2 additions & 0 deletions resources/dictionaries/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -722,8 +722,10 @@ TES
Testgiver
testnet
Testnet
tdesktop
Tezos
timepoint
tma
TMA
TMAs
tock
Expand Down
Loading