Start here: need to register a service and create a plan first? Follow the
5-minute setup.
@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:
- A human-readable text block explaining the payment requirement
- A structured JSON block containing the full
PaymentRequiredobject
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.
- Returns a
PaymentRequirederror 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 thePaymentRequired 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, thePaymentContext 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 toolsdemo.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