Skip to main content
Start here: need to register a service and create a plan first? Follow the 5-minute setup.
Add x402 payment protection to your Express.js API. The paymentMiddleware handles verification and settlement automatically.

Installation

Quick Start: One-Line Payment Protection

The paymentMiddleware from @nevermined-io/payments/express handles the entire x402 flow:
That’s it! The middleware automatically:
  • Returns 402 with payment-required header when no token is provided
  • Verifies the x402 token via the Nevermined facilitator
  • Burns credits after request completion
  • Returns payment-response header with settlement receipt

x402 Headers

The middleware follows the x402 HTTP transport spec:

Route Configuration

Fixed Credits

Dynamic Credits

Calculate credits based on request/response:

Path Parameters

With Agent ID

Payment Scheme (Crypto vs. Fiat)

The middleware auto-detects the payment scheme from plan metadata. Plans with fiat pricing (isCrypto: false) automatically use nvm:card-delegation (Stripe), while crypto plans use nvm:erc4337. You can explicitly override the scheme in the route configuration:
The middleware automatically detects the payment scheme from plan metadata. Plans with fiat pricing (isCrypto: false) use nvm:card-delegation (Stripe). No code changes are needed on the agent side. You can explicitly override with the scheme parameter.
For the full card-delegation specification, see the Card Delegation Spec.

Middleware Options

Complete Example

See the complete working example in the http-simple-agent tutorial on GitHub.

Client Implementation

For fiat plans, clients can use resolveScheme() to auto-detect the payment scheme before generating tokens. See the x402 developer guide for details on scheme resolution and X402TokenOptions.
Here’s how clients interact with your payment-protected API:

Environment Variables

Next Steps

Payment Patterns

Advanced credit charging patterns

x402 Protocol

Deep dive into x402 payment flows

Payment Models

Configure credits, subscriptions, and dynamic pricing

http-simple-agent Tutorial

Complete working example on GitHub