> For the complete documentation index, see [llms.txt](https://agentdex.gitbook.io/agentdex-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://agentdex.gitbook.io/agentdex-docs/developers/api-overview.md).

# API overview

AgentDex exposes three programmatic surfaces, all backed by the same matching engine and the same Keychain-issued credentials.

### Related pages

<table data-view="cards"><thead><tr><th>Title</th><th data-card-target data-type="content-ref">Target</th></tr></thead><tbody><tr><td>MCP server</td><td><a href="/spaces/aCY4JwuHvGKtRcc70Q10/pages/b04bd052a9ab52987fde240aa744f93802538b92">/spaces/aCY4JwuHvGKtRcc70Q10/pages/b04bd052a9ab52987fde240aa744f93802538b92</a></td></tr><tr><td>Rest API</td><td><a href="/spaces/aCY4JwuHvGKtRcc70Q10/pages/f52b0b8b12aaec7c2645ed2998e5c65f4d24dd53">/spaces/aCY4JwuHvGKtRcc70Q10/pages/f52b0b8b12aaec7c2645ed2998e5c65f4d24dd53</a></td></tr><tr><td>Websocket API</td><td><a href="/spaces/aCY4JwuHvGKtRcc70Q10/pages/253d147e97562c475d37f3d7938e22134cf09c42">/spaces/aCY4JwuHvGKtRcc70Q10/pages/253d147e97562c475d37f3d7938e22134cf09c42</a></td></tr></tbody></table>

| Surface       | Best for                                                     | Style                                                                                                                           |
| ------------- | ------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| **MCP**       | LLM agents, autonomous trading, prompt-driven workflows      | JSON-RPC, single endpoint, typed schemas. See [MCP server](/agentdex-docs/developers/mcp-server.md).                            |
| **REST**      | Synchronous integrations, traditional bots, server-to-server | HTTP POST/GET, request/response. See [Rest API](/agentdex-docs/developers/rest-api.md).                                         |
| **WebSocket** | Live data, fills, position updates, low-latency execution    | Persistent connection, channel subscriptions, snapshot + diff. See [Websocket API](/agentdex-docs/developers/websocket-api.md). |

You can mix them freely — open a WebSocket for state, place orders via MCP or REST.

### Endpoints

Base URL: `https://agentdex.xyz`.

MCP sits under `/mcp`, REST under `/v1/...`, WebSocket varies by surface (Market Data WS vs. Direct WS — see [Websocket API](/agentdex-docs/developers/websocket-api.md)).

### Authentication

Two paths into the same account:

| Mode             | Used by                          | Header                                                  |
| ---------------- | -------------------------------- | ------------------------------------------------------- |
| **User session** | Web UI, OAuth-style integrations | `Authorization: Bearer <access_token>` (JWT / Firebase) |
| **API key**      | Bots, agents, server-to-server   | `X-Api-Key: <Keychain-issued key>`                      |

Keys are scoped (`pub`, `ro`, `rw`) and `rw` keys can carry per-key trading limits. See [API keys](/agentdex-docs/developers/api-keys.md).

### Permission scopes

| Scope | Tools / endpoints exposed                                                                             |
| ----- | ----------------------------------------------------------------------------------------------------- |
| `pub` | Public data only — instruments, tickers, order book, exchange status. No key required.                |
| `ro`  | Public + your read-only data — balances, positions, orders, permissions.                              |
| `rw`  | Public + read-only + trading — `place_order`, `change_leverage`, cancellations. Trading limits apply. |

MCP enforces this by **only exposing tools the key can call**. A `ro` key talking to the MCP server doesn't see `place_order` in its tools list — the LLM cannot even attempt unauthorised actions.

### Common conventions

* **Numbers as strings**: prices, amounts, balances are JSON strings to avoid float-precision loss. Parse to decimal in your client.
* **Quote-sized by default**: trading endpoints take `quoted_currency_volume` (USDT) unless you explicitly choose `base_currency_volume` or `contract_volume`.
* **Timestamps**: Unix milliseconds, UTC.
* **Instrument IDs**: `<BASE>_<QUOTE>_PERPETUAL`, e.g. `BTC_USDT_PERPETUAL`. Don't construct these yourself; pull from `get_instruments`.
* **Idempotency**: REST order placement is not idempotent today. If you need at-most-once semantics, dedupe on your side using an external nonce.

### Rate limits

Per-key limits are enforced server-side and refreshed at short intervals. Limits aren't published publicly because they tune over time; you'll see `429 Too Many Requests` or an equivalent error code when you hit them. If you operate at high request rates, contact the team for an appropriate tier.

### Error envelope

REST responses use a uniform envelope:

```json
{
  "status": "OK",
  "data": { ... }
}
```

Errors:

```json
{
  "status": "ERROR",
  "error": { "code": "INSUFFICIENT_FUNDS", "message": "...", "details": null }
}
```

Some `*-by-base-currency` / `*-by-quoted-currency` endpoints return the raw outbox event directly (no envelope). The pages for those endpoints flag this explicitly.

System-level failures (Kafka unreachable, internal timeout) return `HTTP 500` with an empty body.

The common error codes:

| Code                                    | When                                                                              |
| --------------------------------------- | --------------------------------------------------------------------------------- |
| `INVALID_REQUEST_DATA`                  | Malformed JSON or missing required fields.                                        |
| `UNAUTHORIZED`                          | No valid session or API key.                                                      |
| `INVALID_INSTRUMENT_ID`                 | Instrument unknown or outside key's allowlist.                                    |
| `INSUFFICIENT_FUNDS`                    | Required margin + fee reserve exceeds `available`.                                |
| `INSTRUMENT_IS_NOT_ACTIVE`              | Instrument paused.                                                                |
| `IN_EMERGENCY_MODE`                     | Engine in emergency mode; see [Mark price](/agentdex-docs/trading/mark-price.md). |
| `AMOUNT_TOO_SMALL`                      | Below the instrument's minimum order size.                                        |
| `ORDER_VOLUME_MISMATCHES_CONTRACT_SIZE` | Volume not a multiple of contract size (contract-sized mode).                     |

### Picking a surface

Start with MCP if you're driving from an LLM. Start with REST if you have an existing exchange-client codebase to port. Layer in WebSocket once you need to react to fills and price moves below the request-response loop.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://agentdex.gitbook.io/agentdex-docs/developers/api-overview.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
