DEV Community

OpenClaw Cash
OpenClaw Cash

Posted on

Swap tokens from your agent wallet: quote first, then execute

An agent that holds SOL and owes USDC is stuck: it can send what it holds, but the invoice is in a token it does not hold. Swapping is the missing step, and it is two calls, not a detour through a DEX interface. This is the whole flow over the agent API, with every route and field copied out of the public reference at https://openclawcash.com/docs.

Every call authenticates with the same header, and the wallet always spends its own funds:

X-Agent-Key: occ_your_api_key
Enter fullscreen mode Exit fullscreen mode

1. Get the wallet id

Everything else selects a wallet, so start where the ids are:

curl "https://openclawcash.com/api/agent/wallets?includeBalances=true" \
  -H "X-Agent-Key: occ_your_api_key"
Enter fullscreen mode Exit fullscreen mode

The response lists each wallet with id, label, address, network and chain, plus the native balance when you ask for it. Keep the id, it is what the later calls take.

2. Quote before anything moves

The quote route prices the swap and signs nothing. It needs X-Agent-Key like every other agent call, and it is the safe place to look before you commit:

curl -X POST "https://openclawcash.com/api/agent/quote?network=solana-mainnet" \
  -H "Content-Type: application/json" \
  -H "X-Agent-Key: occ_your_api_key" \
  -d '{
    "chain": "solana",
    "walletId": "YUZE66Z",
    "tokenIn": "SOL",
    "tokenOut": "USDC",
    "amountIn": "10000000"
  }'
Enter fullscreen mode Exit fullscreen mode

The answer carries amountOut, amountOutHuman, amountIn, amountInHuman, route, feePercent, dex and network. No transaction is built, no fee is taken and nothing is signed.

On EVM the same route takes the chain as a query parameter instead:

curl -X POST "https://openclawcash.com/api/agent/quote?network=polygon-mainnet" \
  -H "Content-Type: application/json" \
  -H "X-Agent-Key: occ_your_api_key" \
  -d '{
    "walletId": "Q7X2K9P",
    "tokenIn": "WETH",
    "tokenOut": "USDC",
    "amountIn": "100000000000000000"
  }'
Enter fullscreen mode Exit fullscreen mode

amountIn is in base units in both cases: lamports on Solana, so 10000000 is 0.01 SOL, and wei on EVM, so 1000000000000000000 is 1 WETH. That is easy to get wrong, which is why the quote hands amountInHuman and amountOutHuman back to you.

3. Read the policy that will check the swap

Policies are checked before anything is signed, so read them before you are refused:

curl https://openclawcash.com/api/agent/policies \
  -H "X-Agent-Key: occ_your_api_key"
Enter fullscreen mode Exit fullscreen mode

Each entry pairs a wallet with its policies and their current usage. A daily_spending_limit comes back with config.amount and a usage block holding spent and limit for the window, so you can see how much of the cap the swap will consume before you send it.

4. Execute

Slippage lives here and not on the quote. This is the call that moves funds:

curl -X POST https://openclawcash.com/api/agent/swap \
  -H "Content-Type: application/json" \
  -H "X-Agent-Key: occ_your_api_key" \
  -d '{
    "chain": "solana",
    "walletId": "YUZE66Z",
    "tokenIn": "SOL",
    "tokenOut": "USDC",
    "amountIn": "10000000",
    "slippage": 0.5
  }'
Enter fullscreen mode Exit fullscreen mode

The response gives you txHash, status, dex, amountOut, amountOutMin, fee and feePercent. amountOutMin is the guard that came out of the slippage value you sent.

5. Confirm what landed

curl "https://openclawcash.com/api/agent/transactions?walletId=YUZE66Z&chain=solana" \
  -H "X-Agent-Key: occ_your_api_key"
Enter fullscreen mode Exit fullscreen mode

History merges on-chain and app-recorded rows, and each row carries its own network, so an EVM wallet can scope to one chain with network=base-mainnet or merge the bucket with network=all.

What to watch

  • Swaps route through Uniswap v2 (or a compatible pool) on EVM and Jupiter on Solana, so the venue for a given pair is fixed by the chain you are on.
  • Spending an ERC-20 on EVM can need POST /api/agent/approve first, which approves a spender contract for that token. The route is EVM only and it takes tokenAddress, spender and amount.
  • The quote is a price, not an action. Nothing moves until you call the swap, and the price can move between the two calls, which is what the slippage value and amountOutMin are there for.
  • Any valid ERC-20 contract or SPL mint works in wallet operations, not only the tokens the supported-tokens route recommends.

Top comments (0)