Menu

Pay an x402 endpoint from plain Python (no SDK, ~30 lines)
📰

Pay an x402 endpoint from plain Python (no SDK, ~30 lines)

DEV Community: api·Penny·3 days ago
#b7TtVC7x
#devtoapi#devto#dev#name#type
Reading 0:00
15s threshold

Most x402 tutorials assume you'll install a vendor SDK. You don't need one. Paying an x402 (v2, "exact" scheme, USDC on Base) endpoint takes one HTTP retry and one EIP-712 signature. Here is the whole flow in standard-library Python plus eth_account .

1. Ask, get a 402

Call the endpoint normally. An x402 server answers 402 Payment Required with a JSON body that lists what it accepts:

import json , urllib . request try : urllib . request . urlopen ( url , timeout = 20 ) except urllib . error . HTTPError as e : pr = json . loads ( e . read ()) # payment requirements req = pr [ " accepts " ][ 0 ] # scheme, network, asset, amount, payTo, extra{name,version}

Enter fullscreen mode

Exit fullscreen mode

amount is in the asset's smallest unit (USDC has 6 decimals, so "1000" = $0.001).

Check it before you sign: confirm asset is the real USDC contract on Base ( 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 ), that amount is what you expect, and that payTo is the seller you intend to pay.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.