|
1 | 1 | --- |
2 | 2 | title: Agentic payments with x402 protocol |
3 | 3 | sidebar_label: x402 |
4 | | -description: Use the x402 protocol to enable AI agents to autonomously pay for Apify Actor runs using USDC on the Base blockchain. |
| 4 | +description: Use the x402 protocol to enable AI agents to autonomously pay for Apify Actor runs using USDC on the Base blockchain - no Apify account required. |
5 | 5 | sidebar_position: 19 |
6 | 6 | slug: /integrations/x402 |
7 | 7 | --- |
8 | 8 |
|
9 | | -Agentic payments enable AI agents to autonomously run Apify Actors using third-party payment providers, without requiring traditional Apify user accounts. This allows agents to discover, execute, and pay for web scraping and automation tasks independently. |
10 | | - |
11 | | -Apify supports agentic payments through the [x402 protocol](https://www.x402.org/), an open standard for internet-native payments. |
| 9 | +With the [x402 protocol](https://www.x402.org/), AI agents can run Apify Actors and pay with USDC on the [Base](https://www.base.org/) blockchain - no Apify account needed. |
12 | 10 |
|
13 | 11 | :::caution Experimental feature |
14 | 12 |
|
15 | | -Agentic payments are an experimental feature and may change significantly as payment protocols and AI technologies evolve. |
| 13 | +Agentic payments are experimental and may change as payment protocols evolve. Only Actors with the [Pay Per Event](/platform/actors/publishing/monetize/pay-per-event) pricing model are supported. [Standby](/platform/actors/running/standby) Actors are not supported. |
16 | 14 |
|
17 | 15 | ::: |
18 | 16 |
|
19 | 17 | ## What is x402 |
20 | 18 |
|
21 | | -The [x402 protocol](https://www.x402.org/) is an open standard for internet-native payments. It enables direct on-chain payments using USDC on the [Base](https://www.base.org/) blockchain. |
| 19 | +The [x402 protocol](https://www.x402.org/) is an open standard for on-chain payments over HTTP. It uses [USDC](https://www.circle.com/usdc), a stablecoin pegged to the US dollar, on the [Base](https://www.base.org/) blockchain. |
22 | 20 |
|
23 | | -The Apify MCP server implements an MCP extension of this protocol to support agentic payments. The client automatically detects payment requirements in the tool's metadata and signs a USDC payment on the Base blockchain (mainnet). This payment is included with the tool call - no human intervention needed. |
| 21 | +When a server requires payment, it responds with HTTP `402` and tells the client what payment formats it accepts. The client signs a USDC transfer off-chain (locally, without touching the blockchain) and resends the request with the signature attached. The server settles (finalizes) the payment on-chain (on the blockchain) and processes the request. |
24 | 22 |
|
25 | | -## Use x402 with Apify MCP server |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- [`mcpc` CLI](https://github.com/apify/mcp-cli) installed (`npm install -g @apify/mcpc`) - manages your wallet and signs payments. |
| 26 | +- A wallet funded with USDC on [Base](https://www.base.org/) mainnet. |
26 | 27 |
|
27 | | -The [Apify MCP server](/platform/integrations/mcp) provides the simplest way for agents to access Apify's Actor library using x402 payments. |
| 28 | +### Wallet setup |
28 | 29 |
|
29 | | -### Prerequisites |
| 30 | +Use the `mcpc` CLI to create or import a wallet: |
30 | 31 |
|
31 | | -- _`mcpc` CLI_ - Install the [`mcpc` CLI](https://github.com/apify/mcp-cli) (`npm install -g @apify/mcpc`), the Universal MCP command-line client. |
32 | | -- _USDC on Base_ - A wallet funded with USDC on the [Base](https://www.base.org/) mainnet blockchain. |
| 32 | +```bash |
| 33 | +# Create a new wallet (generates a random private key) |
| 34 | +mcpc x402 init |
33 | 35 |
|
34 | | -### Setup |
| 36 | +# Or import an existing wallet from a private key |
| 37 | +mcpc x402 import <private-key> |
35 | 38 |
|
36 | | -Set up a local wallet and connect to the Apify MCP server with x402 payment support. |
| 39 | +# Verify your wallet address |
| 40 | +mcpc x402 info |
| 41 | +``` |
37 | 42 |
|
38 | | -1. Create or import a wallet: |
| 43 | +Fund the displayed wallet address with USDC on Base mainnet. |
39 | 44 |
|
40 | | - ```bash |
41 | | - # Create a new wallet (generates a random private key) |
42 | | - mcpc x402 init |
| 45 | +## Use x402 with Apify MCP server |
43 | 46 |
|
44 | | - # Or import an existing wallet from a private key |
45 | | - mcpc x402 import <private-key> |
| 47 | +The [Apify MCP server](/platform/integrations/mcp) is the recommended way to get started. `mcpc` handles payment signing and retries automatically. |
46 | 48 |
|
47 | | - # Verify your wallet address |
48 | | - mcpc x402 info |
49 | | - ``` |
| 49 | +### How it works |
50 | 50 |
|
51 | | -1. Fund the wallet with USDC on Base mainnet. |
| 51 | +1. The MCP server advertises payment requirements in each paid tool's metadata. |
| 52 | +1. When `mcpc` calls a paid tool, it signs a USDC payment using your wallet and includes it in the request. |
| 53 | +1. The server verifies and settles the payment on-chain, then runs the tool and returns the result. |
| 54 | +1. If you call a tool without a payment, the server responds that payment is required and `mcpc` signs and retries automatically. |
52 | 55 |
|
53 | | -1. Connect to the Apify MCP server with x402 payment mode enabled. Add the `?payment=x402` query parameter to the server URL and pass the `--x402` flag to `mcpc`: |
| 56 | +### Example |
54 | 57 |
|
55 | | - ```bash |
56 | | - mcpc connect "mcp.apify.com?payment=x402" @apify --x402 |
57 | | - ``` |
| 58 | +:::tip Agent autonomy |
58 | 59 |
|
59 | | -1. Call tools as usual. Payments are handled automatically: |
| 60 | +Provide this page (or the example below) as context to your AI agent. The agent can then connect to the Apify MCP server and pay for tools via x402 autonomously - no human in the loop. |
60 | 61 |
|
61 | | - ```bash |
62 | | - mcpc @apify tools-call call-actor actor:="apify/rag-web-browser" input:='{"query": "latest AI news"}' |
63 | | - ``` |
| 62 | +::: |
| 63 | + |
| 64 | +```bash |
| 65 | +# Connect to the Apify MCP server with x402 enabled |
| 66 | +mcpc connect "mcp.apify.com?payment=x402" @apify --x402 |
| 67 | + |
| 68 | +# Call a paid tool - payment is handled automatically |
| 69 | +mcpc @apify tools-call call-actor actor:="apify/instagram-post-scraper" input:='{"username": ["natgeo"], "resultsLimit": 3}' |
| 70 | +``` |
| 71 | + |
| 72 | +## Use x402 with the Apify API directly |
| 73 | + |
| 74 | +You can also send x402 payments directly against the Apify REST API. This works with any HTTP client or x402-compatible wallet. |
64 | 75 |
|
65 | 76 | ### How it works |
66 | 77 |
|
67 | | -When x402 payment mode is enabled, the agent handles the full payment flow autonomously: |
| 78 | +1. Call the Apify API with the header `X-APIFY-PAYMENT-PROTOCOL: X402` and no payment signature. |
| 79 | +1. The API returns HTTP `402` with a `PAYMENT-REQUIRED` header - base64-encoded JSON listing accepted payment formats. |
| 80 | +1. Sign the payment using `mcpc x402 sign <PAYMENT-REQUIRED header value>`. |
| 81 | +1. Resend the request with two headers: `X-APIFY-PAYMENT-PROTOCOL: X402` and `PAYMENT-SIGNATURE: <signed payload>`. |
| 82 | +1. The API settles the payment on-chain, runs the Actor, and returns the dataset items directly. |
| 83 | + |
| 84 | +### Example |
| 85 | + |
| 86 | +:::caution Actor name format |
| 87 | + |
| 88 | +When using the API, replace the `/` in the Actor name with `~` (for example, `apify/instagram-post-scraper` becomes `apify~instagram-post-scraper`). |
| 89 | + |
| 90 | +::: |
| 91 | + |
| 92 | +```bash |
| 93 | +# Step 1: Discover payment requirements |
| 94 | +curl -si \ |
| 95 | + -H "X-APIFY-PAYMENT-PROTOCOL: X402" \ |
| 96 | + "https://api.apify.com/v2/acts/apify~instagram-post-scraper/run-sync-get-dataset-items" |
| 97 | +# → HTTP 402 + PAYMENT-REQUIRED header value |
| 98 | + |
| 99 | +# Step 2: Sign the payment with mcpc |
| 100 | +mcpc x402 sign <PAYMENT-REQUIRED header value> |
| 101 | +# → outputs your base64 PAYMENT-SIGNATURE |
| 102 | + |
| 103 | +# Step 3: Run the Actor and get results in one call |
| 104 | +curl -s \ |
| 105 | + -H "X-APIFY-PAYMENT-PROTOCOL: X402" \ |
| 106 | + -H "PAYMENT-SIGNATURE: <base64-signed-payload>" \ |
| 107 | + -H "Content-Type: application/json" \ |
| 108 | + -d '{"username": ["natgeo"], "resultsLimit": 3}' \ |
| 109 | + "https://api.apify.com/v2/acts/apify~instagram-post-scraper/run-sync-get-dataset-items" |
| 110 | +# → JSON array with the scraped Instagram posts |
| 111 | +``` |
| 112 | + |
| 113 | +You can replace `apify~instagram-post-scraper` with any supported Actor from [Apify Store](https://apify.com/store). |
| 114 | + |
| 115 | +## Pricing and refunds |
| 116 | + |
| 117 | +Both paths (MCP server and direct API) share the same pricing model: |
| 118 | + |
| 119 | +- Minimum transaction: $1 in USDC. |
| 120 | +- The payment is tracked as a prepaid balance. Subsequent calls draw from it without new on-chain transactions. |
| 121 | +- When using the MCP server, `mcpc` signs a new payment automatically when the balance runs out. |
| 122 | +- After 60 minutes of inactivity (no running Actors), any remaining balance is refunded on-chain to your wallet, minus a small [gas fee (network cost)](https://docs.base.org/base-chain/network-information/network-fees) charged by the Base network to process the transaction. |
68 | 123 |
|
69 | | -1. The Apify MCP server advertises payment requirements in each paid tool's metadata. |
70 | | -1. When `mcpc` calls a paid tool, it automatically signs a USDC payment using your local wallet and includes it in the request. |
71 | | -1. The server verifies and settles the payment on-chain, then executes the tool and returns the result. |
72 | | -1. If a tool is called without a payment, the server responds that payment is required and `mcpc` signs and retries automatically. |
| 124 | +## Next steps |
73 | 125 |
|
74 | | -The Apify MCP server requires a minimum transaction of $1.00. The server tracks the payment as a balance. Subsequent tool calls draw from this balance without requiring a new on-chain transaction. When the balance runs out, `mcpc` automatically signs a new payment to top it up. After a period of inactivity, the server refunds any remaining unused balance to the client's wallet address on the Base blockchain. |
| 126 | +- Browse [Apify Store](https://apify.com/store) for supported Actors to use with x402. |
| 127 | +- Learn more about [Pay Per Event](/platform/actors/publishing/monetize/pay-per-event) pricing for Actors. |
| 128 | +- Set up the [Apify MCP server](/platform/integrations/mcp) for automated tool discovery and payment handling. |
| 129 | +- Read the [x402 protocol specification](https://www.x402.org/) for technical details on the payment standard. |
0 commit comments