Hono

Learn how to accept your first payment using Hono and the Sangria TypeScript SDK.

Prerequisites

To get the most out of this guide, you'll need to:

1. Install

Install the Sangria SDK in your Hono project.

npm install @sangria-sdk/core

2. Initialize the client

Create a Sangria instance with your API key.

import { Sangria } from "@sangria-sdk/core";

const sangria = new Sangria({
  apiKey: process.env.SANGRIA_SECRET_KEY,
});

3. Protect a route

Import the Hono adapter and add fixedPrice as middleware to any route.

import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { Sangria } from "@sangria-sdk/core";
import { fixedPrice } from "@sangria-sdk/core/hono";

const app = new Hono();

const sangria = new Sangria({
  apiKey: process.env.SANGRIA_SECRET_KEY,
});

// This endpoint is free
app.get("/", (c) => {
  return c.json({ message: "Hello! This endpoint is free." });
});

// This endpoint costs $0.01 per request
app.get(
  "/premium",
  fixedPrice(sangria, {
    price: 0.01,
    description: "Access premium content",
  }),
  (c) => {
    return c.json({ message: "You accessed the premium endpoint!" });
  }
);

serve({ fetch: app.fetch, port: 4003 });

4. Run it

Start your server:

SANGRIA_SECRET_KEY=sg_live_xxx node server.js

When an agent calls GET /premium:

  1. First request (no payment) — the SDK returns 402 Payment Required with the price and payment details
  2. Agent signs the USDC payment off-chain
  3. Retry with payment — the agent sends the signed payment in a payment-signature header
  4. SDK settles the payment on-chain and your handler runs

Try it yourself

# This will return a 402 with payment requirements
curl http://localhost:4003/premium

On this page

⭐️