Skip to main content
Start here: need to register a service and create a plan first? Follow the 5-minute setup.
Add payment protection to your Strands AI agent tools using the x402 protocol. The @requires_payment decorator handles verification and settlement automatically.

x402 Payment Flow

How Payment Errors Flow

The @requires_payment decorator follows the x402 MCP transport spec — payment errors are returned as tool results with status: "error", not raised as exceptions. Each error includes:
  1. A human-readable text block explaining the payment requirement
  2. A structured JSON block containing the full PaymentRequired object
In Strands, tool errors flow through the LLM. The LLM sees the error and relays it to the user in natural language (e.g., “I need a payment token to use this tool”). The structured PaymentRequired data is preserved in agent.messages. Clients use extract_payment_required(agent.messages) to get the structured PaymentRequired dict from the conversation history. The PaymentRequired contains the accepts array with plan IDs, schemes, and networks needed to acquire an x402 access token.

Installation

The [strands] extra installs the Strands SDK dependency required for the decorator.

Quick Start: Protecting a Tool

The @requires_payment decorator wraps a Strands @tool function with x402 payment verification and settlement.
You must use @tool(context=True) instead of plain @tool. This tells Strands to inject tool_context into the function, which the decorator needs to access invocation_state for the payment token.
That’s it! The decorator automatically:
  • Returns a PaymentRequired error when no token is provided
  • Verifies the x402 token via the Nevermined facilitator
  • Executes the tool function on successful verification
  • Burns credits after successful execution

Client-Side: Payment Discovery

Clients discover payment requirements by calling the agent without a token, then extracting the PaymentRequired from the conversation history:

Decorator Configuration

The decorator 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.

Single Plan

Multiple Plans

Accept multiple payment plans (e.g., basic and premium tiers):

Dynamic Credits

Calculate credits based on tool arguments:

With Agent ID

Scheme and Network

Lifecycle Hooks

Accessing Payment Context

After verification, the PaymentContext is available in tool_context.invocation_state["payment_context"]:

Complete Example

See the complete working example in the strands-simple-agent directory on GitHub. It includes:
  • agent.py — Agent with payment-protected tools
  • demo.py — Full payment discovery and token acquisition flow

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

strands-simple-agent

Complete working example on GitHub