Authorizing User
This article provides code examples in different programming languages on how a developer could authorize a user using Telegram init data.
Client
First of all, it is required to begin with the transmitting init data from the client side to the server. We could do it using this code:
import { retrieveLaunchParams } from '@telegram-apps/sdk';
const { initDataRaw } = retrieveLaunchParams();
fetch('https://example.com/api', {
method: 'POST',
headers: {
Authorization: `tma ${initDataRaw}`
},
});
We are sending a request to an imaginary server using the https://example.com/api
URL. This request uses the HTTP POST method (you can use whatever you want) and appends the Authorization
header, which is the most important here. It represents a string containing 2 parts divided by a space. The first one describes the authorization method (in this case, our server is going to support several others), and the second one contains authorization data. In the case of Telegram Mini Apps, the second part is raw init data.
Server
Now, when init data is transmitted to the server side, we should create a simple HTTP server which uses this data and authorizes the user.
Node.js
The Node.js example uses express to process HTTP requests.
import { validate, parse, type InitDataParsed } from '@telegram-apps/init-data-node';
import express, {
type ErrorRequestHandler,
type RequestHandler,
type Response,
} from 'express';
/**
* Sets init data in the specified Response object.
* @param res - Response object.
* @param initData - init data.
*/
function setInitData(res: Response, initData: InitDataParsed): void {
res.locals.initData = initData;
}
/**
* Extracts init data from the Response object.
* @param res - Response object.
* @returns Init data stored in the Response object. Can return undefined in case,
* the client is not authorized.
*/
function getInitData(res: Response): InitDataParsed | undefined {
return res.locals.initData;
}
/**
* Middleware which authorizes the external client.
* @param req - Request object.
* @param res - Response object.
* @param next - function to call the next middleware.
*/
const authMiddleware: RequestHandler = (req, res, next) => {
// We expect passing init data in the Authorization header in the following format:
// <auth-type> <auth-data>
// <auth-type> must be "tma", and <auth-data> is Telegram Mini Apps init data.
const [authType, authData = ''] = (req.header('authorization') || '').split(' ');
switch (authType) {
case 'tma':
try {
// Validate init data.
validate(authData, token, {
// We consider init data sign valid for 1 hour from their creation moment.
expiresIn: 3600,
});
// Parse init data. We will surely need it in the future.
setInitData(res, parse(authData));
return next();
} catch (e) {
return next(e);
}
// ... other authorization methods.
default:
return next(new Error('Unauthorized'));
}
};
/**
* Middleware which shows the user init data.
* @param _req
* @param res - Response object.
* @param next - function to call the next middleware.
*/
const showInitDataMiddleware: RequestHandler = (_req, res, next) => {
const initData = getInitData(res);
if (!initData) {
return next(new Error('Cant display init data as long as it was not found'));
}
res.json(initData);
};
/**
* Middleware which displays the user init data.
* @param err - handled error.
* @param _req
* @param res - Response object.
*/
const defaultErrorMiddleware: ErrorRequestHandler = (err, _req, res) => {
res.status(500).json({
error: err.message,
});
};
// Your secret bot token.
const token = '1234567890:ABC';
// Create an Express applet and start listening to port 3000.
const app = express();
app.use(authMiddleware);
app.get('/', showInitDataMiddleware);
app.use(defaultErrorMiddleware);
app.listen(3000);
// After the HTTP server was launched, try sending an HTTP GET request to the URL
// http://localhost:3000/ with an Authorization header containing data in the required format.
GoLang
The GoLang example uses gin to process HTTP requests.
package main
import (
"context"
"strings"
"time"
"github.com/gin-gonic/gin"
initdata "github.com/telegram-mini-apps/init-data-golang"
)
type contextKey string
const (
_initDataKey contextKey = "init-data"
)
// Returns new context with specified init data.
func withInitData(ctx context.Context, initData initdata.InitData) context.Context {
return context.WithValue(ctx, _initDataKey, initData)
}
// Returns the init data from the specified context.
func ctxInitData(ctx context.Context) (initdata.InitData, bool) {
initData, ok := ctx.Value(_initDataKey).(initdata.InitData)
return initData, ok
}
// Middleware which authorizes the external client.
func authMiddleware(token string) gin.HandlerFunc {
return func(context *gin.Context) {
// We expect passing init data in the Authorization header in the following format:
// <auth-type> <auth-data>
// <auth-type> must be "tma", and <auth-data> is Telegram Mini Apps init data.
authParts := strings.Split(context.GetHeader("authorization"), " ")
if len(authParts) != 2 {
context.AbortWithStatusJSON(401, map[string]any{
"message": "Unauthorized",
})
return
}
authType := authParts[0]
authData := authParts[1]
switch authType {
case "tma":
// Validate init data. We consider init data sign valid for 1 hour from their
// creation moment.
if err := initdata.Validate(authData, token, time.Hour); err != nil {
context.AbortWithStatusJSON(401, map[string]any{
"message": err.Error(),
})
return
}
// Parse init data. We will surely need it in the future.
initData, err := initdata.Parse(authData)
if err != nil {
context.AbortWithStatusJSON(500, map[string]any{
"message": err.Error(),
})
return
}
context.Request = context.Request.WithContext(
withInitData(context.Request.Context(), initData),
)
}
}
}
// Middleware which shows the user init data.
func showInitDataMiddleware(context *gin.Context) {
initData, ok := ctxInitData(context.Request.Context())
if !ok {
context.AbortWithStatusJSON(401, map[string]any{
"message": "Init data not found",
})
return
}
context.JSON(200, initData)
}
func main() {
// Your secret bot token.
token := "1234567890:ABC"
r := gin.New()
r.Use(authMiddleware(token))
r.GET("/", showInitDataMiddleware)
if err := r.Run(":3000"); err != nil {
panic(err)
}
}