Integrate React app on Telegram mini-app
Integrate React app on Telegram mini-app
Overview#
Building a new app? Check out the Sample Telegram, which includes a basic Telegram <> Openfort integration.
Prerequisites#
We will be using the Openfort SDK to integrate the React app on Telegram mini-app. If you haven't set up the Openfort SDK in your project, follow the quickstart guide.
Configuration#
Telegram mini-app setup#
-
Create a Telegram bot and get the bot token. Follow the official guide to create a bot and get the token.
-
Configure your project providers Enable the Telegram provider and add the bot username and bot token.
Enable the Telegram (Mini-app) provider and add the bot token.
Frontend setup#
- In your frontend project, we will authenticate the user with the Telegram provider.
_10await openfort.authenticateWithThirdPartyProvider({_10 provider: ThirdPartyOAuthProvider.TELEGRAM_MINI_APP,_10 token: initDataRaw,_10 tokenType: TokenType.CUSTOM_TOKEN_10});
The token
is the initDataRaw
you receive from the Telegram SDK after initializing the Telegram mini-app.
In react, you would use @telegram-apps/sdk-react
to retrieve the initDataRaw
:
_23import openfort from "../utils/openfortConfig.ts";_23import { ThirdPartyOAuthProvider, TokenType } from "@openfort/openfort-js";_23import { retrieveLaunchParams } from "@telegram-apps/sdk-react";_23_23const SampleComponent = () => {_23 const { initDataRaw } = retrieveLaunchParams();_23_23 useEffect(() => {_23 if (initDataRaw) {_23 openfort.authenticateWithThirdPartyProvider({_23 provider: ThirdPartyOAuthProvider.TELEGRAM_MINI_APP,_23 token: initDataRaw,_23 tokenType: TokenType.CUSTOM_TOKEN_23 }).then(() => {_23 console.log("Authenticated!");_23 }).catch((error) => {_23 console.error("Failed to authenticate:", error);_23 })_23 }_23 }, [initDataRaw]);_23_23 // ... other code_23}
For more information on how to integrate the Telegram mini-app SDK, refer to the official documentation.
More information about external authentication can be found in the external auth guide.