> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cbpayapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Fee-aware settlement and insufficient funds

> Configure USD-canonical fees, understand standalone debits, and handle enriched 402 responses

Same API on both environments — build against test first, then go live by swapping the base URL and the key. [Environments and testing →](https://docs.cbpayapp.com/en/environment-testing.md)

This guide explains the three fee contracts that integrators must implement:

* fee configuration is **USD-canonical**;
* only standalone service fees may try the configured debit order;
* money-out operations require the full settlement amount in one balance.

The API remains provider-neutral. Amounts are decimal strings; never use
floating point arithmetic for money.

For the complete normalized error catalog, see [Errors](https://docs.cbpayapp.com/en/errors.md).

## USD-canonical pricing

For fee services that use percentage and fixed components, configure both
components in **USD**. The fixed value is expressed in USD cents by the
admin surface and returned as a decimal USD amount. A fee row with `asset:
"USDT"` or `asset: "USDC"` is rejected with `400 invalid_asset` for these
services. Legacy rows may remain visible during migration, but the effective
view and new configuration are USD-canonical.

The fee currency is separate from the balance used to settle an operation:
the fee is resolved in USD, while the operation's settlement asset may be
USD, USDT, USDC or a supported volatile asset. Spreads keep their asset
dimension and are not converted into fee rows.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://api.qbank.cl/platform/v1/rates" \
  -H "Authorization: Bearer <token>"
```

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "base": "USD",
  "fees": [
    {
      "service": "payout",
      "country": "CL",
      "asset": "USD",
      "percent": "1.50",
      "fixed_amount": "0.30",
      "status": "active"
    }
  ],
  "settlement": {
    "default_asset": "USD",
    "assets": [
      { "asset": "USD", "available": true, "settlement_rate": "1" },
      { "asset": "USDT", "available": true, "settlement_rate": "1" }
    ]
  }
}
```

For an operation amount of USD 100.00, the fee is
`ceil(100.00 × 1.50%) + 0.30 = 1.80 USD`. The effective fee view is the
source of truth; do not infer fees from a legacy asset label.

## Debit rules by operation

| Surface | Debit rule | What happens when the balance is short |
| - | - | - |
| Payout | Debit 100% of the selected settlement amount from its settlement asset | `402 insufficient_funds`; no payout is created |
| Crypto withdrawal | Debit amount plus the exact service fee from the withdrawal asset | `402`; `details` may include the amount/fee split |
| Internal transfer | Debit 100% of the transfer amount from one asset | `402`; no partial transfer |
| Banking fiat transfer | Debit the corridor balance plus its exact rail fee | `402`; no partial transfer |
| Standalone fee | Try the configured order from one account balance | `402` if no candidate can cover the complete fee |

The standalone debit order is deterministic. It starts with the account's
configured default, then tries `USD`, `USDT`, and `USDC`, removing duplicates.
An invalid or disabled default is skipped. Every candidate is advisory-only
until the atomic ledger charge succeeds.

Payouts do **not** use this cascade. They must be fully funded by the selected
settlement asset. A race between the advisory check and the ledger charge is
expected to lose atomically and return the same 402 response; do not retry by
creating a new operation.

```mermaid theme={"theme":{"light":"github-light","dark":"github-dark"}}
sequenceDiagram
  participant App as Integrator
  participant API as CBPay API
  participant Ledger as Atomic ledger
  App->>API: POST money-out with idempotency_key
  API->>API: Advisory balance check
  API->>Ledger: Atomic full debit
  alt Balance covers amount + fee
    Ledger-->>API: Debit accepted
    API-->>App: 202/201 operation
  else Balance is short or race lost
    Ledger-->>API: Insufficient funds
    API-->>App: 402 + optional details
  end
```

## The fee-aware 402 response

The historical envelope remains stable:

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USDT",
    "need": "103.930000",
    "available": "80.000000",
    "missing": "23.930000",
    "need_amount": "100.000000",
    "need_fee": "3.930000",
    "need_usd": "103.930000",
    "available_usd": "80.000000",
    "missing_usd": "23.930000",
    "need_amount_usd": "100.000000",
    "need_fee_usd": "3.930000"
  }
}
```

`details` is optional. Its base keys are `asset`, `need`, `available` and
`missing`, formatted in the debited asset. `need_amount` and `need_fee` are
present only when the server knows the exact split. USD twin keys are present
only for USD, USDT and USDC, whose value is exactly 1:1 here. They are not
present for BTC, GOLD, SILVER, PLATINUM, RWA assets or unknown assets.

If the numbers are unavailable, the server returns the compatible historical
body without `details`. Treat an absent block as a valid response.

### Payout

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USD",
    "need": "86.01",
    "available": "80.00",
    "missing": "6.01",
    "need_usd": "86.01",
    "available_usd": "80.00",
    "missing_usd": "6.01"
  }
}
```

The payout fee is included in the exact total, but no amount/fee split is
reported when that split is not attributable independently of settlement
pricing.

### Crypto withdrawal

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USDT",
    "need": "103.930000",
    "available": "100.000000",
    "missing": "3.930000",
    "need_amount": "100.000000",
    "need_fee": "3.930000",
    "need_usd": "103.930000",
    "available_usd": "100.000000",
    "missing_usd": "3.930000",
    "need_amount_usd": "100.000000",
    "need_fee_usd": "3.930000"
  }
}
```

### Internal transfer

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "BTC",
    "need": "0.00150000",
    "available": "0.00100000",
    "missing": "0.00050000"
  }
}
```

No USD twins are emitted for BTC. The transfer is not partially created.

### Standalone service fee

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "USDC",
    "need": "12.000000",
    "available": "5.000000",
    "missing": "7.000000",
    "need_usd": "12.000000",
    "available_usd": "5.000000",
    "missing_usd": "7.000000"
  }
}
```

The charge is idempotent. A failed candidate does not create a partial fee,
and a later retry must use the same operation's idempotency key.

### Banking fiat transfer

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": "insufficient_funds",
  "message": "account balance is not enough for this operation",
  "details": {
    "asset": "BANK_USD",
    "need": "250.50",
    "available": "250.00",
    "missing": "0.50",
    "need_amount": "250.00",
    "need_fee": "0.50"
  }
}
```

Banking rail fees are exact in the operation currency. Banking assets do not
receive USD twins unless the API explicitly identifies the asset as USD,
USDT or USDC.

## Cartola and metadata

Successful standalone charges appear as a debit movement with the service
reference and the asset actually charged. The asset actually charged and its
rate are recorded in the movement metadata; skipped candidates are
operational logs only. A skipped candidate is not a charge and must not be
reconciled as money movement.
Payouts show one debit in the selected settlement asset and never carry
standalone waterfall metadata.

Read movements with a bounded date range:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://api.qbank.cl/platform/v1/movements?from=2026-11-29&to=2026-11-29&page=1&page_size=50" \
  -H "Authorization: Bearer <token>"
```

## Safe retry and states

| Response/state | Action |
| - | - |
| `402 insufficient_funds` | Add funds or select a funded settlement asset, then retry the same request with the same idempotency key |
| `202 processing` / `pending` | Read the resource and wait; do not create a replacement |
| `completed` | Reconcile the final debit and movement |
| `failed` | Read the resource; any post-debit refund is recorded separately |

Never re-send or re-enqueue a transaction after a timeout or 5xx. Read its
status and let the client retry with the same key when appropriate.

## FAQ

> [accordion] Can I configure a fee in USDT?
>
> No for USD-canonical percentage/fixed services. Configure `asset: "USD"`.
> USDT and USDC rows are legacy transition data, not new configuration.

> [accordion] Will a payout use another balance when USD is short?
>
> No. A payout uses 100% of its selected settlement asset. Only standalone
> service fees use the default → USD → USDT → USDC candidate order.

> [accordion] Can I assume details is always present on a 402?
>
> No. It is optional. Always handle the stable `error` and `message` envelope,
> then parse `details` only when present.

> [accordion] Should I create a new operation after a 402 race?
>
> No. The atomic ledger may reject an advisory check lost to a concurrent debit.
> Use the same idempotency key after the account is funded.

> [accordion] Where do I verify the effective fee?
>
> Call `GET /v1/rates`. Its `fees` array is the resolved account view and uses
> USD for canonical fee rows; `asset_prices` and `settlement` describe balances
> and spreads separately.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.