Next.js Integration

Installation

npm install @soldidev/middleware

Basic Setup

Create middleware.ts in your project root:

import { createSoldi } from "@soldidev/middleware/next";

export const middleware = createSoldi({
  apiKey: process.env.SOLDI_API_KEY!,
  defaultPrice: 0.005,
  routes: {
    "/blog/*": { price: 0.005, description: "Blog post" },
    "/premium/*": { price: 0.02, description: "Premium content" },
    "/api/data/*": { price: 0.01, description: "Data API" },
  },
});

export const config = {
  matcher: ["/blog/:path*", "/premium/:path*", "/api/data/:path*"],
};

Environment Variables

# .env.local
SOLDI_API_KEY=sk_live_your_key_here
# Optional: override API URL (defaults to https://api.soldi.dev)
SOLDI_API_URL=https://api.soldi.dev

Route Configuration

Use glob patterns to match routes:

  • /blog/* — matches /blog/my-post but not /blog/deep/nested
  • /blog/** — matches any depth: /blog/a/b/c
  • /api/data/* — matches /api/data/users

Behavior

  • Human visitors: pass through untouched. They never see a paywall.
  • AI agents: receive a 402 Payment Required with payment instructions.
  • Agents with payment: content is served after payment verification.
  • API unreachable: middleware fails open — serves content (never blocks visitors).