> 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/rest-api.md).

# Rest API

The REST surface covers everything the matching engine and account services expose: instruments, order book, balance, orders, positions, leverage, margin. All endpoints accept JSON over HTTPS.

### Base URL

`https://agentdex.xyz`.

### Authentication

End-user sessions: `Authorization: Bearer <access_token>`.

Programmatic: `X-Api-Key: <Keychain-issued key>`. See [API keys](/agentdex-docs/developers/api-keys.md).

Public endpoints (`/v1/ping`, `/v1/futures/order-book`, `/v1/futures/contracts`) don't require authentication.

### Endpoint inventory

#### Public

| Method | Path                     | Purpose                                   |
| ------ | ------------------------ | ----------------------------------------- |
| `ANY`  | `/v1/ping`               | Health check; returns server time.        |
| `GET`  | `/v1/futures/order-book` | Order-book snapshot for an instrument.    |
| `GET`  | `/v1/futures/contracts`  | Instrument list with full specifications. |

#### Orders

| Method | Path                                           | Purpose                                                  |
| ------ | ---------------------------------------------- | -------------------------------------------------------- |
| `POST` | `/v1/futures/place-order`                      | Place by contract count (default sizing).                |
| `POST` | `/v1/futures/place-order-by-quoted-currency`   | Place by USDT amount.                                    |
| `POST` | `/v1/futures/place-order-by-base-currency`     | Place by base-asset amount (e.g. BTC).                   |
| `POST` | `/v1/futures/place-order-with-tpsl`            | Place with attached TP/SL.                               |
| `POST` | `/v1/futures/prepare-order`                    | Dry-run: returns fees, slippage, margin without placing. |
| `POST` | `/v1/futures/prepare-order-by-quoted-currency` | Dry-run, USDT-sized.                                     |
| `POST` | `/v1/futures/prepare-order-by-base-currency`   | Dry-run, base-sized.                                     |
| `POST` | `/v1/futures/cancel-order`                     | Cancel by order id.                                      |
| `POST` | `/v1/futures/cancel-all-orders`                | Cancel everything on an instrument.                      |

#### Stops / TP / SL

| Method | Path                                 | Purpose                                        |
| ------ | ------------------------------------ | ---------------------------------------------- |
| `POST` | `/v1/futures/place-stop-order`       | Place a stop / trigger order.                  |
| `POST` | `/v1/futures/cancel-stop-order`      | Cancel a stop / trigger order.                 |
| `POST` | `/v1/futures/positions/tpsl/replace` | Set/replace TP/SL on a position.               |
| `GET`  | `/v1/futures/positions/tpsl`         | Read attached TP/SL on positions.              |
| `POST` | `/v1/futures/orders/tpsl/replace`    | Set/replace TP/SL attached to a working order. |
| `GET`  | `/v1/futures/orders/tpsl`            | Read TP/SL on orders.                          |

#### Margin & leverage

| Method | Path                             | Purpose                                       |
| ------ | -------------------------------- | --------------------------------------------- |
| `POST` | `/v1/futures/prepare-leverage`   | Dry-run a leverage change.                    |
| `POST` | `/v1/futures/change-leverage`    | Set leverage on an instrument.                |
| `POST` | `/v1/futures/adjust-leverage`    | Incremental leverage change.                  |
| `POST` | `/v1/futures/change-margin-type` | Switch a position between cross and isolated. |
| `POST` | `/v1/futures/change-margin`      | Add or remove margin on an isolated position. |

### Request envelope

All authenticated POSTs take JSON. Required headers:

{% code title="Required headers" %}

```http
Content-Type: application/json
X-Api-Key: <key>          # or Authorization: Bearer <token>
```

{% endcode %}

Each endpoint's request fields, validation rules, response shape, and a `curl` example are covered in the full API reference.

### Response envelope

The standard envelope:

{% code title="Standard response envelope" %}

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

{% endcode %}

Two endpoint families return the raw matching-engine event without the envelope:

* `place-order-by-base-currency`
* `place-order-by-quoted-currency`

These return an `OrderPlacedMessage` directly. The endpoint pages flag this.

### Sample integration

A minimal "place market $50 short, log fill" flow:

{% code title="Place a market $50 short" %}

```bash
# 1. Confirm permissions and the instrument is allowed
curl -H "X-Api-Key: $KEY" https://agentdex.xyz/v1/futures/contracts | jq '.data[] | select(.id=="BTC_USDT_PERPETUAL")'

# 2. Place the order
curl -X POST -H "X-Api-Key: $KEY" -H "Content-Type: application/json" \
  https://agentdex.xyz/v1/futures/place-order-by-quoted-currency \
  -d '{
    "instrument_id": "BTC_USDT_PERPETUAL",
    "side": "sell",
    "quoted_currency_volume": "50",
    "is_market": true
  }'
# → { "order_id":"...", "avg_price":"...", "filled":"...", "remaining":"0" }
```

{% endcode %}

Combine with the WebSocket private `position` topic in [Websocket API](/agentdex-docs/developers/websocket-api.md) for live PnL updates.

### Pagination

History endpoints (order history, trade history) page by cursor or by `offset/limit`. Returned objects include the next cursor; pass it back in the next call.

### Error codes

See [API overview](/agentdex-docs/developers/api-overview.md) for the common error code table.


---

# 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/rest-api.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.
