# Treasury: quote and execute

> Show a locked rate to an approver, execute it within its life and handle expiry, terms and limits without surprises.

[Sandbox · coming soon]

A treasury desk wants a person to approve every conversion while seeing the exact rate, and the system to execute it only if the approval arrives in time.

## The flow

1. The system quotes and shows `factor`, both amounts and a countdown to `expiresAt`.
2. The person approves.
3. The system executes with an `Idempotency-Key` tied to the approval.
4. If the quote expired, it quotes again and asks to approve the new rate.

## 1. Quote and compute before executing

Compute the other side with integers, like zulu does, to show exactly what will be executed:

```js title="preview.js"
const f2 = (factor) => {
  const [whole, fraction = ''] = String(factor).split('.');
  return BigInt(whole) * 100n + BigInt(fraction.padEnd(2, '0'));
};
const roundDiv = (n, d) => (n * 2n + d) / (d * 2n);

// COP → USD by origin amount: cents = round(pesos × 10⁴ / f₂)
export const previewCopToUsdCents = (pesos, factor) => roundDiv(BigInt(pesos) * 10_000n, f2(factor));
```

## 2. Pick the term

| If you need the funds…   | Term         |
| ------------------------ | ------------ |
| Today, before the cutoff | `SAME_DAY`   |
| Tomorrow                 | `NEXT_DAY`   |
| In two business days     | `SECOND_DAY` |

| Limit | Value |
| --- | --- |
| `REAL` FX hours (business days, Bogotá) | 08:05 – 15:30 |
| Last time to quote `SAME_DAY` | 13:00 |

## 3. Execute exactly once

```bash
curl https://sandbox.zulu.com.co/v1/transactions \
  -H "Authorization: Bearer $ZULU_ACCESS_TOKEN" \
  -H "X-API-Key: $ZULU_API_KEY" \
  -H "Idempotency-Key: approval-7781" \
  -H "Content-Type: application/json" \
  -d '{"quote": "EQ-COPXUSDX-261124150405-SBX7Q", "originAmount": 4000000}'
```

If the network drops, retry with the same key: you get the same conversion, not two. A quote already used answers `422`.

## 4. Expiry and limits

| Rule | Operations |
| --- | --- |
| With f₂ = factor × 100: COP→USD, cents = round(pesos × 10⁴ / f₂); USD→COP, pesos = round(cents × f₂ / 10⁴). With `destinationAmount`, the origin rounds up. Both sides must stay within the limits. | [`createFxTransaction`](https://developers.zulu.com.co/en/reference/create-fx-transaction.md) |
| A quote lives 30 seconds and serves a single conversion. | [`getQuote`](https://developers.zulu.com.co/en/reference/get-quote.md), [`createFxTransaction`](https://developers.zulu.com.co/en/reference/create-fx-transaction.md) |

| HTTP | error | cause | Description |
| --- | --- | --- | --- |
| 422 | `UnprocessableEntityApiError` | `quote "%s" is expired` | The quote expired (it lives 30 seconds). Request a new one. |
| 422 | `UnprocessableEntityApiError` | `quote "%s" has already been used` | Each quote serves a single conversion. |
| 422 | `UnprocessableEntityApiError` | `origin or destination amount wasn't calculated with factor "%v"` | You sent `originAmount` and `destinationAmount` and they do not match the quote's factor. Send only one of them. |
| 422 | `UnprocessableEntityApiError` | `%s amount %.2f is below the minimum of %.2f` | One side of the conversion is below the minimum (`GET /v1/limits`). |
| 422 | `UnprocessableEntityApiError` | `%s amount %.2f is above the maximum of %.2f` | One side of the conversion exceeds the maximum (`GET /v1/limits`). |
| 422 | `UnprocessableEntityApiError` | `the fx market is closed` | The FX market is closed (`REAL` hours: 08:05–15:30 on Bogotá business days). The sandbox defaults to `ALWAYS_OPEN`. |
| 422 | `UnprocessableEntityApiError` | `SAME_DAY settlement is not available after %s` | After the cutoff `SAME_DAY` is not available; quote `NEXT_DAY` or `SECOND_DAY`. |
| 422 | `UnprocessableEntityApiError` | `idempotency key "%s" was used with a different request` | The `Idempotency-Key` was already used in the last 24 hours with a different body or operation. Use a new key for every distinct operation. |
