Skip to main content

Getting started with requests

Make x402 payments with a requests client in 2 minutes.
You can find the full code for this example here.

Step 1: Install dependencies

The [evm,svm] extras pull in the EVM and SVM payment mechanisms used below. Without them, the x402.mechanisms.evm / x402.mechanisms.svm imports fail. Install only the extra you need (e.g. 'x402[svm]') if you’re paying on a single network. The quotes keep shells like zsh from expanding the brackets.

Step 2: Set your environment variables

Your .env file should look like this:
  • EVM_PRIVATE_KEY: Hex EVM private key of the paying account (optional if SVM provided)
  • SVM_PRIVATE_KEY: Base58 Solana private key of the paying account (optional if EVM provided)
  • RESOURCE_SERVER_URL: Base URL of the server to call (e.g. http://localhost:4021)
  • ENDPOINT_PATH: Path to a paid endpoint (e.g. /weather)
At least one of EVM_PRIVATE_KEY or SVM_PRIVATE_KEY is required.

Step 3: Create the client script

This example loads your env, creates a sync x402 client (x402ClientSync), registers the EVM and/or SVM payment schemes, uses x402_requests(client) as a context manager to get a session, makes a GET request, and logs the response body and payment settlement from the response headers.

Step 4: Run the script

Your client is now making x402 payments!

Step 5: Test the client

You can test payments against a local server by running the fastapi example or the flask example from the x402 repository. Just set your environment variables to match your local server, install the dependencies, and run the examples.
You can also test your client against PayAI’s live Echo Merchant for free. You will receive a full refund of any tokens that you send, and PayAI will pay for the network fees.

x402 reference

For a deeper dive into message shapes, headers, verification and settlement responses, see the x402 Reference.

Need help?

Join our Community

Have questions or want to connect with other developers? Join our Discord server.