npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@rustigram/tma-server

v0.10.0

Published

Server-side initData validation for Telegram Mini Apps

Readme

@rustigram/tma-server

Server-side initData validation for Telegram Mini Apps. Uses the Web Crypto API (globalThis.crypto.subtle) exclusively — runs in Node 22+, Deno, Cloudflare Workers, and any edge runtime without polyfills.

pnpm add @rustigram/tma-server @rustigram/tma-core

Validation

Two validation strategies — pick based on what you have:

| Strategy | Requires | Use when | | ----------- | ------------------------- | --------------------------------------- | | HMAC-SHA256 | Bot token | You control the bot | | Ed25519 | Bot ID only | Third-party / no bot token | | Parse-only | None (upstream validated) | Behind rustigram-miniapp Rust gateway |

HMAC-SHA256

import { validateInitData } from "@rustigram/tma-server";

const result = await validateInitData(initDataString, process.env.BOT_TOKEN!, {
  maxAgeSeconds: 3600, // reject sessions older than 1 hour
});

if (!result.ok) {
  // result.error: "invalid_hash" | "expired" | "parse_error"
  return new Response("Unauthorized", { status: 401 });
}

const { user, auth_date, query_id } = result.data; // result.data: WebAppInitData

Ed25519 (third-party)

import { validateInitDataSignature } from "@rustigram/tma-server";

const result = await validateInitDataSignature(initDataString, botId, {
  env: "production", // "production" | "test"
  maxAgeSeconds: 3600,
});

Public keys are hardcoded — Telegram's official published keys:

  • Production: e7bf03a2fa4602af4580703d88dda5bb59f32ed8b02a56c187fe7d34caed242d
  • Test: 40055058a4ee38156a06562e52eece92a771bcd8346a8c4615cb7376eddf72ec

SolidStart v2 Middleware

// src/middleware.ts
import { createMiddleware } from "@solidjs/start/middleware";
import { createTmaMiddleware } from "@rustigram/tma-server/solidstart";

export default createMiddleware({
  onRequest: process.env["BOT_TOKEN"] ? [createTmaMiddleware(process.env["BOT_TOKEN"])] : [], // skip validation in local dev when token not set
});

Register in vite.config.ts:

solidStart({ ssr: false, middleware: "./src/middleware.ts" });

Client-side — send initData with every request:

const { bridge } = useTma();

fetch("/api/endpoint", {
  headers: { "X-Telegram-Init-Data": bridge.webApp.initData },
});

Custom header or expiry:

createTmaMiddleware(botToken, {
  headerName: "X-My-Init-Data", // default: "X-Telegram-Init-Data"
  maxAgeSeconds: 1800,
});

Reading Context in Server Functions

import { getTmaUser, getTmaInitData } from "@rustigram/tma-server/solidstart";
import { getRequestEvent } from "solid-js/web";

async function getUser() {
  "use server";
  const event = getRequestEvent();
  return getTmaUser(event!); // WebAppUser — throws if middleware not active
}

async function getInitData() {
  "use server";
  const event = getRequestEvent();
  return getTmaInitData(event!); // WebAppInitData
}

Both throw with a descriptive message if the middleware didn't run or validation failed.

Behind a Rust Gateway

When rustigram-miniapp acts as the gateway, pass null as the bot token. SolidStart parses without re-validating — no BOT_TOKEN needed in the TypeScript environment.

// src/middleware.ts
import { createMiddleware } from "@solidjs/start/middleware";
import { createTmaMiddleware } from "@rustigram/tma-server/solidstart";

export default createMiddleware({
  onRequest: [
    createTmaMiddleware(null, {
      gatewaySecret: process.env.GATEWAY_SECRET!, // verifies X-Tma-Gateway header
    }),
  ],
});

Without gatewaySecret, trust is network-level only (safe in VPC/private network). With it, SolidStart cryptographically verifies the request passed through the Rust gateway.

Parsing without middleware

import { parseInitData } from "@rustigram/tma-server";

// When upstream already validated:
const result = parseInitData(rawInitData);
if (result.ok) {
  console.log(result.data.user);
}

Framework-agnostic (Hono, Express, etc.)

validateInitData is a plain async function with no framework coupling:

import { validateInitData } from "@rustigram/tma-server";

// Hono
app.use("/api/*", async (c, next) => {
  const initData = c.req.header("X-Telegram-Init-Data") ?? "";
  const result = await validateInitData(initData, process.env.BOT_TOKEN!);
  if (!result.ok) return c.json({ error: result.error }, 401);
  c.set("tmaUser", result.data.user);
  await next();
});

Requirements

  • Node.js ≥ 22.12, Deno ≥ 1.40, or any runtime with globalThis.crypto.subtle
  • @rustigram/tma-core (peer dependency — for shared types)