# Idempotency & price guards

Retry purchases without double charges and make sure you never pay more than you agreed to, using idempotency keys, expected-price fields and the quote-then-order pattern.

- Canonical: https://fineproxy.org/developers/docs/idempotency/

Requests that spend money must be safe to retry and must never charge more than you expect. FineProxy gives you two tools for that: **idempotency keys**, which make a retry return the original result instead of buying again, and **price guards**, which reject a purchase when the actual price differs from the price you confirmed.

## Why you need both

- **Networks fail.** A request can time out after the server has already charged the wallet. Without an idempotency key, a retry would buy a second service.
- **Prices move.** Quotes are non-binding (`"binding": false`): stock, volume tiers and discounts can change between the quote and the order. A price guard makes the order fail instead of charging a different amount.

When a guard rejects a request, nothing is charged.

## Money operations at a glance

| Operation | Idempotency | Price guard |
| --- | --- | --- |
| MCP `order_composite`, `order_dedicated` | `idempotency_key` (required) | `expected_total_cents` |
| MCP `add_ips` | `idempotency_key` (required) | `expected_charge_cents` |
| MCP `refresh_ips` | `idempotency_key` (required) | `expected_amount_cents` (`0` = expected free) |
| MCP `purchase_addon` | `idempotency_key` (required) | `expected_price_cents` (required) |
| REST `POST /api/v1/customer/services` (catalog products such as rotating plans) | `params.client_idempotency_key` | `expected_price_cents` |
| REST `POST /api/v1/billing/composite/order` | `idempotency_key` | **None** — see [below](#rest-composite-orders) |
| REST `POST /api/v1/billing/services/{service_id}/add-ips` | `idempotency_key` (required) | `expected_charge_cents` |
| REST `POST /api/v1/billing/services/{service_id}/addons` | `idempotency_key` (required) | `expected_price_cents` |
| REST `POST /api/v1/customer/services/{service_id}/proxy/refresh` | `idempotency_key` (required) | — (preview the price with `POST .../proxy/refresh/quote`) |
| REST `POST /api/v1/customer/services/{service_id}/renew` | `idempotency_key` in the body or the `Idempotency-Key` header | — |
| REST `POST /api/v1/customer/wallet/deposit/init`, MCP `init_deposit` | `idempotency_key` | — (a deposit only creates a checkout link) |

All guard values are integer cents. Exact parameters are in the [API reference](/developers/docs/api/orders/) and the [MCP tools reference](/developers/docs/mcp/tools/).

## Quote, then order

Every safe purchase follows the same three steps:

1. **Quote.** Ask for the price of exactly what you want to buy.
2. **Confirm.** Compare the quoted total with your budget (or show it to a human) and check the wallet balance.
3. **Order** with the same parameters, a fresh idempotency key and the quoted total as the price guard.

For a composite datacenter order through MCP, the quote is `quote_composite`:

```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "tools/call",
  "params": {
    "name": "quote_composite",
    "arguments": { "segments": [{ "country": "DE", "count": 100 }], "period_months": 1 }
  }
}
```

The result contains the price in cents — 100 IPs × 1 month = 1200 cents ($12.00):

```json
{
  "binding": false,
  "segments": [{ "country_code": "DE", "count": 100, "unit_base_cents": 12, "segment_price_cents": 1200, "available": true }],
  "errors": [],
  "total_ip": 100,
  "total_price_cents": 1200,
  "monthly_price_cents": 1200,
  "period_months": 1
}
```

Then order with the same segments and pass `total_price_cents` as `expected_total_cents`:

```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "order_composite",
    "arguments": {
      "segments": [{ "country": "DE", "count": 100 }],
      "period_months": 1,
      "whitelist_ips": ["203.0.113.10"],
      "idempotency_key": "order-7f3c2a9e-5b1d-4c8e-9a0f-2d6b8e4c1a37",
      "expected_total_cents": 1200,
      "auto_renew": false
    }
  }
}
```

`whitelist_ips` must contain at least one IPv4 address: the machine that will connect to the proxies. See the [buy proxies guide](/developers/docs/guides/buy-proxies/) for the full flow.

## MCP price guards

`order_composite` and `order_dedicated` compare `expected_total_cents` with a fresh server-side quote before charging:

- **Match** — the order is placed and the wallet is debited by exactly that amount.
- **Mismatch** — the tool returns `isError: true` and nothing is charged:

```text
Invalid arguments: Price mismatch: expected_total_cents=1199, actual quote is 1200 cents. Nothing was charged.
```

On a mismatch, quote again, confirm the new price and order with the new value. `expected_total_cents` is optional in the tool schema, but always send it — it is what protects you from a price change between quote and order.

The other money tools work the same way: `add_ips` checks `expected_charge_cents`, `refresh_ips` checks `expected_amount_cents` and `purchase_addon` checks `expected_price_cents`.

## MCP idempotency keys

Every MCP money tool requires an `idempotency_key` that identifies one purchase intent:

- **Same key again** — returns the original result without a second charge. For `order_composite`, a replay returns the same `service_id` with `"balance_applied_cents": 0`, and the wallet balance does not change.
- **New key** — a new purchase, even if the parameters are identical to an earlier one.

So: one key per thing you want to buy, reused for every retry of that purchase.

## REST rotating plans and catalog products

`POST /api/v1/customer/services` buys a fixed catalog product, such as a rotating proxy plan. Pass the product price from the catalog as `expected_price_cents`:

```bash
curl -X POST https://fineproxy.org/account_new/api/v1/customer/services \
  -H "Authorization: Bearer $FINEPROXY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "product_id": "00000000-0000-0000-0000-000000000000",
    "expected_price_cents": 3500,
    "auto_renew": false,
    "params": { "client_idempotency_key": "rotating-2026-11-27-0001" }
  }'
```

- For billing-only products, `expected_price_cents` is required: a mismatch or a missing value returns `409` before any wallet debit.
- `params` accepts only `whitelist_ips`, `billing_period_months` and `client_idempotency_key`; any other key returns `422` before any debit.
- Repeating the call with the same product and params returns the existing service without charging again.

A successful purchase returns `201 Created` with the new `service`.

## REST composite orders

`POST /api/v1/billing/composite/order` (datacenter and ISP mixes) has **no price-guard field**. The request schema is strict, so adding `expected_total_cents` does not protect you — it is rejected with `422 {"detail": "Validation error"}` and nothing is charged.

The endpoint does support `idempotency_key`:

- A new key buys a new service, even if an identical one exists.
- The same key replays the service it created, without a second charge.
- The same key with a different order returns `409 IDEMPOTENCY_KEY_REUSED`.
- Without a key, an identical live order (same owner and segments) returns the existing service instead of charging again.

To stay safe with REST composite orders:

1. Call `POST /api/v1/billing/composite/quote` **immediately** before ordering, with exactly the same `segments`, `period_months` and options.
2. Refuse to order if `total_price_cents` exceeds your budget or if `errors` is not empty.
3. Send the order with a fresh `idempotency_key` and the same parameters. Set `"use_balance": true` to pay from the wallet; check `payment_required` and `amount_due_cents` in the response.
4. Compare `total_price_cents` in the order response with the quote, and alert on any difference.

> **Important:** If you need a hard guarantee that you never pay more than the quoted total, place composite orders through the MCP tool [`order_composite`](/developers/docs/mcp/tools/#order_composite) with `expected_total_cents`.

## Generate idempotency keys

- Use a random UUID per purchase intent, optionally with a readable prefix: `order-7f3c2a9e-...`.
- Generate the key **once**, when you decide to buy, and store it with your own order record **before** sending the request. A key that exists only in memory is lost on a crash, and the retry would become a new purchase.
- Keep keys within 128 characters.
- Never reuse a key for a different purchase.

```javascript
import { randomUUID } from 'node:crypto';

const idempotencyKey = `order-${randomUUID()}`;
```

```python
import uuid

idempotency_key = f"order-{uuid.uuid4()}"
```

## Retries and timeouts

| What happened | What to do |
| --- | --- |
| Timeout or network error, no response | Retry with the **same** key and the same body. You get either the original result or a fresh, single execution. |
| `429` or `503` | Back off and retry with the same key. See [Rate limits](/developers/docs/rate-limits/). |
| Price mismatch (MCP `isError`, REST `409`) | Nothing was charged. Quote again, confirm, and order with the new price. |
| `402` insufficient funds | Nothing was charged. Top up, then retry with the same key. |
| `409 IDEMPOTENCY_KEY_REUSED` | You reused a key for a different order. Check your key generation; use a new key only if this really is a new purchase. |
| `422` validation error | Fix the request. Nothing was charged. |

After an uncertain outcome, you can also check what happened before retrying: list your services (`GET /api/v1/customer/services`) and the wallet ledger (`GET /api/v1/customer/wallet/ledger`), where every debit appears with its `service_id`.

## Next steps

- [Buy proxies with the API](/developers/docs/guides/buy-proxies/) — the complete purchase flow.
- [Errors](/developers/docs/errors/) — status codes and error bodies.
- [Authentication](/developers/docs/authentication/#scopes) — limit `wallet:spend` to the keys that need it.
