<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Zhasulan</title>
    <description>The latest articles on DEV Community by Zhasulan (@zhasulan391).</description>
    <link>https://dev.to/zhasulan391</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4211468%2Fd9be2929-d944-4b2c-84d7-0c50283fd4c5.jpg</url>
      <title>DEV Community: Zhasulan</title>
      <link>https://dev.to/zhasulan391</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zhasulan391"/>
    <language>en</language>
    <item>
      <title>How We Built x402 Payments for AI Agents</title>
      <dc:creator>Zhasulan</dc:creator>
      <pubDate>Tue, 27 Oct 2026 17:14:41 +0000</pubDate>
      <link>https://dev.to/zhasulan391/how-we-built-x402-payments-for-ai-agents-42n4</link>
      <guid>https://dev.to/zhasulan391/how-we-built-x402-payments-for-ai-agents-42n4</guid>
      <description>&lt;p&gt;# How We Built x402 Payments for AI Agents&lt;/p&gt;

&lt;p&gt;*From 402 header to USDC on-chain in 30 lines of code*&lt;br&gt;
---&lt;br&gt;
AI agents need to pay for things. Data APIs, compute, storage, other agents' services. But traditional payment systems (Stripe, PayPal) require human intervention. Credit cards, accounts, KYC — none of that works for autonomous agents.&lt;/p&gt;

&lt;p&gt;Enter **x402**: a protocol that turns HTTP 402 (Payment Required) into a crypto payment rail. No accounts. No API keys. Just a wallet and a request.&lt;/p&gt;

&lt;p&gt;We built an open-source implementation with two public SDKs (TypeScript + Python). Here's how it works.&lt;/p&gt;

&lt;p&gt;## The Problem&lt;/p&gt;

&lt;p&gt;Imagine an AI agent that needs real-time market data. It calls an API. The API wants $0.05 per request. How does the agent pay?&lt;/p&gt;

&lt;p&gt;- **Stripe?** Requires account setup, API keys, human approval.&lt;/p&gt;

&lt;p&gt;- **Crypto wallet?** Requires gas, complex signing, on-chain tx for every $0.05.&lt;/p&gt;

&lt;p&gt;- **x402?** One header. One signature. Done.&lt;br&gt;
## The Architecture&lt;br&gt;
agent server facilitator&lt;/p&gt;

&lt;p&gt;| GET /resource | |&lt;/p&gt;

&lt;p&gt;|-------------------------&amp;gt;| |&lt;/p&gt;

&lt;p&gt;| 402 + requirements | |&lt;/p&gt;

&lt;p&gt;|&amp;lt;-------------------------| |&lt;/p&gt;

&lt;p&gt;| sign EIP-3009 (off-chain) |&lt;/p&gt;

&lt;p&gt;| GET /resource + X-PAYMENT |&lt;/p&gt;

&lt;p&gt;|-------------------------&amp;gt;| verify + settle |&lt;/p&gt;

&lt;p&gt;| |---------------------------&amp;gt;|&lt;/p&gt;

&lt;p&gt;| | on-chain USDC transfer |&lt;/p&gt;

&lt;p&gt;| 200 + resource | |&lt;/p&gt;

&lt;p&gt;|&amp;lt;-------------------------| |&lt;/p&gt;

&lt;p&gt;1. **Agent** requests a paid resource&lt;/p&gt;

&lt;p&gt;2. **Server** responds 402 with payment requirements (amount, network, recipient)&lt;/p&gt;

&lt;p&gt;3. **Agent SDK** signs an EIP-3009 &lt;code&gt;TransferWithAuthorization&lt;/code&gt; off-chain (gasless!)&lt;/p&gt;

&lt;p&gt;4. **Agent** retries with &lt;code&gt;X-PAYMENT&lt;/code&gt; header&lt;/p&gt;

&lt;p&gt;5. **Server** forwards to **facilitator**, which verifies the signature and settles on-chain&lt;/p&gt;

&lt;p&gt;6. **Server** returns 200 + resource&lt;/p&gt;

&lt;p&gt;The seller receives USDC. The facilitator pays gas. The agent pays nothing extra.&lt;/p&gt;

&lt;p&gt;## The Code (Python)&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
python

from netthouse import X402Client

client = X402Client(private\_key="0x...")

\# Any paid URL. The SDK pays automatically on HTTP 402.

result = client.fetch("https://api.example.com/market-data")

if result.response.ok:

&amp;amp;#x20;   print(result.response.json())  # the paid resource

&amp;amp;#x20;   print("paid:", result.paid)    # True

That's it. Three lines. The SDK handles discovery, signing, retry.

The Code (TypeScript)

import { X402Client } from "@netthouse/sdk";

const client = new X402Client({ privateKey: "0x..." });

const result = await client.fetch("https://api.example.com/market-data");
if (result.response.ok) {

&amp;amp;#x20; console.log(await result.response.json());

&amp;amp;#x20; console.log("paid:", result.paid);

}

Same API. Same flow. Different language.

The EIP-3009 Signature

The magic is in the signature. EIP-3009 TransferWithAuthorization is an off-chain authorization that a facilitator can execute on-chain. It includes:

from: payer address

to: seller address

value: amount in USDC (6 decimals)

validAfter / validBefore: time window

nonce: unique per transaction

The SDK signs this with the payer's private key. The facilitator verifies the signature and submits the on-chain transaction. The seller receives USDC. The facilitator pays gas.

The Facilitator

The facilitator is a trusted third party that:

Verifies the EIP-3009 signature

Checks the nonce hasn't been used

Submits the on-chain transaction

Pays gas (reimbursed by the seller via a separate mechanism)

In our implementation, the facilitator is a Fastify module that runs alongside the API. For production, you'd run it as a separate service.

The Discovery

How does an agent know what resources are available and how much they cost? The /discover endpoint:
{

&amp;amp;#x20; "x402Version": 1,

&amp;amp;#x20; "items": \[

&amp;amp;#x20;   {

&amp;amp;#x20;     "resource": "/market-data",

&amp;amp;#x20;     "type": "http",

&amp;amp;#x20;     "x402Version": 1,

&amp;amp;#x20;     "accepts": \[

&amp;amp;#x20;       {

&amp;amp;#x20;         "scheme": "exact",

&amp;amp;#x20;         "network": "base-sepolia",

&amp;amp;#x20;         "payTo": "0x34798CCeF50aaa45194E5E20008711A5Fbfd182C",

&amp;amp;#x20;         "maxAmountRequired": "50000",

&amp;amp;#x20;         "resource": "/market-data",

&amp;amp;#x20;         "description": "Crypto market snapshot",

&amp;amp;#x20;         "asset": "0x036CbD53842c5426634e7929541eC2318f3dCF7e"

&amp;amp;#x20;       }

&amp;amp;#x20;     ]

&amp;amp;#x20;   }

&amp;amp;#x20; ]

}

The agent calls /discover, sees the catalog, picks a resource, and pays.

The First Payment

We tested the Python SDK against our live API. The first payment:

Resource: /market-data ($0.05 USDC)

Network: Base Sepolia (testnet)

Transaction: https://sepolia.basescan.org/tx/0xf9f7589adeb5e10019cd95b871cb74b3b3e3b54b5940964bae2794ce912eb0e8

Status: Success ✅

The agent paid. The seller received USDC. The facilitator paid gas. Everything worked.

What's Next

Mainnet launch: Switch from Base Sepolia to Base mainnet (need ETH for facilitator gas)

More resources: Add /news, /weather, /sentiment to the catalog

Dashboard: Seller analytics (revenue, requests, popular resources)

Webhooks: Notify sellers when payments arrive

Rust SDK: For high-performance agents

Try It

TypeScript SDK: npm install @netthouse/sdk

Python SDK: pip install netthouse

Live API: https://netthouse-api.onrender.com/discover

GitHub: https://github.com/Zhasulan391/sdk-typescript

Both SDKs are MIT licensed. Both are public. Both are working.

Built with Fastify, viem, eth-account, and a lot of coffee.



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>agents</category>
      <category>ai</category>
      <category>crypto</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
