WebSockets
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.
Authentication
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.
Connection Setup
Real-time streaming requires the Professional plan or higher. Your plan’s connection limits and minimum update interval apply.
The optional throttle supports 1s, 5s, 10s, 30s, or 60s. When omitted in the URL or a subscription message, it uses your plan’s minimum interval. A supported value faster than your plan allows is rejected with THROTTLE_NOT_ALLOWED.
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:
- URL query parameters: put your subscriptions in the connection URL. This is the simplest option when you know what you need when you connect. The parameters are listed on each topic page.
- Subscription messages: send a JSON message through the open connection to set or change your subscriptions.
A connection without any subscription is accepted; it receives nothing until you send a subscription message.
Subscription Messages
Send a JSON object with the subscription fields of the topic you connected to, and optionally a throttle:
{
"throttle": "10s",
"currencyUuids": ["Qwsogvtv82FCd", "razxDUgYGNAdQ"]
} | Topic | Subscription fields (choose one) |
|---|---|
| Rates | currencyUuids |
| Exchange Rates | references, exchangeUuids, or currencyUuids |
| Tickers | marketUuids, exchangeUuids, or currencyUuids |
- A message replaces your subscription. It does not add to what you subscribed to before, whether in the URL or in an earlier message. The same goes for the throttle: a message without
throttlesets it back to your plan’s minimum interval. - Every accepted message is answered with a confirmation listing your subscriptions and throttle.
- A refused message leaves your previous subscription unchanged. You receive an error message, the connection stays open, and the data you were already subscribed to keeps arriving.
list=allis only available as a URL query parameter. To switch to or from a full list, open a new connection.
Code examples
// Connect with subscriptions specified in the URL
const connection = new WebSocket(
'wss://api.coinranking.com/v2/real-time/rates?x-access-token=your-api-key¤cy-uuids[]=Qwsogvtv82FCd¤cy-uuids[]=razxDUgYGNAdQ&throttle=10s'
);
connection.onmessage = (event) => {
const message = JSON.parse(event.data);
console.log(message);
}; // Connect first, then send a JSON formatted subscription message
const connection = new WebSocket('wss://api.coinranking.com/v2/real-time/rates?x-access-token=your-api-key');
connection.onopen = () => {
connection.send(JSON.stringify({
throttle: '10s',
currencyUuids: ['Qwsogvtv82FCd', 'razxDUgYGNAdQ'],
}));
};
connection.onmessage = (event) => {
const message = JSON.parse(event.data);
if (message.type === 'confirmation') console.log('Subscribed to', message.subscriptions);
else if (message.type === 'error') console.error(message.code, message.errors);
else console.log(message);
}; // Server-side, with the ws package: send the API key in a header
import WebSocket from 'ws';
const connection = new WebSocket(
'wss://api.coinranking.com/v2/real-time/rates?currency-uuids[]=Qwsogvtv82FCd&throttle=10s',
{ headers: { 'x-access-token': process.env.COINRANKING_API_KEY } }
);
connection.on('message', (data) => {
console.log(JSON.parse(data));
}); GET /v2/real-time/rates?x-access-token=your-api-key HTTP/1.1
Host: api.coinranking.com
Connection: Upgrade
Upgrade: websocket
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: q4xkcO32u266gldTuKaSOw== Messages
Every message from the server is a JSON text frame with a type field:
rate,exchange-rate, orticker: a data message. Its fields are described on the topic page.confirmation: the answer to an accepted subscription message. A connection also receives one when it opens, confirming its URL subscriptions and effective throttle.error: a refused URL parameter or subscription message, see Errors.
{
"type": "confirmation",
"topic": "rate",
"throttle": "10s",
"subscriptions": ["Qwsogvtv82FCd", "razxDUgYGNAdQ"]
} Errors
Refused before the connection opens. A missing or unknown API key, an account without an active API subscription, 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.
{
"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 one of 1s, 5s, 10s, 30s, or 60s. A supported value faster than your plan allows returns THROTTLE_NOT_ALLOWED instead. |
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. |
THROTTLE_NOT_ALLOWED | The requested update interval is faster than your plan allows. An opening request is followed by close code 1008. A rejected subscription message leaves the existing connection unchanged. Equivalent SSE status: 403. |
STREAM_CONNECTION_LIMIT | Your account has reached its shared WebSocket and SSE connection limit. Close an existing connection before opening another. The server sends this error and closes with code 1008. Equivalent SSE status: 429. |
STREAMING_UNAVAILABLE | Streaming capacity could not be checked. The server sends this error and closes with code 1008. Retry after a delay. Equivalent SSE status: 503. |
STREAM_ACCESS_REQUIRED | The stream is missing valid gateway authorization. Use the documented api.coinranking.com URL with your API key. The server sends this error and closes with code 1008. Equivalent SSE status: 403. |
FORBIDDEN | The gateway refuses the HTTP upgrade with status 403 when streaming access is not configured for the plan, the feed is disabled, or a route access policy refuses the request. |
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. |
Reconnecting
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.