> For the complete documentation index, see [llms.txt](https://docs.opinion.trade/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.opinion.trade/developer-guide/opinion-clob-python-sdk/builder-mode/cancel-order.md).

# Cancel Order

> Cancel orders for a user. Requires the user's API key.

## Cancel Single Order

```python
result = builder.cancel_order_for_user(user_apikey, "order_id_here")
# Returns: {"result": True}
```

### Parameters

| Parameter     | Type | Required | Description                                                         |
| ------------- | ---- | -------- | ------------------------------------------------------------------- |
| `user_apikey` | str  | Yes      | User's API key (from `create_user()` or `regenerate_user_apikey()`) |
| `order_id`    | str  | Yes      | Order ID to cancel                                                  |

## Cancel Multiple Orders

```python
results = builder.cancel_orders_batch_for_user(user_apikey, [
    "order_id_1",
    "order_id_2",
    "order_id_3",
])

for r in results:
    if r["success"]:
        print(f"Cancelled: {r['order_id']}")
    else:
        print(f"Failed: {r['order_id']}, Error: {r['error']}")
```

### Parameters

| Parameter     | Type       | Required | Description                 |
| ------------- | ---------- | -------- | --------------------------- |
| `user_apikey` | str        | Yes      | User's API key              |
| `order_ids`   | list\[str] | Yes      | List of order IDs to cancel |

### Response

List of result dicts, each containing:

| Key        | Type | Description                    |
| ---------- | ---- | ------------------------------ |
| `index`    | int  | Position in the input list     |
| `success`  | bool | Whether cancellation succeeded |
| `order_id` | str  | The order ID                   |
| `result`   | dict | API response (if success)      |
| `error`    | str  | Error message (if failed)      |

## Cancel All Orders

```python
result = builder.cancel_all_orders_for_user(user_apikey)

print(f"Cancelled: {result['cancelled']}/{result['total_orders']}")
```

With optional filters:

```python
from opinion_clob_sdk.chain.py_order_utils.model.sides import OrderSide

result = builder.cancel_all_orders_for_user(
    user_apikey,
    market_id=123,            # Optional: only this market
    side=OrderSide.BUY,       # Optional: only BUY orders
)
```

### Parameters

| Parameter     | Type      | Required | Description                                          |
| ------------- | --------- | -------- | ---------------------------------------------------- |
| `user_apikey` | str       | Yes      | User's API key                                       |
| `market_id`   | int       | No       | Filter by market ID                                  |
| `side`        | OrderSide | No       | Filter by side (`OrderSide.BUY` or `OrderSide.SELL`) |

### Response

```python
{
    "total_orders": 5,
    "cancelled": 4,
    "failed": 1,
    "results": [ ... ]   # List of per-order results
}
```

## Get User Orders

```python
response = builder.get_user_orders(
    user_apikey=user_apikey,
    market_id=123,     # 0 for all markets
    status="1",        # "1"=pending, "2"=filled, "3"=canceled, "4"=expired, "5"=failed
    limit=20,          # Max 20
    page=1,
)
```

### Parameters

| Parameter     | Type | Required | Description                                                   |
| ------------- | ---- | -------- | ------------------------------------------------------------- |
| `user_apikey` | str  | Yes      | User's API key                                                |
| `market_id`   | int  | No       | Filter by market (0 for all, default: 0)                      |
| `status`      | str  | No       | Status filter. Comma-separated for multiple (e.g., `"1,2,3"`) |
| `limit`       | int  | No       | Orders per page, max 20 (default: 10)                         |
| `page`        | int  | No       | Page number (default: 1)                                      |

## Notes

* All cancellation methods use the **user's API key**, not the builder API key.
* The builder manages user API keys and acts on their behalf.
* `cancel_all_orders_for_user()` uses pagination internally to collect all open orders before cancelling.


---

# 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://docs.opinion.trade/developer-guide/opinion-clob-python-sdk/builder-mode/cancel-order.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.
