PayWeavePayWeaveBack to Home
Apps

Hono Middleware

The @payweave/hono package integrates with Hono's middleware system. The .charge() method returns a Hono-native middleware handler that works across all Hono runtimes - Cloudflare Workers, Bun, Deno, and Node.js.

Installation

Terminal
npm install @payweave/hono

Basic setup

TypeScript
import { Hono } from 'hono';
import { Payweave } from '@payweave/hono';

const app = new Hono();
const payweave = new Payweave(
  process.env.PAYWEAVE_APP_ID!,
  process.env.PAYWEAVE_APP_SECRET!
);

app.get(
  '/api/weather',
  payweave.charge({ price: '0.001', description: 'Weather API' }),
  (c) => c.json({ temp: 72, unit: 'F' })
);

export default app;

Route groups

Apply the same pricing to an entire group of routes by using Hono's app.use() with a path pattern:

TypeScript
// All routes under /api/v2/* require payment
app.use(
  '/api/v2/*',
  payweave.charge({ price: '0.003', description: 'v2 API access' })
);

app.get('/api/v2/users', (c) => c.json({ users: [] }));
app.get('/api/v2/posts', (c) => c.json({ posts: [] }));

Dynamic pricing

The price option accepts a function that receives the Hono context object, letting you price requests based on query parameters, headers, or body:

TypeScript
app.post(
  '/api/summarize',
  payweave.charge({
    price: async (c) => {
      const body = await c.req.json();
      return body.text.length > 5000 ? '0.01' : '0.002';
    },
    description: 'Text summarization',
  }),
  async (c) => {
    const body = await c.req.json();
    return c.json({ summary: '...' });
  }
);

Accessing payment data

After verification, payment details are available via c.get('payweave'):

TypeScript
app.get(
  '/api/data',
  payweave.charge({ price: '0.001', description: 'Data API' }),
  (c) => {
    const receipt = c.get('payweave');
    return c.json({ data: '...', txId: receipt.txId });
  }
);
Hono middleware runs on the edge by default. PayWeave verifies payments via Tempo with minimal latency, making it well-suited for edge-deployed APIs.