Telegram Mini-Apps have emerged as a powerful tool 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, offer a seamless and integrated user experience without the need for separate downloads or installations.
The recent milestone of the TON blockchain surpassing 1 billion transactions, with half of this volume recorded in just the last three months, underscores the growing popularity and potential of Telegram Mini-Apps. This rapid growth is driven by innovative applications across various categories, including games, management tools, and utilities.
Key Benefits of Telegram Mini-Apps:
- Instant access: Users can engage with Mini-Apps directly from a Telegram chat, bypassing traditional app distribution challenges.
- Seamless integration: The familiar Telegram interface reduces friction and enhances user engagement.
- Bot interaction: Leverage Telegram Bots for automated experiences and interactive features.
- Massive user base: Tap into Telegram's existing pool of 950 million monthly active users.
- Monetization opportunities: Explore various revenue streams through in-app purchases, subscriptions, or advertising on the TON blockchain.
How to build a Telegram mini-app with Openfort
In this tutorial, we'll walk through the process of building a web3 Telegram mini app using Openfort's SDK. The full mini-app repository can be found here.
You can also test it out in your Telegram app by chatting with "@OpenfortMiniAppBot".
By the end of this guide, you will have a fully functioning web3 Telegram mini app that allows users to:
- Authenticate using their Telegram account
- Generate a non-custodial smart wallet
- Mint NFTs directly from Telegram
- View their wallet information
Note: There are two methods of authentication with telegram. Telegram auth requires the botUsername as an OAuth2 authentication flow with telegram, similarly to how you login with Google with an access/refresh token returned. This method goes through the screen you have seen with the Continue with Telegram button.
The other method, telegramMiniApp, uses the signed initDataRaw from the telegramSDK available in the mini app as third party authentication. It does not have any kind of refresh token since telegram is in charge of issuing new authentications. This method can be used directly from the mini app without requiring interaction with the user as he is already authenticated with the mini app.
So the telegram method can be used for example in a normal browser like firefox, chrome or unity or another platform. The telegramMiniApp method is specially designed to work in mini apps an it's the one used in this guide.
Prerequisites
- Openfort account
- Telegram account
- Node.js and yarn installed
- ngrok or similar tunneling service for local development
Step 1: Create a Telegram Mini App with BotFather
- Add BotFather to your Telegram
- Enter the command
/newbot
(new bot must be created first before creating an app) - Choose and enter a name and username for your bot
- You will be given an access token - go to Openfort authentication providers and add the
bot_token
in the Telegram Mini-app section. - Create a new mini app with the command
/newapp
- Select your bot and follow the prompts for app name, description, and photo
- When asked for the URL, you'll need your ngrok URL (we'll set this up later)
Step 2: Project Setup
- Clone the repository:
_10git clone https://github.com/openfort-xyz/sample-telegram-mini-app-embedded-wallet_10cd sample-telegram-mini-app-embedded-wallet
- Create environment files:
_10# In /next-app directory_10cp .env.example .env_10_10# In /telegram-bot directory_10cp .env.example .env
- Configure environment variables:
In /next-app/.env
:
_11NEXT_PUBLIC_TELEGRAM_BOT_TOKEN=<your-bot-token>_11_11NEXT_PUBLIC_OPENFORT_PUBLISHABLE_KEY=<from-openfort-dashboard>_11NEXT_PUBLIC_OPENFORT_SHIELD_PUBLISHABLE_KEY=<from-openfort-dashboard>_11NEXT_PUBLIC_OPENFORT_POLICY_ID=<your-policy-id>_11NEXT_PUBLIC_OPENFORT_CHAIN_ID=<your-chain-id>_11NEXT_PUBLIC_OPENFORT_CONTRACT_ID=<your-contract-id>_11_11OPENFORT_SECRET_KEY=<from-openfort-dashboard>_11OPENFORT_SHIELD_SECRET_KEY=<from-openfort-dashboard>_11OPENFORT_SHIELD_ENCRYPTION_SHARE=<from-openfort-dashboard>
In /telegram-bot/.env
:
_10BOT_TOKEN=<your-bot-token>_10FRONTEND_APP_ORIGIN=<your-ngrok-url>
Step 3: Set Up Public URL
- Install ngrok:
npm install -g ngrok
- Start ngrok:
ngrok http 3000
- Copy the HTTPS URL provided by ngrok
- Update your Mini App URL in BotFather with this URL
- Add the URL to
FRONTEND_APP_ORIGIN
in/telegram-bot/.env
Step 4: Configure Openfort
- Create an account on Openfort Dashboard
- Create a new application
- Get your API keys (Standard and Shield)
- Create a new Asset and Policy
- Add all IDs and keys to your environment variables
Step 5: Install Dependencies and Run
- Install dependencies:
_10# In /next-app directory_10cd next-app && yarn install_10_10# In /telegram-bot directory_10cd ../telegram-bot && yarn install
- Start both services:
_10# In /next-app directory_10yarn dev_10_10# In /telegram-bot directory (new terminal)_10yarn dev
Step 6: Test the Integration
- Open your Telegram bot
- Send the
/start
command - Click the "Launch App" button
- The mini app should open and:
- Authenticate you using your Telegram account
- Generate a non-custodial wallet
- Allow you to mint an NFT
Key Components
Telegram Bot
The bot is configured in /telegram-bot/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})
API Routes
The Next.js app includes three main API routes:
/api/createAuthConfig
- Configures Telegram Mini App authentication/api/createEncryptionSession
- Creates encryption session for non-custodial wallet/api/mintNft
- Handles NFT minting
Main App Page
The main app logic is in /next-app/src/app/login/telegram/page.tsx
, handling:
- Telegram authentication
- Wallet configuration
- NFT minting interface
For more detailed information, refer to the repository README.
Conclusion
You now have a working Telegram mini app that:
- Authenticates users via Telegram
- Creates self-custodial smart wallets
- Allows users to mint NFTs
- Handles sponsored transactions securely through Openfort's infrastructure
The mini app provides a seamless web3 experience without requiring users to leave Telegram, making it more accessible to mainstream users.
For more information and advanced features, refer to: