Telegram Mini Apps Platform Documentation

website·Indexed 33 days ago

https://docs.telegram-mini-apps.com

Documentation for building web applications within Telegram. Includes guides on the platform's viewport, theming, and closing behavior, as well as technical details on init data validation, user authorization, and app communication via methods and events. Features documentation for the tma.js SDK, bridge, and Node.js init-data validation packages.

Tokens
37.7K
Snippets
239
Records
331
Agent score
65%

What's inside Telegram Mini Apps

  1. Overview of @tma.js/bridge

    @tma.js/bridge is the lowest-level communication layer for Telegram Mini Apps. It provides fundamental utilities and types for platform development. While it can be used standalone, it is generally recommended to use the higher-level @tma.js/sdk package for most application needs.
  2. Understand Telegram Mini App communication patterns

    Telegram Mini Apps operate in a web-based environment provided by the Telegram native application. Communication between the native app and the Mini App is based on two primary patterns:

    1. Events: Messages or notifications sent from the Telegram native application to the Mini App.
    2. Methods: Functions called by the Mini App that are executed by the Telegram native application.
  3. Understand the Telegram Mini Apps architecture

    Telegram Mini Apps are web applications displayed within a WebView inside the Telegram client. They function as add-ons to Telegram Bots; therefore, a Telegram Bot must be created before a Mini App can be implemented. The developer provides a URL for the web application, which the Telegram client loads as the source for its WebView component.
  4. Implement the Main Button in Telegram Mini Apps

    The Main Button is a persistent UI component used for final actions, such as submitting a shopping cart or sending data. Developers can control the button's text, active state, visibility, and background colors. To provide a better user experience during long-running operations, it is recommended to enable the internal loader to indicate the application is processing a request.
  5. Use the Telegram Test Environment for Mini App development

    Telegram provides a dedicated test environment for developers. Unlike the production environment, which requires HTTPS for Mini App links, the test environment allows developers to specify Mini App links using the HTTP protocol or direct IP addresses. This is essential for local development and testing before deploying to production.
  6. Mount and unmount the Mini App component

    The miniApp component must be mounted before use. To ensure properties have correct values, themeParams must be mounted before calling miniApp.mount(). The isMounted signal tracks the current state. Use unmount() to tear down the component.
    import { miniApp, themeParams } from '@tma.js/sdk';
    
    // Correct mounting sequence
    themeParams.mount();
    miniApp.mount();
    console.log(miniApp.isMounted()); // true
    
    // Unmounting
    miniApp.unmount();
    console.log(miniApp.isMounted()); // false
  7. Install @tma.js/sdk-react

    The @tma.js/sdk-react package provides React.js utilities for Telegram Mini Apps. It has react as a peer dependency and fully re-exports the @tma.js/sdk package, meaning you do not need to install the base SDK separately.
    # Using pnpm
    pnpm i @tma.js/sdk-react
    
    # Using npm
    npm i @tma.js/sdk-react
    
    # Using yarn
    yarn add @tma.js/sdk-react
  8. Mount and unmount the Secondary Button

    The secondaryButton must be mounted to work with properly configured properties. Because it depends on the miniApp and themeParams components, those must be mounted first. Use the mount() method to initialize and unmount() to clean up. The isMounted signal tracks the current state.
    import { secondaryButton, miniApp, themeParams } from '@tma.js/sdk';
    
    // Order of mounting is critical
    miniApp.mount();
    themeParams.mount();
    secondaryButton.mount();
    
    console.log(secondaryButton.isMounted()); // true
    
    secondaryButton.unmount();
    console.log(secondaryButton.isMounted()); // false
  9. Install @tma.js/init-data-node

    Install the @tma.js/init-data-node package to handle Telegram Mini Apps initialization data on the server side using npm, pnpm, or yarn.
    pnpm i @tma.js/init-data-node
    # or
    npm i @tma.js/init-data-node
    # or
    yarn add @tma.js/init-data-node
  10. Use functional API alternatives in @tma.js/bridge to handle errors as values

    The @tma.js/bridge package provides functional alternatives to its standard functions (suffixed with Fp). Instead of throwing exceptions, these functions return errors as values. It is recommended to use a library like fp-ts to process these results, typically using the Either type to handle success and failure cases explicitly.
    import * as E from 'fp-ts/Either';
    import { pipe } from 'fp-ts/function';
    import { postEventFp, retrieveLaunchParamsFp } from '@tma.js/bridge';
    
    // Example: Posting an event functionally
    pipe(
      postEventFp('web_app_expand'), 
      E.match(
        error => {
          // Handle the specific error here
        },
        () => {
          // Success case
        }
      )
    );
    
    // Example: Retrieving launch parameters functionally
    pipe(
      retrieveLaunchParamsFp(), 
      E.match(
        error => {
          // Handle error
        },
        launchParams => {
          // Use the retrieved launch parameters
        }
      )
    );