0x CLI

View as Markdown

The 0x CLI is a single binary, 0x, that wraps the 0x APIs — indicative prices, on-chain swaps, gasless swaps, cross-chain bridging, and status polling — behind one command-line tool.

It works two ways:

  • For you, it’s a fast, safe way to trade from the terminal: colored quote tables, confirmation prompts, transaction simulation before every execution, and OS-keyring storage for wallet secrets.
  • For your AI agent, every command is non-interactive-safe and emits a machine-readable JSON envelope with stable exit codes, structured error codes, and inline response schemas in every --help. It also bundles its own 0x-trade agent skill so a coding agent knows exactly how to drive the CLI.

The CLI calls the 0x APIs, so a 0x API key is required. Price commands need only the key, on-chain swaps additionally need a wallet (an EVM private key and/or a Solana keypair).

Install

Verify the install:

$0x --version

Configure

Run the interactive setup once, or set values manually:

$0x config init

Guides you through your API key, default chain, and wallet.

Wallet secrets are stored in the OS keyring (macOS Keychain, Linux libsecret, Windows Credential Locker) by default and are never written to disk in plaintext. You can opt out with --plaintext (or the ZEROX_* env vars) when a keyring isn’t available.

Quick start

$# Check a price (read-only, no wallet needed) — USDC → WETH on Base
$0x price --chain base \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0x4200000000000000000000000000000000000006 \
> --amount 1000000
$
$# Execute a swap
$0x swap --chain base \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0x4200000000000000000000000000000000000006 \
> --amount 1000000
$
$# Bridge USDC from Base to Arbitrum
$0x cross-chain --from base --to arbitrum \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0xaf88d065e77c8cC2239327C5EDb3A432268e5831 \
> --amount 1000000

Amounts are in base units: USDC (6 decimals) 1000000 = 1 USDC, ETH (18) 1000000000000000000 = 1 ETH, SOL (9) 1000000000 = 1 SOL.

Tokens must be passed as contract addresses (EVM) or base58 mint addresses (Solana) — ticker symbols like USDC are not accepted. Addresses are chain-specific; USDC on Base and USDC on Ethereum are different contracts.

What it can do

CommandDescription
0x priceIndicative price for a pair (standard or --gasless)
0x swapOn-chain swap on any supported EVM chain or Solana; --gasless for no-gas swaps
0x cross-chainBridge and swap between chains
0x statusTrack gasless or cross-chain trade status (--poll to wait for completion)
0x chainsList supported chains
0x configManage API key, wallet, RPC, and defaults
0x skillInstall or print the bundled agent skill

Run any command with --help to see its flags and a RESPONSE: block documenting the JSON it returns.

Built for AI agents

The CLI is designed as a first-class tool for AI agents and scripts. Everything an agent needs to drive it reliably is stable and documented.

A consistent JSON envelope

When stdout is not a TTY, output switches to a JSON envelope automatically. Inside an agent loop, pass -o json-envelope explicitly for stability:

$0x price --chain base \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0x4200000000000000000000000000000000000006 \
> --amount 1000000 -o json-envelope
1{
2 "version": "1",
3 "command": "price",
4 "status": "success",
5 "exit_code": 0,
6 "data": { "rate": "0.00038", "buy_amount": { "raw": "380000000000000", "formatted": "0.00038" } },
7 "metadata": { "chain_id": 8453, "chain_name": "Base", "api_version": "v2" }
8}

On failure, the envelope carries a stable, actionable error:

1{
2 "command": "swap",
3 "status": "error",
4 "exit_code": 5,
5 "error": {
6 "code": "API_KEY_MISSING",
7 "message": "No API key configured",
8 "category": "config",
9 "retryable": false,
10 "suggestion": "Run '0x config set api_key <your-key>' or set ZEROX_API_KEY env var"
11 }
12}

Stable exit codes

Agents should match on exit_code first, then error.code. A few of the most useful:

CodeMeaningAgent action
0SuccessProceed
3Config errorRun 0x config init
4Network errorRetry with backoff
5Auth errorUpdate API key
6Validation failed (no liquidity, insufficient balance)Fix parameters or fund wallet
11Transaction revertedDo not retry as-is
12Transaction pendingPoll with 0x status
25Confirmation requiredRe-run with --yes
30Dry-run completedInformational

Non-interactive by design

Every interactive prompt has a flag equivalent, so an agent never gets stuck:

$# Non-interactive swap with JSON output
$0x swap --chain base \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0x4200000000000000000000000000000000000006 \
> --amount 1000000 --yes -o json-envelope
$
$# Simulate without executing — signs and submits nothing
$0x swap --chain base \
> --sell 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 \
> --buy 0x4200000000000000000000000000000000000006 \
> --amount 1000000 --dry-run

Bundled agent skill

The CLI ships with its own 0x-trade agent skill (source) compiled into the binary, so it’s always in sync with the installed version. Drop it into your agent’s skills directory with one command:

$# Installs SKILL.md + reference guides into ./.claude/skills/0x-trade/
$0x skill install
$
$# Or print the skill to stdout
$0x skill print

The skill teaches the agent the command contract — base-unit amounts, the --yes -o json-envelope pattern, exit-code handling, dry-run discipline — plus deep-dive references for gasless, cross-chain, Solana, config, tokens, and errors.

This is a different skill from the 0x-api Agent Skill. The 0x-api skill teaches an agent to write code against the 0x REST APIs; the bundled 0x-trade skill teaches it to drive this CLI. They’re complementary — use 0x-api when embedding swaps in your own app, 0x-trade when an agent runs the 0x binary as a tool.

Telemetry

The CLI sends anonymous, opt-out usage statistics (command name, exit code, chain, duration) to help prioritize chains and surface common errors. It never sends token addresses, amounts, transaction hashes, wallet addresses, API keys, or RPC URLs. Opt out any time:

$0x config set telemetry.enabled false # persistent
$export DO_NOT_TRACK=1 # cross-tool standard

Next steps