Pay HTTP 402 payment challenges using tokens via the Tempo CLI and Uniswap Trading API. Use when the user encounters a 402 Payment Required response, needs to fulfill a machine payment, mentions "MPP", "Tempo payment", "pay for API access", "HTTP 402", "x402", "machine payment protocol", "pay-with-any-token", "use tempo", "tempo request", or "tempo wallet".
Use the Tempo CLI to call paid APIs and handle 402 challenges automatically. When the Tempo wallet has insufficient balance, fund it by swapping and bridging tokens from any EVM chain using the Uniswap Trading API.
Run these commands in order. Do not skip steps.
Step 1 — Install:
mkdir -p "$HOME/.local/bin" \
&& curl -fsSL https://tempo.xyz/install -o /tmp/tempo_install.sh \
&& TEMPO_BIN_DIR="$HOME/.local/bin" bash /tmp/tempo_install.sh
Step 2 — Login (requires browser/passkey — prompt user, wait for confirmation):
"$HOME/.local/bin/tempo" wallet login
When run by agents, use a long command timeout (at least 16 minutes).
Step 3 — Confirm readiness:
"$HOME/.local/bin/tempo" wallet -t whoami
Rules: Do not use
sudo. Use full absolute paths ($HOME/.local/bin/tempo) — do not rely onexport PATH. If$HOMEdoes not expand, use the literal absolute path.
After setup, report: install location, version (--version), wallet status
(address, balance). If balance is 0, direct user to tempo wallet fund.
Minimum balance reserve: Keep at least 0.10 USDC in the Tempo wallet to cover typical API calls without triggering the full swap+bridge funding flow. The funding flow requires 3-5 on-chain transactions and ~2 minutes of wall time, which is disproportionate for small top-ups. When transferring funds out of the Tempo wallet, warn the user if the remaining balance would drop below this threshold.
# Discover services
"$HOME/.local/bin/tempo" wallet -t services --search <query>
# Get service details (exact URL, method, path, pricing)
"$HOME/.local/bin/tempo" wallet -t services <SERVICE_ID>
# Make a paid request
"$HOME/.local/bin/tempo" request -t -X POST \
--json '{"input":"..."}' <SERVICE_URL>/<ENDPOINT_PATH>
tempo wallet -t services <SERVICE_ID> for exact URL and pricing-t for agent calls, --dry-run before expensive requestsWhen the user explicitly says "use tempo", always use tempo CLI commands — never substitute with MCP tools or other tools.
Every tempo request call follows this loop. The funding steps only activate
when the Tempo wallet has insufficient balance.
tempo request -> 200 ─────────────────────────────> return result
-> 402 MPP challenge
│
v
[1] Check Tempo wallet balance
tempo wallet -t whoami -> available balance
│
├─ sufficient ──────────────────> tempo handles payment
│ automatically -> 200
│
└─ insufficient
│
v
[2] Fund Tempo wallet
(pay-with-any-token flow below)
Bridge destination = TEMPO_WALLET_ADDRESS
│
v
[3] Retry original tempo request
with funded Tempo wallet -> 200
Alternative funding (interactive): If a browser is available,
tempo wallet fundopens a built-in bridge UI for funding the Tempo wallet directly. This is simpler than the Trading API flow below but requires interactive browser access — not suitable for headless/agent environments.
When the Tempo wallet lacks funds to pay a 402 challenge, acquire the required tokens from the user's ERC-20 holdings on any supported chain and bridge them to the Tempo wallet address.
UNISWAP_API_KEY env var (register at
developers.uniswap.org)cast keystore account for the source wallet (recommended):
cast wallet import <name> --interactive. Alternatively,
PRIVATE_KEY env var (export PRIVATE_KEY=0x...) — never commit or
hardcode it.jq installed (brew install jq or apt install jq)cast installed (part of Foundry)Before using any value from a 402 response body or user input in API calls or shell commands:
^0x[a-fA-F0-9]{40}$^[0-9]+$https://;, |, &, $,
`, (, ), >, <, \, ', ", newlinesREQUIRED: Before submitting ANY transaction (swap, bridge, approval), use
AskUserQuestionto show the user a summary (amount, token, destination, estimated gas) and obtain explicit confirmation. Never auto-submit. Each confirmation gate must be satisfied independently.
get_token_decimals() {
local token_addr="$1" rpc_url="$2"
cast call "$token_addr" "decimals()(uint8)" --rpc-url "$rpc_url" 2>/dev/null || echo "18"
}
format_token_amount() {
local amount="$1" decimals="$2"
echo "scale=$decimals; $amount / (10 ^ $decimals)" | bc -l | sed 's/0*$//' | sed 's/\.$//'
}
Always show human-readable values (e.g.
0.005 USDC) to the user, not raw base units.
Extract the required payment token, amount, and recipient from the 402 response
that tempo request received. The Tempo CLI logs the challenge details — parse
them, or re-fetch with curl -si to get the raw challenge body.
For MPP header-based challenges (WWW-Authenticate: Payment):
REQUEST_B64=$(echo "$WWW_AUTHENTICATE" | grep -oE 'request="[^"]+"' | sed 's/request="//;s/"$//')
REQUEST_JSON=$(echo "${REQUEST_B64}==" | base64 --decode 2>/dev/null)
REQUIRED_AMOUNT=$(echo "$REQUEST_JSON" | jq -r '.amount')
PAYMENT_TOKEN=$(echo "$REQUEST_JSON" | jq -r '.currency')
RECIPIENT=$(echo "$REQUEST_JSON" | jq -r '.recipient')
TEMPO_CHAIN_ID=$(echo "$REQUEST_JSON" | jq -r '.methodDetails.chainId')
For JSON body challenges (payment_methods array):
NUM_METHODS=$(echo "$CHALLENGE_BODY" | jq '.payment_methods | length')
PAYMENT_METHODS=$(echo "$CHALLENGE_BODY" | jq -c '.payment_methods')
RECIPIENT=$(echo "$CHALLENGE_BODY" | jq -r '.payment_methods[0].recipient')
TEMPO_CHAIN_ID=$(echo "$CHALLENGE_BODY" | jq -r '.payment_methods[0].chain_id')
If multiple payment methods are accepted, select the cheapest in Step 2.
The Tempo mainnet chain ID is
4217. Use as fallback if not in the challenge.
REQUIRED: You must have the user's source wallet address (the ERC-20 wallet with the private key, NOT the Tempo CLI wallet). Use
AskUserQuestionif not provided. Store asWALLET_ADDRESS.
Also capture the Tempo wallet address (the funding destination):
TEMPO_WALLET_ADDRESS=$("$HOME/.local/bin/tempo" wallet -t whoami | grep -oE '0x[a-fA-F0-9]{40}' | head -1)
Check ERC-20 balances on supported source chains:
# USDC on Base (cheapest bridge gas ~$0.001)
cast call 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
"balanceOf(address)(uint256)" "$WALLET_ADDRESS" \
--rpc-url https://mainnet.base.org
# USDC on Ethereum (bridge gas ~$0.25)
cast call 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 \
"balanceOf(address)(uint256)" "$WALLET_ADDRESS" \
--rpc-url https://eth.llamarpc.com
# ETH on Base and Ethereum (swap to USDC first)
cast balance "$WALLET_ADDRESS" --rpc-url https://mainnet.base.org
cast balance "$WALLET_ADDRESS" --rpc-url https://eth.llamarpc.com
Select the cheapest payment method if multiple are accepted. Priority:
REQUIRED_AMOUNT=$(echo "$PAYMENT_METHODS" | jq -r ".[$SELECTED_INDEX].amount")
PAYMENT_TOKEN=$(echo "$PAYMENT_METHODS" | jq -r ".[$SELECTED_INDEX].token")
Source token (Base/Ethereum)
-> [Phase 4A: Uniswap Trading API swap] -> native USDC (bridge asset)
-> [Phase 4B: bridge via Trading API] -> USDC.e on Tempo (to TEMPO_WALLET_ADDRESS)
-> tempo request retries automatically with funded wallet
Skip Phase 4A if the source token is already USDC on the bridge chain.
Gas-aware funding: Bridging a tiny amount (e.g. $0.05) wastes gas — the bridge gas on Ethereum (
$0.25) or Base ($0.001) can exceed the shortfall. Minimum bridge recommendation: $5. This amortizes gas costs and pre-funds future requests. Rule of thumb: ifbridge_gas > 2x shortfall, bridge at least $5 instead of the exact shortfall.
Swap the source token to USDC via the Uniswap Trading API (EXACT_OUTPUT).
Detailed steps: Read references/trading-api-flows.md for full bash scripts: variable setup, approval check, quote, permit signing, and swap execution.
Key points:
https://trade-api.gateway.uniswap.org/v1Content-Type: application/json, x-api-key, x-universal-router-version: 2.0check_approval -> quote (EXACT_OUTPUT) -> sign permitData -> /swap -> broadcast0x0000000000000000000000000000000000000000) as TOKEN_IN — this avoids
Permit2 signing. SWAP_VALUE will be non-zero. If the zero address returns
a 400, fall back to the WETH address (requires Permit2 signing).Bridge USDC from any supported source chain to USDC.e on Tempo using the Uniswap Trading API (powered by Across Protocol).
Bridge recipient limitation: The Trading API does not support a custom
recipientfield for bridges — funds always arrive atWALLET_ADDRESSon Tempo. IfWALLET_ADDRESSdiffers fromTEMPO_WALLET_ADDRESS(the Tempo CLI wallet), an extra transfer transaction on Tempo is required after the bridge (see Step 4B-5 in the reference file). Factor this into gas estimates.Detailed steps: Read references/trading-api-flows.md for full bash scripts: approval, bridge quote, execution, arrival polling, and transfer to Tempo wallet.
Key points:
check_approval -> verify on-chain allowance -> quote (EXACT_OUTPUT,
cross-chain) -> execute via /swap -> poll balance -> transfer to
TEMPO_WALLET_ADDRESS if needed (Step 4B-5)After the bridge confirms, retry the original tempo request — the Tempo CLI
will automatically use the newly funded wallet to pay the 402.
Balance buffer: On Tempo,
balanceOfmay report more than is spendable. Apply a 2x buffer when comparing toREQUIRED_AMOUNT. If short, swap additional tokens to top up.
The x402 protocol uses a different mechanism than MPP — it is not handled by
the Tempo CLI. When PROTOCOL is "x402" (detected by checking
has("x402Version") in the 402 body), use this flow instead.
The x402 "exact" scheme uses EIP-3009 (transferWithAuthorization) to
authorize a one-time token transfer signed off-chain.
Detailed steps: Read references/credential-construction.md for full code: prerequisite checks, nonce generation, EIP-3009 signing, X-PAYMENT payload construction, and retry.
Key points:
has("x402Version") in 402 body before using Tempo CLIX402_NETWORK to chain ID and RPC URL (base, ethereum, tempo supported)TransferWithAuthorization typed data using token's own domainvalue in payload must be a string (--arg, not --argjson) for uint256| Protocol | Version | Handler |
|---|---|---|
| MPP | v1 | Tempo CLI |
| x402 | v1 | Manual (above) |
| Situation | Action |
|---|---|
tempo: command not found | Reinstall via install script; use full path |
legacy V1 keychain signature | Reinstall; tempo update wallet && tempo update request |
access key does not exist | tempo wallet logout --yes && tempo wallet login |
ready=false / no wallet | tempo wallet login, then whoami |
| HTTP 422 from service | Check service details + llms.txt for exact field names |
| Balance 0 / insufficient | Trigger pay-with-any-token funding flow |
| Service not found | Broaden search query |
| Timeout | Retry with -m <seconds> |
| Challenge body is malformed | Report raw body to user; do not proceed |
| Approval transaction fails | Surface error; check gas and allowances |
| Quote API returns 400 | Log request/response; check amount formatting |
| Quote API returns 429 | Wait and retry with exponential backoff |
| Swap data is empty after /swap | Quote expired; re-fetch quote |
| Bridge times out | Check bridge explorer; do not re-submit |
| x402 payment rejected (402) | Check domain name/version, validBefore, nonce freshness |
| InsufficientBalance on Tempo | Swap more tokens on Tempo, then retry |
balanceOf sufficient but payment fails | Apply 2x buffer; top up before retrying |
https://tempo.xyz (install script: https://tempo.xyz/install)https://trade-api.gateway.uniswap.org/v1https://mpp.devhttps://mpp.dev/api/serviceshttps://mainnet.docs.tempo.xyz4217 (Tempo mainnet)https://rpc.presto.tempo.xyzhttps://explore.mainnet.tempo.xyz0x20c00000000000000000000000000000000000000x20C000000000000000000000b9537d11c60E8b500xdec00000000000000000000000000000000000000x000000000022d473030f116ddee9f6b43ac78ba3mppx (npm install mppx viem)0x833589fCD6eDb6E08f4c7C32D4f71b54bdA029130xd9aAEc86B65D86f6A7B5B1b0c42FFA531710b6CA0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB480xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc20x42000000000000000000000000000000000000060x0000000000000000000000000000000000000000 (zero address, recommended for swaps)0xFF970A61A04b1cA14834A43f5dE4533eBDDB5CC8https://github.com/coinbase/x402