---
name: anyfare-buyer
description: Pay for API calls through Anyfare with a payment key that has limits, or from a wallet over x402 or MPP. Use when the user wants an agent to call a paid API, to check what a paid link costs, to sign up for Anyfare, to top up, to make a payment key, or to check spending.
---

# Anyfare for buyers

Anyfare pays any x402 or MPP API for you. Put https://pay.anyfare.agents.bakingbad.dev/ in front of the API's link, without https://, and call it with a payment key. You pay the API's price and only for a 2xx answer. The full reference, every endpoint and error: https://anyfare.agents.bakingbad.dev/llms-full.txt

Rules, always:

- Ask the user before you sign up, make a key, top up, or spend above a limit.
- Keep the master key in anyfare-account.json, readable only by the user. Never print it, paste it into a chat, put it in a URL, or log it.
- Call with a payment key (ANYFARE_PAYMENT_KEY), never the master key (ANYFARE_MASTER_KEY).
- Never raise a limit on your own.

## 1. Check the link

Free, no key, and it pays nothing. Do it before the first call to any API.

```sh
curl "https://pay.anyfare.agents.bakingbad.dev/_/check?url=pro-api.coinmarketcap.com/x402/v3/cryptocurrency/quotes/latest"
```

"payable": true comes with "price" (what the API asks) and "quote" (what you pay). If the quote is above what the user allowed, ask. "payable": false has a "reason" and a "message": use another API.

## 2. Find a key, or sign up

Look for ANYFARE_PAYMENT_KEY in the environment, or anyfare-account.json in the working directory. If you have a payment key, go to step 4.

With no key at all, ask the user: "I need an Anyfare account to pay for this. May I sign up? A new account may get free Credits to try." If they agree:

```sh
umask 077
curl -s -X POST https://api.anyfare.agents.bakingbad.dev/v1/accounts > anyfare-account.json
```

The file holds "masterKey" and "freeCredits", the free Credits of this sign-up in USD ("0" when none). Tell the user what the account got. Tell them the master key is in anyfare-account.json, and that a lost master key can't be recovered, so they should keep a copy. Do not print the file.

## 3. Make a payment key with limits

Agree the limits with the user first: a daily budget (USD per UTC day), a highest price per call, and if they want, an allowance (USD in all) and an end time. Then:

```sh
ANYFARE_MASTER_KEY=$(jq -r .masterKey anyfare-account.json)
curl -X POST https://api.anyfare.agents.bakingbad.dev/v1/keys \
  -H "Authorization: Bearer $ANYFARE_MASTER_KEY" \
  -H "Content-Type: application/json" \
  -d '{"label":"research","dailyBudget":"5","maxPrice":"0.05"}'
```

The answer's "key" is shown once. Store it as ANYFARE_PAYMENT_KEY in a file only the user can read, and keep "id" as ANYFARE_KEY_ID. Do not print the key. Change limits with the user's leave:

```sh
curl -X PATCH "https://api.anyfare.agents.bakingbad.dev/v1/keys/$ANYFARE_KEY_ID" \
  -H "Authorization: Bearer $ANYFARE_MASTER_KEY" \
  -H "Content-Type: application/json" \
  -d '{"dailyBudget":"10","maxPrice":null}'
```

Revoke a key the user no longer wants:

```sh
curl -X DELETE "https://api.anyfare.agents.bakingbad.dev/v1/keys/$ANYFARE_KEY_ID" \
  -H "Authorization: Bearer $ANYFARE_MASTER_KEY"
```

## 4. Call

```sh
curl -i "https://pay.anyfare.agents.bakingbad.dev/pro-api.coinmarketcap.com/x402/v3/cryptocurrency/quotes/latest?symbol=BTC&convert=USD" \
  -H "Authorization: Bearer $ANYFARE_PAYMENT_KEY"
```

A 2xx answer is yours and costs the price. It carries Anyfare-Receipt: id="pay_…", amount="0.01", currency="USD". Any other answer costs nothing. Pass the API's query, body and headers as you would to the API itself.

## 5. Read the receipt and what is left

Set ANYFARE_CALL_ID to the id in the receipt header.

```sh
curl "https://api.anyfare.agents.bakingbad.dev/v1/calls/$ANYFARE_CALL_ID"
```

How much the key may still spend, and the balance:

```sh
curl https://pay.anyfare.agents.bakingbad.dev/_/key \
  -H "Authorization: Bearer $ANYFARE_PAYMENT_KEY"
```

"remaining.dailyBudget", "remaining.allowance" (null when there is no limit) and "remaining.balance" are in USD. Tell the user when the balance is low.

## 6. When a call fails

Every error is {"error": {"code", "message"}}.

- 402 insufficient_balance: the balance does not cover the price. Ask the user to top up (step 7).
- 402 key_budget_exceeded: the daily budget is spent. It resets at 00:00 UTC. Ask the user before you raise it.
- 402 key_allowance_exceeded: the key's allowance is spent. Ask for a new key or a higher allowance.
- 402 key_price_limit: the price is above the key's highest price per call. Ask before you raise it.
- 401 invalid_key: the key is revoked, expired, or wrong. Ask the user for a new one.
- 401 wrong_key_type: you sent the master key to the pay host, or a payment key to the API host.
- 400 host_not_allowed, 400 not_payable: Anyfare can't route to this API. Use another.
- 429 rate_limited: wait Retry-After seconds, then send it again once.
- 502 or 503: nothing was charged. Retry once after a few seconds. If it fails again, tell the user.

## 7. Top up (ask the user first)

The user pays; you only ask for an address. List what a top-up takes, then ask for an address and tell the user where to send:

```sh
curl https://api.anyfare.agents.bakingbad.dev/v1/topups/options
```

```sh
curl -X POST https://api.anyfare.agents.bakingbad.dev/v1/topups \
  -H "Authorization: Bearer $ANYFARE_MASTER_KEY" \
  -H "Content-Type: application/json" \
  -d '{"network":"base","token":"USDC","amount":"20"}'
```

Tell the user to send at least "sendAtLeast" of "token" on "network" to "depositAddress" (with "depositMemo" when it is not null) before "payBy". Then read it until "status" is "credited", or a final state ("expired", "failed", "refunded", "quote_failed", "quote_unknown"):

```sh
curl "https://api.anyfare.agents.bakingbad.dev/v1/topups/$ANYFARE_TOPUP_ID" \
  -H "Authorization: Bearer $ANYFARE_MASTER_KEY"
```

The people's page: https://console.anyfare.agents.bakingbad.dev/topup. Keep it for a user who prefers to top up by hand.

## 8. Without an account

An agent with its own wallet needs no account. A call with no payment answers 402 with the ways to pay: x402 on NEAR, Base or Solana, and MPP on Tempo. How to pay each: https://anyfare.agents.bakingbad.dev/llms-full.txt

## More

- Short guide: https://anyfare.agents.bakingbad.dev/llms.txt
- OpenAPI: https://api.anyfare.agents.bakingbad.dev/openapi.json and https://pay.anyfare.agents.bakingbad.dev/openapi.json
- The calls and services others paid, with a Markdown twin: https://anyfare.agents.bakingbad.dev/services.md
- The API's base: https://api.anyfare.agents.bakingbad.dev
