Does Kraken have a WebSocket API? How can I connect to it?
Kraken has both a REST API and a WebSocket API. Our WebSocket API is available at the following secure URLs:
Public (unauthenticated) market data feeds: wss://ws.kraken.com/
Private (authenticated) account data feeds and trading endpoints: wss://ws-auth.kraken.com/
Authentication is not required to connect to the public market data feeds, but a valid authentication token is required for the private feeds.
All messages sent and received via the WebSocket API are plain text encoded in JSON format. The details of each message type are provided by our WebSocket API documentation.
Do you have WebSocket libraries available?
We have a variety of WebSocket API example code available, which can either be used directly, or as a starting point for your own implementation:
- a WebSocket API command line client (in Python),
- a WebSocket API test suite (in JavaScript),
- examples of how to use our recommended Python WebSocket module (websocket-client),
- and a WebSocket API library (Github).
Our WebSocket API can be implemented in any programming language that you prefer, and our API support team would be happy to assist you or provide example code in additional languages if needed.
Which feeds/endpoints are supported?
Our WebSocket API provides a variety of real time market data feeds, account data feeds, and trading endpoints. A complete list of available feeds/endpoints, along with the implementation details for each feed/endpoint, can been found in our WebSocket API documentation.
How do I know I'm successfully connected? How do I know I'm still connected?
When subscribed to a feed, a {u'event': u'heartbeat'} message will be received at a frequency of 1 heartbeat per second.
Heartbeats will not be received if other messages (market data, account data, etc.) are received instead. When and why market or account data is received depends upon the feed that you are subscribed to.
If you are subscribing to the feed of a currency pair with low trading volume, you may only receive heartbeats for long periods.
If you unsubscribe to a feed you will no longer receive heartbeats or trading data and after being not subscribed to any feed for 1 minute you will be disconnected from the WebSockets feed.
When and why does the WebSocket send updated data?
When and why market data or account data is received, depends upon the feed that you are subscribed to. For example:
Ticker: When there is a trade or batch of trades for a currency pair, a ticker message is published for that pair. You will only receive this message if you are subscribed to the ticker feed for this pair.
Trade: Similarly, when there is a trade or batch of trades for a currency pair, a trade message is also published for that pair. You will only receive this message if you are subscribed to the trade feed for this pair.
OHLC: An updated OHLC interval is published for each interval when a trade or batch of trades for a currency pair is executed. You will only receive updates for the intervals that you are subscribed to. After crossing an interval border, no message is sent until the next trade occurs.
Book: An initial snapshot of the order book with the chosen depth is published when first subscribed to the book feed.
As new orders are added to the order book or trades are executed that affect the order book depth you are subscribed to, a message is published containing order book updates for any affected price levels and volumes. These updates can contain only bids, only asks, or both bids and asks. See How to build an order book below for details.
Spread: When a new highest bid or lowest ask order is placed, or a trade is executed that changes the best bid or best ask price, a spread message is published with the updated bid and ask prices.
Do WebSocket feeds provide historical data or only current data?
The WebSocket market data feeds only provide current data, however it is possible to simultaneously connect to the WebSocket API for current data and the REST API for historical data.
Use of the reqid parameter.
Subscription requests can include a client provided reqid parameter which can subsequently be used to match the desired WebSocket subscription request with the channel ID for that subscription.
Note that if you make multiple WebSocket subscriptions at the same time, they will all be assigned the same reqid value. To assign a different reqid value, each feed must be subscribed to individually.
I want more details, where can I find them?
The complete WebSocket API specification is available in the WebSocket API documentation.
Differences between currency pair symbols in REST and WebSocket APIs.
The WebSocket API only accepts currency pair symbols in the ISO 4217-A3 format, such as XBT/USD.
The REST API AssetPairs endpoint can be used to translate symbols between the REST API versions (such as XBTUSD or XXBTZUSD) and the WebSocket API version (such as XBT/USD).
wsname field provides the WebSocket API symbol.
How to build an order book
The WebSocket API book feed is the most complex of all the WebSocket feeds, but as long as the book feed messages are interpreted correctly, it is possible to maintain a local copy of an order book with just a few lines of code.
Our WebSocket API support pages provide detailed instructions for interpreting the book feed messages, along with an example book feed transcript, and example code (in Python) for maintaining a valid local copy of an order book.
What are the WebSocket API connection limits?
The WebSocket API limits the maximum number of simultaneous connections to provide protection against misuse (such as DDoS attacks, for example). If your WebSocket client makes too many connections at the same time, the following error message will be received:
{“error”: {“message”: “You have reached maximum capacity for WebSocket connections. Please reach out to Kraken support team for any questions.“, “hint”: “Your code may be reconnecting too often or when it has no need to.“, “name”: “WebSocket connection rate error”}}
The WebSocket API allows multiple feed subscriptions via a single WebSocket connection (such as subscribing to all available market data for the XBT/USD currency pair via a single connection), so it is possible to stream all available market data for all currency pairs without reaching the WebSocket connection limits.
What are the WebSocket API rate limits?
The same trading rate limits apply across all order entry interfaces (web site, Kraken Terminal, REST and WebSocket APIs), and are explained in detail on our rate limits support page.
The message rate limit of a single WebSocket API connection will vary depending upon the load on the system. WebSocket API clients will receive the error message {"Error": "Exceeded msg rate"} when the message rate limit is exceeded.
Trading (placing/cancelling orders) via the WebSocket API.
Trading via the WebSocket API is available via the addOrder and cancelOrder endpoints, which are used to place new orders and cancel existing (open) orders respectively.
The WebSocket trading endpoints are available via the same private WebSocket connection (wss://ws-auth.kraken.com/) as the private account management feeds (openOrders and ownTrades).
Full details regarding the trading endpoints are available via the WebSocket API documentation, and our WebSocket API support pages provide further information and examples for using the trading endpoints in your own code.
Why do I receive a 403 error when connecting to the WebSocket API?
WebSocket API connections are intermediated through Cloudflare, hence any security settings implemented by Cloudflare can affect connections to both the public (wss://ws.kraken.com) and private (wss://ws-auth.kraken.com) URLs.
Cloudflare requires that Server Name Indication (SNI) is used for all TLS connections, otherwise the WebSocket connection would fail, and an HTTP 403 Forbidden error would be returned instead. Cloudflare's support pages provide further details regarding their TLS SNI requirement.
Our official Python WebSocket API library has been updated to support TLS SNI, but some third party WebSocket API libraries might still be affected. Please reach out to our API support team if you need any assistance updating your WebSocket API code to support TLS SNI.
WebSocket API assistance.
Please reach out to our API support team if you need any assistance implementing your WebSocket API client.