Telegram Mini-Apps are powerful tools for developers to reach over 950 million monthly active users directly within the Telegram messenger. These lightweight applications, built using web technologies like HTML, CSS, and JavaScript, provide a seamless, integrated experience without requiring separate downloads or installations.
In this guide, you'll learn how to create a Telegram Mini-App integrated with Openfort's SDK to enable Web3 features like non-custodial wallets and NFT minting. This article is optimized for technical readers looking for a practical starting point, and you'll find a fully functional sample repository linked below.
The repository for this tutorial can be found here.
Test it live via Telegram by chatting with @OpenfortMiniAppBot.
Step 0: Overview and Architecture
Before diving into the implementation, it’s important to understand the high-level structure and flow of the Telegram Mini-App you’ll build.
Key Components
-
Frontend (Client-Side):
- Built with React using the Telegram Web Apps SDK.
- Handles the user interface and state management.
- Processes
initData
received from Telegram for authentication. - Provides interactions for minting NFTs and viewing wallet information.
-
Backend (Server-Side):
- Built with Node.js and Express.
- Validates
initData
for security and ensures the user is authenticated. - Integrates with Openfort’s SDK to manage smart wallets and sponsored transactions.
- Exposes API endpoints for NFT minting and wallet generation.
Telegram Authentication Flows
Telegram provides two distinct methods for authentication within Mini-Apps:
-
initData
Flow:- Used within the Mini-App environment.
- Relies on Telegram to authenticate the user via the
initData
object. - No refresh token is involved, as Telegram handles re-authentication for subsequent sessions.
-
OAuth Flow:
- More versatile and works in browsers or platforms outside the Mini-App context.
- Returns an access token/refresh token pair, similar to logging in with Google.
- Suitable for broader integrations where the user isn't already within the Telegram app.
This guide focuses on the initData
flow, which is specially designed for seamless use in Telegram Mini-Apps.
High-Level Flow
Here’s how the application will work end-to-end:
- The user opens the Telegram Mini-App from a bot.
- Telegram sends
initData
to the Mini-App containing signed user information. - The frontend sends this
initData
to the backend for validation. - The backend:
- Validates
initData
using Telegram-provided cryptographic methods. - Creates a self-custodial wallet using Openfort.
- Returns relevant user data and wallet information to the frontend.
- Validates
- The user interacts with the Mini-App:
- Minting NFTs.
- Viewing wallet details.
- Executing blockchain transactions.
Objectives
By the end of this tutorial, you will have:
- Set up a Telegram Mini-App using React.
- Integrated Openfort’s SDK for embedded wallet creation.
- Built backend routes to handle Telegram authentication and Web3 interactions.
Step 1: Set Up the Telegram Mini-App
1.1 Create a Bot and Mini-App with BotFather
- Add BotFather to your Telegram and run
/newbot
. - Follow the prompts to name your bot and get its access token.
- Add this token to the Openfort authentication provider settings.
- Run
/newapp
to create a Mini-App linked to your bot. Use your ngrok URL as the app’s URL (we’ll set this up next).
Step 2: Clone and Configure the Project
2.1 Clone the Repository
_10git clone https://github.com/openfort-xyz/sample-telegram-mini-app-embedded-wallet_10cd sample-telegram-mini-app-embedded-wallet
2.2 Set Up Environment Variables
In the /frontend
and /backend
directories, copy and configure .env.example
files:
_10cp .env.example .env
-
Frontend
.env
:_10NEXT_PUBLIC_TELEGRAM_BOT_TOKEN=<your-bot-token>_10NEXT_PUBLIC_OPENFORT_PUBLISHABLE_KEY=<your-publishable-key>_10NEXT_PUBLIC_OPENFORT_POLICY_ID=<your-policy-id>_10NEXT_PUBLIC_OPENFORT_CHAIN_ID=<your-chain-id> -
Backend
.env
:_10BOT_TOKEN=<your-bot-token>_10FRONTEND_APP_ORIGIN=<your-ngrok-url>
Step 3: Run the Application
3.1 Install Dependencies
_10# In frontend directory_10cd frontend && yarn install_10_10# In backend directory_10cd ../backend && yarn install
3.2 Start Services
_10# Start frontend_10yarn start_10_10# Start backend_10yarn dev
Use ngrok
to expose your local server:
_10ngrok http 3000
Update the Mini-App URL in BotFather with your ngrok HTTPS link.
Step 4: Key Integration Features
Telegram Authentication
The app uses initDataRaw
from the Telegram Web Apps SDK for third-party authentication. No additional refresh tokens are needed.
Embedded Wallet
With Openfort’s SDK, users can:
- Create a non-custodial wallet.
- Mint NFTs using predefined policies.
API Routes
The backend provides endpoints for:
/api/createAuthConfig
: Configures authentication./api/createEncryptionSession
: Sets up a session for wallet creation./api/mintNft
: Facilitates NFT minting.
Example Code Snippets
Telegram Bot Integration
In /backend/src/bot/features/openfort.ts
:
_10feature.command('start', async (ctx) => {_10 const keyboard = new InlineKeyboard().webApp(_10 'Launch App',_10 `${process.env.FRONTEND_APP_ORIGIN}/login/telegram`,_10 )_10 return ctx.reply('Click to launch.', { reply_markup: keyboard })_10})
Wallet Initialization
In /frontend/src/app/login/telegram/page.tsx
:
_10useEffect(() => {_10 const initTelegram = async () => {_10 const telegramSDK = Telegram.WebApp;_10 telegramSDK.ready();_10 const wallet = await openfort.createWallet({ user: telegramSDK.initData });_10 setWallet(wallet);_10 };_10 initTelegram();_10}, []);
Conclusion
You’ve built a Telegram Mini-App with React that:
- Authenticates users via the Telegram SDK.
- Generates EVM-compatible non-custodial wallets.
- Facilitates Web3 interactions, such as NFT minting.
For further details, check: