A WebSocket is a persistent, two-way connection. You open it once, receive new data as soon as it is calculated, and change your subscriptions at any time by sending a message, without reconnecting.
This page explains how to connect and how to manage a connection. What you can subscribe to, and the messages you receive, are described per topic: Rates, Exchange Rates, and Tickers. Prefer plain HTTP? The same topics are available as Server-Sent Events.
The browser's WebSocket API cannot set request headers, so authenticate with a query parameter:
wss://api.coinranking.com/v2/real-time/rates?x-access-token=your-api-key
Server-side WebSocket libraries can set headers on the opening request. There, send the key in the x-access-token header instead, exactly as for the REST endpoints, which keeps it out of URLs and logs. A key in a web page is visible to every visitor, so for a public website, connect from your server and relay the data to the browser.
Open a connection to the path of the topic you want to receive:
wss://api.coinranking.com/v2/real-time/rates
wss://api.coinranking.com/v2/real-time/exchange-rates
wss://api.coinranking.com/v2/real-time/tickers
A connection receives one topic. You can subscribe in two ways, and combine them:
A connection without any subscription is accepted; it receives nothing until you send a subscription message.
Send a JSON object with the subscription fields of the topic you connected to, and optionally a throttle:
json{
"throttle": "10s",
"currencyUuids": ["Qwsogvtv82FCd", "razxDUgYGNAdQ"]
}
| Topic | Subscription fields (choose one) |
|---|---|
| Rates | currencyUuids |
| Exchange Rates | references, exchangeUuids, or currencyUuids |
| Tickers | marketUuids, exchangeUuids, or currencyUuids |
throttle sets it back to 1s.list=all is only available as a URL query parameter. To switch to or from a full list, open a new connection.Every message from the server is a JSON text frame with a type field:
rate, exchange-rate, or ticker: a data message. Its fields are described on the topic page.confirmation: the answer to an accepted subscription message. A connection that subscribes through the URL does not receive one; data starts arriving right away.error: a refused URL parameter or subscription message, see Errors.json{
"type": "confirmation",
"topic": "rate",
"throttle": "10s",
"subscriptions": ["Qwsogvtv82FCd", "razxDUgYGNAdQ"]
}
Refused before the connection opens. A missing or unknown API key, a plan without real-time access, or an exhausted rate limit is refused during the HTTP upgrade, before any WebSocket exists. The response is a normal 401, 403, or 429 with the same JSON body as on the Server-Sent Events endpoint. Browsers do not expose that response: the WebSocket only fires an error event followed by a close event with code 1006. To see the reason, request the Server-Sent Events URL of the same topic with the same key, which is refused with the same status and body.
Refused after the connection opens. An invalid URL parameter or subscription message is answered with an error message. After an invalid URL parameter, the server closes the connection; after an invalid subscription message, the connection stays open.
json{
"type": "error",
"code": "UNKNOWN_CURRENCY_UUID",
"errors": ["Could not find coins with the following UUIDs: notarealuuid"]
}
The codes are the same on both transports. Match on code rather than on the text in errors; the messages may be reworded.
| Code | Meaning |
|---|---|
INVALID_THROTTLE |
The throttle in the URL or in a subscription message is not 1s or 10s. |
INVALID_SUBSCRIPTION |
More than one kind of subscription was given, or a references entry is not in the exchangeUuid_currencyUuid format. |
TOO_MANY_UUIDS |
More than 100 UUIDs in one subscription. |
UNKNOWN_CURRENCY_UUIDUNKNOWN_MARKET_UUIDUNKNOWN_EXCHANGE_UUID |
One or more UUIDs do not exist. The whole subscription is refused, and the error lists the offending UUIDs. |
DEX_ADD_ON_REQUIRED |
DEX markets or exchanges were requested without the Real-time DEX data add-on. |
INVALID_MESSAGE |
A subscription message is not a JSON object, or one of its subscription fields is not an array of strings. |
INTERNAL_SERVER_ERROR |
Something went wrong on our side. The error carries a reference you can quote when contacting support. |
There is no maximum connection duration. The server sends a WebSocket ping frame every 25 seconds, which browsers and WebSocket libraries answer automatically. A connection that leaves two consecutive pings unanswered is closed, so a client that stops responding is cut off rather than buffered without bound.
When the real-time service is deployed or restarted, every connection is closed with code 1001. Reconnect after a short delay. Subscriptions in the URL are restored by reconnecting with the same URL; subscriptions you set with a message have to be sent again. Opening a connection counts toward your rate limit, so do not retry a refused connection without fixing its cause, and back off when you hit the limit.