stagehand icon indicating copy to clipboard operation
stagehand copied to clipboard

Add x402 agent payments: let AI agents pay per browser session autonomously (USDC, no shared credit pools)

Open QuinnYates opened this issue 8 months ago • 2 comments

Add x402 agent payments: let AI agents pay per browser session autonomously

Stagehand + Browserbase is one of the most natural fits for agent-native payments. Agents use browser automation to complete real web tasks — and today they run against an operator's pre-paid Browserbase credit pool. The operator manually manages billing as agents consume sessions.

x402 changes this. HTTP 402 Payment Required, repurposed for agent micropayments (Coinbase-backed, live on Base mainnet):

Agent starts browser session
  → Browserbase returns 402 + payment challenge
  → Agent pays USDC per-session on Base
  → Agent retries with X-Payment proof
  → Browserbase verifies → starts session → returns CDP endpoint

No operator in the loop. Each session is a settled micropayment from the agent's wallet.

402 challenge format

{
  "version": "x402/1.0",
  "accepts": [{
    "scheme": "exact",
    "network": "base-mainnet",
    "maxAmountRequired": "50000",
    "resource": "https://www.browserbase.com/v1/sessions",
    "description": "Browserbase session — per session",
    "payTo": "0xYOUR_WALLET",
    "asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"
  }]
}

Why this fits agentic browser automation particularly well

Browserbase is built for agents — Stagehand is designed for autonomous web tasks. The billing model should match: per-session, agent-funded, no human top-up. An AI agent running a nightly data collection job should be able to fund its own browser sessions from its wallet.

x402 also enables per-agent cost attribution at the wallet level, which is not possible with shared API keys.

Implementation

Full guide: https://github.com/tomopay/gateway/blob/main/docs/guides/x402-rest-api-integration.md

~10 lines of verification code. No new infrastructure — additional 402 path on the session creation endpoint.


Happy to discuss how to handle session timeout/refund semantics (the x402 spec supports max-timeout fields to handle this).

QuinnYates avatar Apr 03 '26 04:04 QuinnYates

Great writeup on the mechanics. We shipped an x402-native endpoint for a complementary piece of agent infrastructure — CAPTCHA solving — if it's useful as a reference implementation.

The /x402/solve endpoint on solve is live on Base mainnet. The agent sends no API key — just hits the endpoint, gets a 402 back with payment requirements, signs with its wallet, retries, and gets the Turnstile token:

import axios from 'axios';
import { withPaymentInterceptor } from 'x402-axios';
import { privateKeyToAccount } from 'viem/accounts';

const account = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY);
const client = withPaymentInterceptor(axios.create(), account);

const { data } = await client.post('https://api.useagentstore.com/x402/solve', {
  type: 'turnstile',
  url: 'https://example.com',
});
// → { success: true, token: '0.abc...', solvedInMs: 2200 }

We wrote up the full protocol flow and the economics (/bin/zsh.002/call, Base fees ~/bin/zsh.0001) in a blog post if you want a concrete example to reference when discussing the Browserbase integration.

The session-level semantics you mention (timeout/refund) are interesting — we handle solve failures with auto-refund on the 502 path. Happy to compare notes on how we structured that.

getrelove avatar Apr 19 '26 16:04 getrelove

Browserbase supports x402! https://docs.browserbase.com/integrations/x402/introduction#x402-protocol-integration https://x.com/browserbase/status/2003171178211606973?s=20

miguelg719 avatar Apr 29 '26 01:04 miguelg719