Public WebSockets API Python code example

Following is a simple Python implementation for connecting to and subscribing to our public WebSockets API. It will allow you to connect to the public WebSockets API, subscribe to channels and then print the data in your terminal environment.

You can download the Python implementation from the following link: kraken_connection.py

Connection in terminal (node.js)

Connecting to the public WebSockets API:

wscat -c wss://ws.kraken.com/

Get ping:

{"event": "ping"}

{"event": "ping", "reqid":1012}

Subscribe to channel:

{ "event": "subscribe", "pair": ["ETH/USD"], "subscription": { "name": "spread" }}

{ "event": "subscribe", "pair": ["XBT/USD"], "subscription": { "name": "spread" }, "reqid":1223}

{ "event": "subscribe", "pair": ["XBT/EUR"], "subscription": { "name": "trade" }}

{ "event": "subscribe", "pair": ["XBT/USD"], "subscription": { "name": "ohlc", "interval":5 }}

{ "event": "subscribe", "pair": ["XRP/EUR"], "subscription": { "name": "book", "depth": 10 }}

Unsubscribe from channel:

{ "event": "unsubscribe", "channelID": 146 }

{ "event": "unsubscribe", "pair": ["ETH/USD"], "subscription": { "name": "spread" }}

{ "event": "unsubscribe", "pair": ["XBT/USD"], "subscription": { "name": "spread" }, "reqid": 1223 }

Refer to the public WebSockets API documentation that can be found here for more advanced details: https://www.kraken.com/en-us/help/websocket-api