Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions typescript/.changeset/funny-masks-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@coinbase/agentkit": patch
---

Updated x402 actions to v2 and improved discovery and query/body parameter handling
6 changes: 3 additions & 3 deletions typescript/agentkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@
"@solana/spl-token": "^0.4.12",
"@solana/web3.js": "^1.98.1",
"@vaultsfyi/sdk": "^2.1.9",
"@x402/evm": "^2.0.0",
"@x402/svm": "^2.0.0",
"@x402/fetch": "^2.0.0",
"@zerodev/ecdsa-validator": "^5.4.5",
"@zerodev/intent": "^0.0.24",
"@zerodev/sdk": "^5.4.28",
"@zoralabs/coins-sdk": "0.2.8",
"@zoralabs/protocol-deployments": "0.6.1",
"axios": "^1.9.0",
"bs58": "^4.0.1",
"canonicalize": "^2.1.0",
"clanker-sdk": "^4.1.18",
Expand All @@ -71,8 +73,6 @@
"sushi": "6.2.1",
"twitter-api-v2": "^1.18.2",
"viem": "2.38.3",
"x402": "^0.6.0",
"x402-axios": "^0.6.0",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
91 changes: 64 additions & 27 deletions typescript/agentkit/src/action-providers/x402/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This directory contains the **X402ActionProvider** implementation, which provide
x402/
├── x402ActionProvider.ts # Main provider with x402 payment functionality
├── schemas.ts # x402 action schemas
├── constants.ts # Network mappings and type definitions
├── index.ts # Main exports
├── utils.ts # Utility functions
└── README.md # This file
Expand All @@ -20,15 +21,17 @@ x402/
1. `make_http_request`: Make initial HTTP request and handle 402 responses
2. `retry_http_request_with_x402`: Retry a request with payment after receiving payment details

### Alternative Action
### Alternative Actions

- `make_http_request_with_x402`: Direct payment-enabled requests (skips confirmation flow)
- `discover_x402_services`: Discover available x402 services (optionally filter by asset and price)
- `discover_x402_services`: Discover available x402 services (optionally filter by price)

## Overview

The x402 protocol enables APIs to require micropayments for access. When a client makes a request to a protected endpoint, the server responds with a `402 Payment Required` status code along with payment instructions.

This provider supports **both v1 and v2 x402 endpoints** automatically.

### Recommended Two-Step Flow

1. Initial Request:
Expand Down Expand Up @@ -64,21 +67,33 @@ Makes initial request and handles 402 responses:

### `retry_http_request_with_x402` Action

Retries request with payment after 402:
Retries request with payment after 402. Supports both v1 and v2 payment option formats:

```typescript
// v1 format (legacy endpoints)
{
url: "https://api.example.com/data",
method: "GET", // Optional, defaults to GET
headers: { "Accept": "..." }, // Optional
body: { ... }, // Optional
selectedPaymentOption: { // Payment details from 402 response
method: "GET",
selectedPaymentOption: {
scheme: "exact",
network: "base-sepolia",
network: "base-sepolia", // v1 network identifier
maxAmountRequired: "1000",
asset: "0x..."
}
}

// v2 format (CAIP-2 network identifiers)
{
url: "https://api.example.com/data",
method: "GET",
selectedPaymentOption: {
scheme: "exact",
network: "eip155:84532", // v2 CAIP-2 identifier
amount: "1000",
asset: "0x...",
payTo: "0x..."
}
}
```

### `make_http_request_with_x402` Action
Expand All @@ -96,22 +111,33 @@ Direct payment-enabled requests (use with caution):

### `discover_x402_services` Action

Fetches available services and optionally filters them by maximum price in USDC whole units. The action defaults to USDC on the current network:
Fetches all available services from the x402 Bazaar with full pagination support. Returns simplified output with url, price, and description for each service.

```typescript
{
maxUsdcPrice: 0.1 // optional (e.g., 0.1 for $0.10 USDC)
facilitator: "cdp", // Optional: 'cdp', 'payai', or custom URL
maxUsdcPrice: 0.1, // Optional, filter by max price in USDC
keyword: "weather", // Optional, filter by description/URL keyword
x402Versions: [1, 2] // Optional, filter by protocol version
}
```

Example filtering for USDC services under $0.10:

```ts
const maxUsdcPrice = 0.1;

const services = await discover_x402_services({ maxUsdcPrice });

Example response:

```json
{
"success": true,
"walletNetworks": ["base-sepolia", "eip155:84532"],
"total": 150,
"returned": 25,
"services": [
{
"url": "https://api.example.com/weather",
"price": "0.001 USDC on base-sepolia",
"description": "Get current weather data"
}
]
}
```

## Response Format
Expand All @@ -132,21 +158,32 @@ Successful responses include payment proof when payment was made:

## Network Support

The x402 provider currently supports the following networks:
- `base-mainnet`
- `base-sepolia`
- `solana-mainnet`
- `solana-devnet`
The x402 provider supports the following networks:

| Internal Network ID | v1 Identifier | v2 CAIP-2 Identifier |
|---------------------|---------------|----------------------|
| `base-mainnet` | `base` | `eip155:8453` |
| `base-sepolia` | `base-sepolia` | `eip155:84532` |
| `solana-mainnet` | `solana` | `solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp` |
| `solana-devnet` | `solana-devnet` | `solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1` |

The provider supports both EVM and SVM (Solana) wallets for signing payment transactions.

## v1/v2 Compatibility

This provider automatically handles both v1 and v2 x402 endpoints:

The provider requires EVM-compatible networks where the wallet can sign payment transactions.
- **Discovery**: Filters resources matching either v1 or v2 network identifiers
- **Payment**: The `@x402/fetch` library handles protocol version detection automatically
- **Headers**: Supports both `X-PAYMENT-RESPONSE` (v1) and `PAYMENT-RESPONSE` (v2) headers

## Dependencies

This action provider requires:
- `axios` - For making HTTP requests
- `x402-axios` - For handling x402 payment flows
- `x402` - For payment requirement types and validation
- `@x402/fetch` - For handling x402 payment flows
- `@x402/evm` - For EVM payment scheme support
- `@x402/svm` - For Solana payment scheme support

## Notes

For more information on the **x402 protocol**, visit the [x402 documentation](https://x402.gitbook.io/x402/).
For more information on the **x402 protocol**, visit the [x402 documentation](https://docs.cdp.coinbase.com/x402/overview).
88 changes: 88 additions & 0 deletions typescript/agentkit/src/action-providers/x402/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Known facilitator registry
*/
export const KNOWN_FACILITATORS = {
cdp: "https://api.cdp.coinbase.com/platform/v2/x402",
payai: "https://facilitator.payai.network",
} as const;

export type KnownFacilitatorName = keyof typeof KNOWN_FACILITATORS;

export const DEFAULT_FACILITATOR: KnownFacilitatorName = "cdp";

/**
* Supported networks for x402 payment protocol
*/
export const SUPPORTED_NETWORKS = [
"base-mainnet",
"base-sepolia",
"solana-mainnet",
"solana-devnet",
] as const;

/**
* USDC token addresses for Solana networks
*/
export const SOLANA_USDC_ADDRESSES = {
"solana-devnet": "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
"solana-mainnet": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
} as const;

/**
* Network mapping from internal network ID to both v1 and v2 (CAIP-2) formats.
* Used for filtering discovery results that may contain either format.
*/
export const NETWORK_MAPPINGS: Record<string, string[]> = {
"base-mainnet": ["base", "eip155:8453"],
"base-sepolia": ["base-sepolia", "eip155:84532"],
"solana-mainnet": ["solana", "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"],
"solana-devnet": ["solana-devnet", "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1"],
};

/**
* x402 protocol version type
*/
export type X402Version = 1 | 2;

/**
* Payment option from discovery API (supports both v1 and v2 formats)
*/
export interface PaymentOption {
scheme: string;
network: string;
asset: string;
// v1 format
maxAmountRequired?: string;
// v2 format
amount?: string;
price?: string;
payTo?: string;
description?: string;
}

/**
* Resource from discovery API
*/
export interface DiscoveryResource {
url?: string;
resource?: string;
type?: string;
metadata?: {
[key: string]: unknown;
description?: string;
input?: Record<string, unknown>;
output?: Record<string, unknown>;
};
accepts?: PaymentOption[];
x402Version?: number;
lastUpdated?: string;
}

/**
* Simplified resource output for LLM consumption
*/
export interface SimplifiedResource {
url: string;
price: string;
description: string;
}
Loading
Loading