# Agent quick start

The calls an AI agent makes to buy BTC, in order, over plain HTTP or with the command line's `--json`. An order is typed data the agent's wallet signs, so no person needs to click anything, and the API key cannot move funds. Everything here runs in the sandbox, where nothing real moves.

| # | Call | Gives |
| --- | --- | --- |
| 1 | `POST /sandbox/keys` | A test key, with no sign-up. Once: keep it |
| 2 | `GET /price` | What an amount gets now. Nothing reserved |
| 3 | `GET /quote` | The order for this wallet and address, with `steps`. Valid for 60 seconds |
| 4 | Sign each `signature` step, then `POST /orders` | The `orderId` |
| 5 | `GET /status?orderId=…` | `state`, until it ends |

## Get a key once

One IP address can get five test keys an hour, so an agent should get one and reuse it, not get one per run.

```sh
export BASE=https://openapi.maxy.co/v1
export MAXY_API_KEY=$(curl -s -X POST "$BASE/sandbox/keys" | jq -r .secretKey)
```

## Price, quote, sign, status

The agent pays with USDC on Base: it signs and sends no transaction. The wallet is a throwaway one here; live, it is the agent's own. The Bitcoin address is a public test address, for a test key only.

```sh
H="x-maxy-key: $MAXY_API_KEY"
TEST_WALLET_KEY=$(cast wallet new --json | jq -r '.[0].private_key')
WALLET=$(cast wallet address --private-key "$TEST_WALLET_KEY")

curl -sG -H "$H" "$BASE/price" --data-urlencode "from=USDC on base" -d to=BTC -d amount=20 | jq -c '{toAmount, liquidityAvailable}'
curl -sG -H "$H" "$BASE/quote" --data-urlencode "from=USDC on base" -d to=BTC -d amount=20 \
  -d fromAddress="$WALLET" -d toAddress=bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 > quote.json
jq -e '.sandbox == true' quote.json > /dev/null || { echo 'This sample is for a test key (mxk_test_…).' >&2; exit 1; }
jq -c '{quoteId, issues}' quote.json

SIGS='{}'
for ID in $(jq -r '.steps[]? | select(.kind == "signature") | .id' quote.json); do
  SIG=$(cast wallet sign --private-key "$TEST_WALLET_KEY" --data "$(jq -c --arg id "$ID" '.steps[] | select(.id == $id) | .typedData' quote.json)")
  SIGS=$(echo "$SIGS" | jq -c --arg id "$ID" --arg sig "$SIG" '. + {($id): $sig}')
done
ORDER_ID=$(curl -s -X POST "$BASE/orders" -H "$H" -H "content-type: application/json" \
  -d "$(jq -c --argjson sigs "$SIGS" '{order: .order, signatures: $sigs}' quote.json)" | jq -r .orderId)

until curl -s -H "$H" "$BASE/status?orderId=$ORDER_ID&chainId=8453" > status.json && jq -e '.isComplete or .state == "refundable" or .error' status.json > /dev/null; do
  sleep 2
done
jq -c '{state, btcTxId, error}' status.json
```

```python
import os
import time
import requests
from eth_account import Account
from eth_account.messages import encode_typed_data

BASE = "https://openapi.maxy.co/v1"
h = {"x-maxy-key": os.environ["MAXY_API_KEY"]}
wallet = Account.create()  # the agent's wallet; live, its own
ask = {"from": "USDC on base", "to": "BTC", "amount": "20"}

price = requests.get(f"{BASE}/price", headers=h, params=ask).json()
quote = requests.get(f"{BASE}/quote", headers=h, params={
    **ask, "fromAddress": wallet.address, "toAddress": "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"}).json()
if not quote.get("steps"):
    raise SystemExit(quote.get("issues") or quote.get("error"))

sigs = {s["id"]: "0x" + bytes(wallet.sign_message(encode_typed_data(full_message=s["typedData"])).signature).hex()
        for s in quote["steps"] if s["kind"] == "signature"}
placed = requests.post(f"{BASE}/orders", headers=h, json={"order": quote["order"], "signatures": sigs}).json()

while True:
    status = requests.get(f"{BASE}/status", headers=h, params={"orderId": placed["orderId"], "chainId": 8453}).json()
    if status.get("error") or status["isComplete"] or status["state"] == "refundable":
        break
    time.sleep(2)
print(price["toAmount"], status.get("state"), status.get("btcTxId"))
```

In the sandbox the order reads `pending`, then `in_progress`, then `completed`, in about 20 seconds. Paying with ETH adds one escrow transaction and `POST /orders/submit`: [Place the order](https://maxy.co/docs/buy-btc#place-the-order).

## With the command line

The same order with `npx -p maxy-labs maxy`, once the SDK is installed ([Command line](https://maxy.co/docs/command-line)). `MAXY_WALLET_KEY` gives the command line a wallet without storing it: here a throwaway one; live, the agent's own. With `--json` every command prints one JSON object (`order watch`: one line per change): `swap` prints it once the order is placed, with `orderId` and `next`, the command that follows the order.

```sh
export MAXY_WALLET_KEY=$(cast wallet new --json | jq -r '.[0].private_key')
npx -p maxy-labs maxy price 20 usdc base --json
ORDER_ID=$(npx -p maxy-labs maxy swap 20 usdc base to btc --from MAXY_WALLET_KEY \
  --to bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4 --yes --json | jq -r .orderId)
npx -p maxy-labs maxy order watch "$ORDER_ID" --json
```

## What each answer means

| The agent reads | It means | The agent does |
| --- | --- | --- |
| `state: pending` or `in_progress` | A solver is committing or paying | Poll again in 2 seconds |
| `state: completed` | The BTC is sent: `btcTxId` | Record `btcTxId`, and stop |
| `state: refundable` | No solver filled in time | `GET /orders/{id}/refund`, and send its transaction from the wallet |
| `state: cancelled`, `expired`, `refunded` | Ended, nothing paid out | Stop. Quote again if still wanted |
| `state: failed` | See `rawStatus` | Stop, and send Maxy the `orderId` and `requestId` |
| `issues.orderCap`, `order_cap_exceeded` | Over the key's cap | Order less |
| `liquidityAvailable: false` | Cannot fill this size now | A smaller amount, or later |
| `quote_expired` | Over 60 seconds since the quote | Quote again, sign again |
| `429`, `rate_limited` | Over the rate limit | Wait `Retry-After` seconds |
| `retryable: true` | The same call may work later | Retry it as is: the same signed order returns the same `orderId` |

Which key an agent gets, and what each can do: [For AI agents](https://maxy.co/docs/for-ai-agents).

---
Source: https://maxy.co/docs/agent-quick-start (Maxy Docs › Command line and agents › Agents)
