# BazaarLink Top-up - Developer Skill

> Load this skill to let your AI assistant check a BazaarLink balance and add credits with x402.
> Money moves here: read the safety rules first and follow them every time.

- **Base URL:** `https://api.bazaarlink.ai`
- **Auth:** `Authorization: Bearer sk-bl-YOUR_API_KEY` (never print this value)
- **Full docs:** https://bazaarlink.ai/docs/integrations/x402
- **Main API skill:** https://bazaarlink.ai/skill.md

---

## Safety rules (read first)

1. **Never show the key.** Do not print, log, paste or store the API key (`sk-bl-...`) or any wallet private key in chat, files, commit messages or tool output. Read it from an environment variable or the user's secret store.
2. **Signing and paying need the user's explicit confirmation, every time.** Before anything is signed or sent with `X-PAYMENT`, tell the user the credits to add, the total to pay, the network, the asset and the recipient (`payTo`) taken from the 402 response, and wait for a clear yes in this conversation. An earlier yes for another amount does not count.
3. **Never change the amount, network, asset or recipient on your own.** Use exactly what the user approved. If the 402 response differs from what the user approved (another amount, network, asset or recipient), stop and ask again.
4. **The user's wallet signs, not you.** Do not read a private key to sign on the user's behalf unless the user gave you a signing tool for this purpose. If you cannot sign, show the user what to sign and ask them to do it.
5. **After a timeout or a 5xx, check the original order first.** Read the balance (section 1) and compare it with the balance from before. Do not create a new signature before you know whether the first payment went through.
6. **Never pay twice.** A signed payment can be used once; to retry, resend the SAME signed payload, not a new signature. Top-up: resending a payment that was already credited returns 200 with status already_credited.
7. **Payments settle on-chain and cannot be reversed.** Only top up when the user asked for it, and never to "test".

---

## 1. Check the balance

```bash
curl https://api.bazaarlink.ai/v1/credits \
  -H "Authorization: Bearer $BAZAARLINK_API_KEY"
```

The answer is `{ "data": { "total_credits": ..., "total_usage": ... } }`: `total_credits` is the remaining balance in US dollars, `total_usage` the lifetime spend. An invalid or disabled key answers 401.

## 2. Read the limits (no key needed)

GET /api/x402/topup returns minUsd, maxUsd and network, markupRate, flatFeeUsd without authentication. maxUsd is null when there is no upper limit.

```bash
curl https://api.bazaarlink.ai/api/x402/topup
```

Example answer (the numbers are placeholders: read the real ones from the live response, do not assume them):

```json
{ "minUsd": 1, "maxUsd": 50, "network": "eip155:196", "markupRate": 0.1, "flatFeeUsd": 0 }
```

## 3. Top up an existing balance

POST /api/x402/topup credits the account that owns the API key or the signed-in session. The 402 flow is the same and the body only has amountUsd. amountUsd is the credits to add; the payment also includes the service fee.

Authenticate with Authorization: Bearer plus your API key, or use a signed-in browser session.

The 402 flow, three steps:

1. Send the request without a payment. The server answers 402 Payment Required with a JSON body: x402Version, resource, and accepts (network, asset, amount, payTo, maxTimeoutSeconds).
2. Sign a payment authorization for one entry in accepts with your own wallet key.
3. Send the same request again with the base64-encoded payment payload in the X-PAYMENT header. The header name payment-signature is accepted as an alias.

Take network, asset and amount from the 402 response instead of hard-coding them. amount is in the asset's smallest unit (6 decimals for USDT0). Send the same JSON body in both requests.

Step 1: ask without paying (answer: 402 with the requirements; nothing is charged):

```bash
curl -i -X POST https://api.bazaarlink.ai/api/x402/topup \
  -H "Authorization: Bearer $BAZAARLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"amountUsd":1}'
```

The requirements in the 402 body look like this (addresses and amounts are placeholders). Show the user the real ones and ask for confirmation (safety rules 2 and 3) before step 2:

```json
{
  "x402Version": 2,
  "resource": {
    "url": "https://api.bazaarlink.ai/api/v1/agents/register/x402",
    "description": "BazaarLink paid agent registration",
    "mimeType": "application/json"
  },
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:196",
      "payTo": "0xPAY_TO_ADDRESS",
      "asset": "0xASSET_CONTRACT",
      "amount": "1100000",
      "maxTimeoutSeconds": 300,
      "extra": { "name": "USD₮0", "version": "1" }
    }
  ]
}
```

Step 3: resend the same request with the signed payment:

```bash
curl -i -X POST https://api.bazaarlink.ai/api/x402/topup \
  -H "Authorization: Bearer $BAZAARLINK_API_KEY" \
  -H "Content-Type: application/json" \
  -H "X-PAYMENT: <base64 payment payload>" \
  -d '{"amountUsd":1}'
```

A successful top-up returns 200 with status credited, the new balance in US dollars, the txHash, and paidUsd, creditedUsd and serviceFeeUsd (what you paid, what was credited, and the platform top-up fee).

```http
HTTP/1.1 200 OK
Content-Type: application/json

{
  "status": "credited",
  "balance": 12.5,
  "txHash": "0xTX_HASH",
  "paidUsd": 1.1,
  "creditedUsd": 1,
  "serviceFeeUsd": 0.1
}
```

## 4. Register an agent account with x402 (when you have no key yet)

POST /api/v1/agents/register/x402 creates an agent account, an API key and an initial balance in one paid request. It needs no API key and no login.

- Agent name, 1 to 100 characters. (`name`)
- Optional description, up to 4000 characters. (`description`)
- Credits to add in US dollars, a positive number within the current limits. The 402 payment amount adds the service fee; read the total from the response. (`amountUsd`)

Unknown fields are rejected with 400 bad_request. The flow is the same three steps against `https://api.bazaarlink.ai/api/v1/agents/register/x402` (ask, the user's wallet signs, resend).

```bash
curl -i -X POST https://api.bazaarlink.ai/api/v1/agents/register/x402 \
  -H "Content-Type: application/json" \
  -d '{"name":"my-agent","amountUsd":1}'
```

```bash
curl -i -X POST https://api.bazaarlink.ai/api/v1/agents/register/x402 \
  -H "Content-Type: application/json" \
  -H "X-PAYMENT: <base64 payment payload>" \
  -d '{"name":"my-agent","amountUsd":1}'
```

A successful registration returns 201 with these fields. The API key appears only here, so store it right away.

```json
{
  "api_key": "sk-bl-...",
  "credits": 1,
  "credits_usd": "$1.0000",
  "claim_token": "...",
  "claim_expires": "2026-10-08T00:00:00.000Z",
  "upgrade_url": "https://api.bazaarlink.ai/claim?token=...",
  "referral_code": "...",
  "free_model": "auto:free",
  "message": "...",
  "referral_message": "...",
  "base_url": "https://api.bazaarlink.ai/api/v1",
  "docs": "https://api.bazaarlink.ai/llms.txt",
  "payment": {
    "txHash": "0xTX_HASH",
    "network": "eip155:196",
    "amountUsd": 1.1,
    "paidUsd": 1.1,
    "creditedUsd": 1,
    "serviceFeeUsd": 0.1
  }
}
```

Store the new `api_key` in a secret store right away and never print it (safety rule 1).

## 5. Errors

The x402 routes return errors as { "error": { "message", "code" } }. Two responses differ: 410 uses { "error", "code" } and 429 uses { "error" } with a Retry-After header.

| HTTP | Code | Meaning |
|------|------|---------|
| 400 | `bad_request` | The body is not valid: wrong type, missing name, or an unknown field. |
| 401 | `unauthenticated` | Top-up only: no valid API key or session. |
| 402 | `(payment requirements body)` | No usable payment yet. The body holds the requirements, so sign and resend. It is also returned when the payment does not match the requirements or fails verification. |
| 402 | `payment_amount_too_low` | The settled amount is lower than the amount requested. |
| 403 | `geo_blocked` | x402 is not available from your region. |
| 410 | `agent_register_discontinued` | Agent registration is switched off. |
| 429 | `(Retry-After header)` | Too many registrations from the same network source. Wait for the seconds given in Retry-After. |
| 502 | `facilitator_error` | The payment could not be verified or settled. Try again later. |
| 502 | `settlement_error` | The payment was received but could not be credited. Resend the same payment instead of signing a new one. |
| 502 | `registration_error` | The payment was received but the account could not be created. Resend the same payment instead of signing a new one. |
| 503 | `disabled / runtime_disabled` | x402 is switched off or not configured right now. |
| 503 | `asset_unavailable / dedup_unavailable` | A dependency (asset configuration, duplicate check or top-up check) is unavailable. Try again later. |
| 503 | `signup_disabled / agent_register_unavailable` | Registration is temporarily disabled or unavailable. |

A signed authorization can be used once. The server recognizes a repeated payment before it contacts the payment network, so a retry never charges you twice. Registration: for 15 minutes the same signed payment returns the same 201 response, API key included, so a network failure can be retried safely. After that the response is 200 with status already_registered and the payment txHash, and the API key is not shown again.
