REST APIHow-tos
Remove orders
List a wallet's open orders, and remove them from the book with the REST API.
Removing an order takes it off Predict's order book. It's free and takes effect right away. To make the signed order unusable on-chain as well, cancel it on-chain instead: see Cancel orders.
You need your API key and a JWT for the wallet. See Log in.
1. List the open orders
GET /v1/orders with status=OPEN returns the wallet's open orders. Each one has its id and, under order, its hash. Page through them with first and after (see Pagination).
2. Remove them
POST /v1/orders/remove takes the orders' IDs, from 1 to 100 per request:
{ "data": { "ids": ["123456789", "123456790"] } }POST /v1/orders/remove-by-hash does the same with the orders' hashes, sent as hashes instead of ids.
The response sorts the orders into three lists:
{
"success": true,
"removed": ["123456789"],
"noop": ["123456790"],
"rejected": []
}| List | Meaning |
|---|---|
removed | The order was open, and is now off the book. |
noop | The order was no longer open, for example because it filled or was already removed. Nothing changed. |
rejected | The order can't be removed yet, because its removalLockedUntil time hasn't passed. Try again after that time. |
The whole flow
This removes every open order of the wallet:
const apiKey = "YOUR_API_KEY";
const apiUrl = "https://api.predict.fun";
const jwt = "YOUR_JWT"; // from the Log in how-to
const headers = {
"Content-Type": "application/json",
"x-api-key": apiKey,
Authorization: `Bearer ${jwt}`,
};
// 1. List the wallet's open orders, one page at a time.
const ids: string[] = [];
let after: string | undefined;
do {
const query = new URLSearchParams({ status: "OPEN", first: "100" });
if (after) query.set("after", after);
const response = await fetch(`${apiUrl}/v1/orders?${query}`, { headers });
const page = await response.json();
if (!response.ok) throw new Error(`${page.error}: ${page.message}`);
ids.push(...page.data.map((order: { id: string }) => order.id));
after = page.cursor ?? undefined;
} while (after);
// 2. Remove them, up to 100 per request.
for (let i = 0; i < ids.length; i += 100) {
const response = await fetch(`${apiUrl}/v1/orders/remove`, {
method: "POST",
headers,
body: JSON.stringify({ data: { ids: ids.slice(i, i + 100) } }),
});
const result = await response.json();
if (!response.ok) throw new Error(`${result.error}: ${result.message}`);
console.log(
`Removed ${result.removed.length}, already closed ${result.noop.length}, ` +
`still locked ${result.rejected.length}`,
);
}
console.log(`${ids.length} open orders found`);When it fails
| Status | message | Cause |
|---|---|---|
401 | one or more orders do not belong to this wallet | An ID or hash isn't an order of the JWT's wallet, or doesn't exist. Nothing is removed. |
400 | Mentions minItems(1) or maxItems(100) | The list is empty, or has more than 100 entries. |
401 | authorization error | The JWT is missing or has expired. Log in again. |