x402-types
Core types for the x402 payment protocol.
This crate provides the foundational types used throughout the x402 ecosystem for implementing HTTP 402 Payment Required flows. It is designed to be blockchain-agnostic, with chain-specific implementations provided by separate crates.
Overview
The x402 protocol enables micropayments over HTTP by leveraging the 402 Payment Required status code. When a client requests a paid resource, the server responds with payment requirements. The client signs a payment authorization, which is verified and settled by a facilitator.
┌────────┐ ┌────────┐ ┌─────────────┐
│ Client │ ──1──▶ │ Server │ ──2──▶ │ Facilitator │
│ │ ◀──4── │ │ ◀──3── │ │
└────────┘ └────────┘ └─────────────┘
1. Request paid resource
2. Verify payment with facilitator
3. Payment valid / settled
4. Return resource (or 402 Payment Required)
Installation
Add to your Cargo.toml:
[]
= "0.1"
With telemetry support:
[]
= { = "0.1", = ["telemetry"] }
Modules
| Module | Description |
|---|---|
chain |
Blockchain identifiers and provider abstractions (CAIP-2 chain IDs) |
config |
Server configuration, CLI parsing, RPC config, and environment variable resolution |
facilitator |
Core trait for payment verification and settlement |
networks |
Registry of well-known blockchain networks |
proto |
Wire format types for protocol messages (V1 and V2) |
scheme |
Payment scheme system for extensible payment methods |
timestamp |
Unix timestamp utilities for payment authorization windows |
util |
Helper types (base64, string literals, money amounts) |
Protocol Versions
The crate supports two protocol versions:
- V1 (
proto::v1): x402 protocol v1 - V2 (
proto::v2): x402 protocol v2
Key Types
Chain Identifiers
use ChainId;
// Parse a CAIP-2 chain ID
let chain_id: ChainId = "eip155:8453".parse.unwrap;
assert_eq!;
assert_eq!;
// Convert from network name (V1 compatibility)
let chain_id = from_network_name.unwrap;
Payment Requirements (V2)
use ;
// Payment requirements returned in 402 response
let requirements = PaymentRequirements ;
Facilitator Trait
use Facilitator;
use ;
// Implement for your payment verification service
Timestamps
use UnixTimestamp;
// Create timestamp for payment validity window
let valid_after = now;
let valid_before = valid_after + from_secs;
Related Crates
| Crate | Description |
|---|---|
x402-chain-eip155 |
EVM chain support (Ethereum, Base, etc.) |
x402-chain-solana |
Solana blockchain support |
x402-chain-aptos |
Aptos blockchain support |
x402-axum |
Axum middleware for payment-gated endpoints |
x402-reqwest |
Reqwest client with automatic payment handling |
x402-facilitator-local |
Local facilitator implementation |
Feature Flags
| Feature | Description |
|---|---|
cli |
Enables CLI argument parsing via clap for configuration loading |
telemetry |
Enables tracing instrumentation for debugging and monitoring |
License
Apache-2.0