Back to articles
Pay-per-request APIs from Python: skip the API key, pay with USDC instead

Pay-per-request APIs from Python: skip the API key, pay with USDC instead

via Dev.to TutorialSebastian Wallinstabot project

API keys are friction. Signup, billing portal, monthly minimums, credential rotation. For agents and scripts that call APIs occasionally, there's a better model: pay per call, no account needed. The x402 protocol does this at the HTTP level. Your script gets a 402 Payment Required response, pays automatically, and retries. Here's how it works from Python. Install pip install "x402[httpx,evm]" Call a pay-per-request API import asyncio import httpx from x402 import x402Client from x402.http.clients.httpx import x402AsyncTransport from x402.mechanisms.evm.exact import ExactEvmScheme from x402.mechanisms.evm import EthAccountSigner from eth_account import Account # Wire up your wallet once account = Account . from_key ( " 0xYOUR_PRIVATE_KEY " ) signer = EthAccountSigner ( account ) wallet = x402Client () wallet . register ( " eip155:* " , ExactEvmScheme ( signer = signer )) async def search ( query : str ): transport = x402AsyncTransport ( wallet ) async with httpx . AsyncClient ( transpor

Continue reading on Dev.to Tutorial

Opens in a new tab

Read Full Article
3 views

Related Articles