Management API Reference

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.

    Contract Info

    Enable the Telegram (Mini-app) provider and add the bot token.

    Contract Info

Frontend setup#

  • In your frontend project, we will authenticate the user with the Telegram provider.

_10
await 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:


_23
import openfort from "../utils/openfortConfig.ts";
_23
import { ThirdPartyOAuthProvider, TokenType } from "@openfort/openfort-js";
_23
import { retrieveLaunchParams } from "@telegram-apps/sdk-react";
_23
_23
const 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.