Create Your First Fetch Client
Use x402 with the standard Fetch API to make paid HTTP requests. Perfect for both browser and Node.js environments.1
Install Dependencies
Create a new project and install the required packages:
Expected outcome: Project directory created with x402 fetch client dependencies installed.
2
Environment Configuration
Create a
.env
file in your project root:Security Note: Never commit private keys to version control. Use environment variables or secure key management services in production. For testing, you can use a dummy private key.
Expected outcome: Environment variables configured for your fetch client.
3
Create the fetch client
Create
fetch-client.ts
with the payment-enabled fetch implementation:Expected outcome: Fetch client code created with automatic payment handling.
4
Run your fetch client
Execute your fetch client to make paid requests:Example successful output:
Expected outcome: The client should automatically handle the 402 Payment Required response, make the payment, and receive the protected content.
Make sure your paid API server is running on the configured URL. The fetch client will automatically detect 402 responses, handle payment processing, and retry the request with payment proof.