WebSocket APIHow-tos
Follow your orders
Get the events of your wallet's orders as they happen, from acceptance to settlement.
The predictWalletEvents/{jwt} topic sends an event each time something happens to one of your wallet's orders. Use it to learn whether an order was accepted, when it matched, and when its trades settled on-chain.
Subscribe with a JWT
The topic holds a JWT for the wallet, from the REST API's Log in how-to:
{ "method": "subscribe", "requestId": 1, "params": ["predictWalletEvents/eyJ0eXAiOiJKV1Qi…"] }The server checks the JWT when you subscribe. An invalid or expired JWT gets an invalid_credentials error. A JWT is valid for 24 hours, so log in again before you subscribe on a new connection. Nothing is sent on subscribe: events start with the next thing that happens to an order.
Events
Each event's type says what happened to the order, such as orderAccepted, orderTransactionSuccess or orderCancelled. Order lifecycle describes when you get each type, and the wallet events topic lists every field.
Every event has the order's orderId and orderHash, a timestamp in epoch milliseconds, and details with the marketId, the side (quoteType), the outcome, the price, the quantity and the quantityFilled.
The whole example
This subscribes with a JWT and prints a line for each event:
// Print each event of a wallet's orders as it happens.
// Runs in browsers and in Node.js 22 or later, which have WebSocket built in.
const jwt = "YOUR_JWT"; // from the REST API's Log in how-to
const topic = `predictWalletEvents/${jwt}`;
// Browsers can't set headers on a WebSocket, so the key goes in the URL.
const ws = new WebSocket("wss://ws.predict.fun/ws?apiKey=YOUR_API_KEY");
ws.addEventListener("open", () => {
ws.send(
JSON.stringify({ method: "subscribe", requestId: 1, params: [topic] }),
);
});
ws.addEventListener("message", (event) => {
const message = JSON.parse(event.data);
if (message.type === "R" && !message.success) {
console.error(`Subscribe failed: ${message.error.message}`);
} else if (message.topic === "heartbeat") {
// Echo each heartbeat, or the server closes the connection.
ws.send(JSON.stringify({ method: "heartbeat", data: message.data }));
} else if (message.topic === topic) {
const { type, orderId, reason, details } = message.data;
const { quoteType, outcome, quantityFilled, quantity, price } = details;
console.log(
`${type} ${orderId}: ${quoteType} ${outcome} ` +
`${quantityFilled}/${quantity} at ${price}${reason ? ` (${reason})` : ""}`,
);
}
});After a reconnect
Events sent while you were disconnected aren't replayed. After you reconnect:
- Subscribe again, with a JWT that hasn't expired.
- Fetch your open orders from
GET /v1/orders?status=OPEN, following itscursorfor more pages. - Treat that list as the current state, and apply new events on top of it.
Stay connected shows the reconnection itself.