T402 API Reference - v2.8.0
    Preparing search index...

    Function withT402

    • Wraps a Next.js App Router API route handler with t402 payment protection.

      Unlike paymentProxy which works as middleware, withT402 wraps individual route handlers and guarantees that payment settlement only occurs after the handler returns a successful response (status < 400). This provides more precise control over when payments are settled.

      Type Parameters

      • T = unknown

      Parameters

      • routeHandler: (request: NextRequest) => Promise<NextResponse<T>>

        The API route handler function to wrap

      • routeConfig: RouteConfig

        Payment configuration for this specific route

      • server: t402ResourceServer

        Pre-configured t402ResourceServer instance

      • OptionalpaywallConfig: PaywallConfig

        Optional configuration for the built-in paywall UI

      • Optionalpaywall: PaywallProvider

        Optional custom paywall provider (overrides default)

      • syncFacilitatorOnStart: boolean = true

        Whether to sync with the facilitator on startup (defaults to true)

      Returns (request: NextRequest) => Promise<NextResponse<T>>

      A wrapped Next.js route handler

      import { NextRequest, NextResponse } from "next/server";
      import { withT402 } from "@t402/next";
      import { t402ResourceServer } from "@t402/core/server";
      import { registerExactEvmScheme } from "@t402/evm/exact/server";

      const server = new t402ResourceServer(myFacilitatorClient);
      registerExactEvmScheme(server, {});

      const handler = async (request: NextRequest) => {
      return NextResponse.json({ data: "protected content" });
      };

      export const GET = withT402(
      handler,
      {
      accepts: {
      scheme: "exact",
      payTo: "0x123...",
      price: "$0.01",
      network: "eip155:84532",
      },
      description: "Access to protected API",
      },
      server,
      );