# Create orders

> Turn a quote option into an order, bind the right accounts, and retry creation safely.

`POST /v3/orders` turns one quote option into an order bound to the user's accounts. The answer is the order, with the steps that fund it. Nothing happens on-chain until the user runs those steps.

## The request

```bash
curl -sS https://api.hyperstream.dev/v3/orders \
  -H "X-API-Key: $KHALANI_API_KEY" -H "Content-Type: application/json" \
  -d '{
    "option": "opt_…",
    "accounts": {
      "payer": { "chain": "eip155:42161", "address": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4" },
      "recipient": { "chain": "eip155:8453", "address": "0x5B38Da6a701c568545dCfcB03FcB875f56beddC4" }
    }
  }'
```

- `option` is an option's `id` from `POST /v3/quote`, unchanged, sent before the quote's `expiresAt`.
- `accounts` binds an account to each role the option lists in `roles`. Each account is `{ chain, address }`, on the chain the role names.
- Leave out a role you leave to its default, or send it as `null`. Do the same for a role the option doesn't list. A `payer` on a non-EVM source is a `400 ACCOUNT_INVALID`.

The answer is `201` with the order. Every later call uses its `id` (`ord_…`), so store it before you hand anything to the wallet.

## Choose the accounts

| Role | Who it is | Rules |
|---|---|---|
| `payer` | Sends the deposit, on an EVM source. | Required on an EVM source, absent on a non-EVM source. The deposit must come from this account: a deposit from any other account can't fund the order, and has to be recovered through Khalani. |
| `recipient` | Receives the payout, on the destination chain. | Required. |
| `refundTo` | Receives any refund, on the source chain, and signs cancels. | Defaults to `payer` on an EVM source. Required on a non-EVM source. |

Bind accounts the user controls:

- **When early cancellation is available, `refundTo` must be able to sign messages**: an account one key controls, or on EVM a contract wallet that signs through EIP-1271, such as a Safe. Never an exchange deposit address, a token contract, or any other contract that can't sign. The refund would land there, and no one could cancel the order. The API can't tell these apart from a wallet, so it accepts them.
- **Take addresses from the user's connected wallet.** The addresses in these docs are placeholders or token contracts.
- **The recipient and `refundTo` must be able to receive.** The API checks this when it can, and answers `422 ACCOUNT_NOT_READY` or `422 ACCOUNT_NOT_ACTIVATED`. On EVM, a native coin is paid as a plain transfer, so a contract with no payable `receive` or `fallback` function can't receive one. Some non-EVM chains have their own rules: see [Notes for specific chains](/guides/supported-assets/#notes-for-specific-chains).
- **A smart-contract wallet doesn't have the same address on every chain.** Send `recipient` and `refundTo` explicitly, as accounts it controls on those chains.

## Retry creation safely

Sending the same request again never creates a second order, so retry it after a lost answer, a timeout, or an error whose `recovery` is `retry`:

- If creation finished, you get the order back with `200`, even after the quote expired.
- If the first request is still running and the funding window is open, you get `409 IDEMPOTENCY_IN_PROGRESS`. Send it again after `retryAfterSeconds`.
- If the first request reserved the option, a retry can finish creation with `201` while the original funding window is open.
- If the option is still available and the quote is valid, the retry can create the order as usual.

A repeat is the same request when it names the same option and the same accounts. Address formatting doesn't matter, and neither does sending `refundTo` as its default or leaving it out.

The same option with other accounts is a `409 QUOTE_USED`, whether its creation finished or not. Request a new quote. To find an order the first request made, list with `GET /v3/orders?account=<CAIP-10 id>`.

## Errors

| Answer | Meaning | What to do |
|---|---|---|
| `400 VALIDATION_FAILED` | The body doesn't match the schema, such as a missing `recipient`. `field` names it. | Fix the request. |
| `400 ROLE_MISSING` | `payer` is missing on an EVM source, or `refundTo` on a non-EVM source. `field` names it. | Add the account. |
| `400 ACCOUNT_INVALID` | An account is on the wrong chain, its address isn't valid there or is the zero address, or it names a role the option doesn't have. `field` names it. | Fix the account. |
| `400 OPTION_INVALID` | The option wasn't issued to your key, or was changed. | Request a new quote. |
| `400 UNSUPPORTED_PAIR` | The pair stopped trading since the quote. | Refresh the catalog and quote again. |
| `409 QUOTE_USED` | The option is reserved for other accounts. | Request a new quote. |
| `409 REQUOTE_REQUIRED` | Your fees or the order's deadlines changed since the quote. | Request a new quote. |
| `409 IDEMPOTENCY_IN_PROGRESS` | The same request is still running. | Send it again after `retryAfterSeconds`. |
| `422 AMOUNT_TOO_SMALL` | With the fees now, the deposit would publish or pay nothing. Its `recovery` is `requote`. | Request a new quote, which gives the size range. |
| `410 QUOTE_EXPIRED` | The quote or the order's funding window expired before creation finished. | Request a new quote. |
| `422 ACCOUNT_NOT_ACTIVATED` | The recipient or `refundTo` must be activated on its chain before it can be paid. | Activate it as the chain requires, then retry. See [Notes for specific chains](/guides/supported-assets/#notes-for-specific-chains). |
| `422 ACCOUNT_NOT_READY` | The recipient or `refundTo` can't take the payment, such as an EVM contract that rejects a native coin. | Make the account ready as the chain requires, or use another account. |
| `503 UPSTREAM_UNAVAILABLE` | A dependency can't be reached now. | Send the same request again after `retryAfterSeconds` or a backoff. |
| `503 MAINTENANCE` | Order creation is paused. | Retry after `retryAfterSeconds`. |

Every error says what to do in `recovery`, including codes not listed here. See [Errors and retries](/guides/errors/).

## Next

The order is `awaiting_funding`. [Fund it](/guides/fund-orders/) before `terms.fundBy`.
