> 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-typescript-sdk/builder-mode/cancel-order.md).

# Cancel Order

> Cancel orders and query order history for a user.

## Cancel Single Order

```typescript
import { BuilderClient } from '@opinion-labs/opinion-clob-sdk';

const builder = new BuilderClient({
  host: 'https://openapi.opinion.trade/openapi',
  builderApiKey: 'YOUR_BUILDER_KEY',
  chainId: 56,
});

const result = await builder.cancelOrderForUser(userApiKey, 'order_id_here');
```

| Parameter    | Type   | Required | Description        |
| ------------ | ------ | -------- | ------------------ |
| `userApiKey` | string | Yes      | User's API key     |
| `orderId`    | string | Yes      | Order ID to cancel |

## Cancel Batch

Cancel multiple orders at once:

```typescript
const results = await builder.cancelOrdersBatchForUser(userApiKey, [
  'order_id_1',
  'order_id_2',
  'order_id_3',
]);

for (const r of results) {
  if (r.success) {
    console.log(`Order ${r.orderId} cancelled`);
  } else {
    console.log(`Order ${r.orderId} failed: ${r.error}`);
  }
}
```

| Parameter    | Type      | Required | Description                  |
| ------------ | --------- | -------- | ---------------------------- |
| `userApiKey` | string    | Yes      | User's API key               |
| `orderIds`   | string\[] | Yes      | Array of order IDs to cancel |

### Batch Response

```typescript
Array<{
  index: number;      // Position in the input array
  success: boolean;   // Whether cancellation succeeded
  result?: unknown;   // Result on success
  error?: string;     // Error message on failure
  orderId: string;    // The order ID
}>
```

## Cancel All Orders

Cancel all open orders with optional filters:

```typescript
import { OrderSide } from '@opinion-labs/opinion-clob-sdk';

// Cancel all open orders
const result = await builder.cancelAllOrdersForUser(userApiKey);

// Cancel all open orders for a specific market
const result = await builder.cancelAllOrdersForUser(userApiKey, { marketId: 123 });

// Cancel all open BUY orders
const result = await builder.cancelAllOrdersForUser(userApiKey, { side: OrderSide.BUY });
```

| Parameter          | Type      | Required | Description                  |
| ------------------ | --------- | -------- | ---------------------------- |
| `userApiKey`       | string    | Yes      | User's API key               |
| `options.marketId` | number    | No       | Filter by market ID          |
| `options.side`     | OrderSide | No       | Filter by side (BUY or SELL) |

### Cancel All Response

```typescript
{
  totalOrders: number;   // Total orders found matching filters
  cancelled: number;     // Successfully cancelled
  failed: number;        // Failed to cancel
  results: Array<{ index: number; success: boolean; result?: unknown; error?: string; orderId: string }>
}
```

## Get User Orders

Query a user's orders:

```typescript
const orders = await builder.getUserOrders(userApiKey, {
  marketId: 123,
  status: '1',    // 1 = open/pending
  limit: 20,      // Max 20 per page
  page: 1,
});
```

| Parameter          | Type   | Required | Description                             |
| ------------------ | ------ | -------- | --------------------------------------- |
| `userApiKey`       | string | Yes      | User's API key                          |
| `options.marketId` | number | No       | Filter by market ID                     |
| `options.status`   | string | No       | Filter by status (e.g., `'1'` for open) |
| `options.limit`    | number | No       | Results per page (default: 10, max: 20) |
| `options.page`     | number | No       | Page number (default: 1)                |

## Notes

* Order cancellation uses the **user's API key**, not the builder API key.
* Cancellation is gasless (counts toward the 2,000/day limit).
* `cancelAllOrdersForUser()` automatically paginates through 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:

```
GET https://docs.opinion.trade/developer-guide/opinion-clob-typescript-sdk/builder-mode/cancel-order.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
