# Build with a coding agent

> Point a coding agent at the raw docs and the OpenAPI spec, give it the rules, and test what it builds without moving funds.

These docs are written so that a coding agent, such as Claude Code, Codex or Cursor, can build an integration from them alone. What it needs is all plain text:

| File | What it holds |
|---|---|
| [`/llms.txt`](/llms.txt) | The rules an integration must follow, and a link to every page's Markdown copy. Start here. |
| [`/_llms-txt/khalani-api.txt`](/_llms-txt/khalani-api.txt) | Every get-started, guide and concept page for the API, in one file. |
| [`/openapi/v3.json`](/openapi/v3.json) | The OpenAPI 3.1 contract: every route, field, status and example. |
| `/<page>.md` | Any page as Markdown: add `.md` to its path, such as [`/guides/fund-orders.md`](/guides/fund-orders.md). |

## Have it read the files raw

Tell your agent to download these files, for example with `curl`, rather than through a web-browsing or fetch tool. Many such tools summarize a page before the agent sees it, and a summary drops exact values from tables and code: an error code, a field name, an amount in base units. An agent working from a summary writes plausible code against an API that doesn't exist.

## A prompt to start from

Paste this into your agent, and add your own stack and where the code goes:

```text
Build a backend integration with the Khalani API v3 (https://api.hyperstream.dev/v3).

Read these with curl, raw, before writing code. Don't use a summarizing fetch tool:
- https://docs.khalani.network/llms.txt  (the integration rules: follow every one)
- https://docs.khalani.network/_llms-txt/khalani-api.txt  (the guides)
- https://docs.khalani.network/openapi/v3.json  (the contract; generate types from it)

Build: fetch the catalog, quote a trade, create an order from an option the
user's wallet can fund, return the funding steps to the frontend, poll the
order until summary.pollAfterSeconds is null, and relay a cancel signed by
refundTo. Read the API key from the KHALANI_API_KEY environment variable;
it must never reach the browser.

Handle every error by its `recovery` field, not its `code`. Treat open lists
as open. Send API-Revision: 0 on every request. Write tests that mock the
API and the wallet; never send a real deposit from a test.
```

## Generate types

Generate TypeScript types from the contract, so the compiler checks field names:

```bash
npx openapi-typescript@7.13.0 https://docs.khalani.network/openapi/v3.json -o khalani-v3.d.ts
```

Each named schema, such as `Order`, `QuoteResponse` and `ApiError`, becomes `components["schemas"]["Order"]`. Open lists come through as their known values or any string, so a value added later still type-checks.

## Test without moving funds

Most of an integration can be exercised for free:

- `GET /v3/catalog` and `POST /v3/quote` create nothing.
- `POST /v3/orders` creates an order, but nothing moves until the user funds it. An order you never fund expires at no cost. It does count against your [rate limits](/guides/rate-limits/).
- Errors are easy to provoke: a 1-unit quote returns `422 AMOUNT_TOO_SMALL`, and a second `POST /v3/orders` with the same option and other accounts returns `409 QUOTE_USED`.

Run one real order with a small amount, as the [quickstart](/quickstart/) does, before you go live. There is no test environment: every call reaches production.

## Review what it wrote

Check these by hand. They are where generated integrations go wrong:

- The API key is read on the backend only, and never logged.
- Asset ids come from the catalog, and amounts are base-unit strings converted with each asset's `decimals`, never floats.
- The option `id` is sent to `POST /v3/orders` unchanged.
- An option whose `requires` holds a value the code doesn't know is skipped.
- On an EVM source, the deposit is sent from the `payer`, after the [allowance check](/guides/fund-orders/#check-the-allowance-first), with the step's `value` and `gasLimit`.
- On a non-EVM source, exactly the transfer step's `amount` goes to its `to` address as given, once.
- Polling follows `summary.pollAfterSeconds` and stops when it is null. It doesn't stop at `delivering`.
- Errors are handled by `recovery`. A request that failed with `retry` is resent unchanged.
- Response parsing ignores unknown fields.
