Skip to main content
Accept crypto payments in your Bun HTTP server with the Armory middleware.

Installation

# npm
npm install @armory-sh/middleware-bun

# yarn
yarn add @armory-sh/middleware-bun

# pnpm
pnpm add @armory-sh/middleware-bun

# bun
bun add @armory-sh/middleware-bun

Quick Start

import { createBunMiddleware } from '@armory-sh/middleware-bun';

const middleware = createBunMiddleware({
  payTo: '0xYourWalletAddress...',
  amount: '1.0'
});

Bun.serve({
  port: 3000,
  fetch: async (req) => {
    const result = await middleware(req);

    // If middleware returns a response, payment failed/required
    if (result) return result;

    // Payment verified — handle your request
    return new Response(JSON.stringify({ data: 'protected content' }));
  }
});

How It Works

The middleware returns:
  • null if payment is valid (your handler proceeds)
  • A Response object if payment is missing/invalid (returned to client)

Settlement Modes

Control how payments are settled after verification:
const middleware = createBunMiddleware({
  payTo: '0x...',
  amount: '1.0',
  settlementMode: 'verify'  // | 'settle' | 'async'
});
ModeDescription
verifyVerify only, don’t settle (great for testing)
settleVerify and settle on-chain before responding
asyncReturn immediately, settle in background

Configuration

const middleware = createBunMiddleware({
  payTo: '0x...',
  amount: '1.0',
  settlementMode: 'verify'
});

Route Configuration

For per-route behavior, use createRouteAwareBunMiddleware:
import { createRouteAwareBunMiddleware } from '@armory-sh/middleware-bun';

const middleware = createRouteAwareBunMiddleware({
  routes: ['/api/basic', '/api/premium/*'],
  payTo: '0xYourAddress...',
  amount: '$1.00',
  network: 'base',
  perRoute: {
    '/api/premium/*': {
      amount: '$5.00'
    }
  }
});

Tips

Use settlementMode: 'verify' during development to skip on-chain settlement while still testing payment verification.
The amount can be a human-readable string like "1.0" — no manual decimal calculation needed.
With settlementMode: 'settle', responses will be delayed until the on-chain transaction completes. Use async for better UX.

More Information

See @armory-sh/middleware-bun for full configuration options.