Skip to content
Tollgate Docs

Express / Hono adapters

Tollgate ships @tollgatepay/next as the first-class adapter. If you're on Express or Hono, the shared primitives in @tollgatepay/core let you wire up a middleware in ~50 lines. Reference implementations below; track the issue tracker for published @tollgatepay/express and @tollgatepay/hono packages.

Express

packages/express/src/index.ts
// packages/express/src/index.ts — one implementation reference.
import { verifyIOU, make402Response, type TollgateConfig } from "@tollgatepay/core";
import type { Request, Response, NextFunction } from "express";

export function tollgateMiddleware(config: TollgateConfig & { price: number }) {
  return async (req: Request, res: Response, next: NextFunction) => {
    const iouHeader = req.headers["x-tollgate-iou"];
    if (!iouHeader) {
      return res.status(402).json(make402Response(config));
    }
    const result = await verifyIOU(iouHeader as string, config);
    if (!result.valid) return res.status(403).json({ error: result.error });
    (req as Request & { payment?: typeof result.payment }).payment = result.payment;
    next();
  };
}

Hono

packages/hono/src/index.ts
// packages/hono/src/index.ts — one implementation reference.
import type { MiddlewareHandler } from "hono";
import { verifyIOU, make402Response, type TollgateConfig } from "@tollgatepay/core";

export function tollgate(config: TollgateConfig & { price: number }): MiddlewareHandler {
  return async (c, next) => {
    const iouHeader = c.req.header("x-tollgate-iou");
    if (!iouHeader) return c.json(make402Response(config), 402);
    const result = await verifyIOU(iouHeader, config);
    if (!result.valid) return c.json({ error: result.error }, 403);
    c.set("payment", result.payment);
    await next();
  };
}

Other frameworks

Any framework with a Web-standard Request/ Response bridge (Fastify with @fastify/middie, Astro API routes, Deno serve) can import from @tollgatepay/core directly — the primitives are runtime-agnostic.