WebSocket API Command Line Client

The Kraken Futures WebSocket API client allows full access to the futures WebSocket API via a command prompt, hence no programming knowledge or API experience is required.

All WebSocket feeds are accessible including:

  • public market data feeds
  • private account management feeds

The command line client can be used as a standalone API client, or can be called from other programs (such as other interpreted languages like Bash scripts, or compiled languages like C).

Installation

1. Install Python 2 (if necessary).

  • MacOS and Linux probably already have Python 2
  • Windows probably does not, but can be installed from https://www.python.org/


2. Download the krakenfuturesws.py file to your computer in the folder (directory) of your choosing.

  • For example: Macintosh HD > Users > satoshi > KrakenFuturesWS


3. Open a command prompt (such as MacOS's Terminal), and navigate to the folder (directory) chosen in the previous step.

You can use the "cd" command (change directory) to navigate. For example:

cd /Users/satoshi/KrakenFuturesWS


4. Make the krakenfuturesws.py executable.

You can use the "chmod" command (change mode). For example:

chmod 755 krakenfuturesws.py


5. Optional: Add your API keys to the same folder where you are keeping the krakenfuturesws.py file.

Copy and paste your API public key from account settings into a file called "API_Public".

Copy and paste your API private key from account settings into a file called "API_Private".

See: How to generate an API key pair?

An API key pair is only needed if you plan to use the private WebSocket feeds to access your Kraken Futures account (such as balance feeds, order and position feeds, account notifications, etc.).

Usage

The command line WebSocket API client is used by typing commands like "./krakenfuturesws.py api_feed feed_parameters" at a command prompt. For example:

./krakenfuturesws.py ticker_lite pi_xbtusd

./krakenfuturesws.py book pi_ethusd pi_ltcusd pi_bchusd

./krakenfuturesws.py account_balances_and_margins

./krakenfuturesws.py fills

./krakenfuturesws.py open_positions

Complete details of all of the Kraken Futures WebSocket feeds are available via our API documentation.

Python 2 Code

The WebSocket API command line client can be downloaded in the Installation section above.

For reference, the Python 2 code for the API client is as follows:

#!/usr/bin/env python

# Kraken Futures WebSocket API
# 
# Usage: ./krakenfuturesws feed parameters
# Example: ./krakenfuturesws ticker_lite pi_xbtusd
# Example: ./krakenfuturesws book pi_xbtusd pi_ethusd
# Example: ./krakenfuturesws account_balances_and_margins
# Example: ./krakenfuturesws fills
# 
# For private feeds (anything accessing an account), API public key and API private key must be in separate text files called API_Public and API_Private respectively.

import sys
import base64
import hashlib
import hmac
import json
from websocket import create_connection

api_public = {"trade", "book", "ticker", "ticker_lite"}
api_private = {"account_balances_and_margins", "account_log", "deposits_withdrawals", "fills", "open_positions", "open_orders", "open_orders_verbose", "notifications_auth"}
api_domain = "wss://futures.kraken.com/ws/v1"

if len(sys.argv) < 2:
	print("Usage: {} feed parameters".format(sys.argv[0]))
	print("Example: {} ticker_lite pi_xbtusd".format(sys.argv[0]))
	sys.exit(1)

try:
	ws = create_connection(api_domain)
	print(ws.recv())
	ws.send('{"event":"subscribe", "feed":"heartbeat"}')
	print(ws.recv())
except Exception as error:
	print("WebSocket connection failed due to unexpected error (%s)" % error)
	sys.exit(1)

api_feed = sys.argv[1]

if api_feed in api_public:
	if len(sys.argv) < 3:
		print("Usage: {} feed parameters".format(sys.argv[0]))
		print("Example: {} ticker_lite pi_xbtusd".format(sys.argv[0]))
		ws.close()
		sys.exit(1)
	api_symbols = sys.argv[2]
	for count in range(3, len(sys.argv)):
		api_symbols = api_symbols + '","' + sys.argv[count]
	api_data = '{"event":"subscribe", "feed":"%(feed)s", "product_ids":["%(symbols)s"]}' % {"feed":api_feed, "symbols":api_symbols}
elif api_feed in api_private:
	try:
		api_key = open("API_Public").read().strip()
		api_secret = base64.b64decode(open("API_Private").read().strip())
	except:
		print("API public key and API private key must be in text files called API_Public and API_Private")
		ws.close()
		sys.exit(1)
	api_data = '{"event":"challenge", "api_key":"%s"}' % api_key
	try:
		ws.send(api_data)
		api_data = ws.recv()
	except Exception as error:
		print("WebSocket authentication failed due to unexpected error (%s)" % error)
		ws.close()
		sys.exit(1)
	api_challenge = str(json.loads(api_data)["message"])
	api_sha256 = hashlib.sha256(api_challenge).digest()
	api_hmacsha512 = hmac.new(api_secret, api_sha256, hashlib.sha512)
	api_signature = base64.b64encode(api_hmacsha512.digest())
	api_data = '{"event":"subscribe", "feed":"%(feed)s", "api_key":"%(key)s", "original_challenge":"%(challenge)s", "signed_challenge":"%(signature)s"}' % {"feed":api_feed, "key":api_key, "challenge":api_challenge, "signature":api_signature}
else:
	print("Usage: {} feed parameters".format(sys.argv[0]))
	print("Example: {} ticker_lite pi_xbtusd".format(sys.argv[0]))
	ws.close()
	sys.exit(1)

try:
	ws.send(api_data)
except Exception as error:
	print("Subscription to %(feed)s feed failed due to unexpected error (%(error))" % {"feed":sys.argv[1], "error":error})
	ws.close()
	sys.exit(1)

for count in range(300):
	try:
		api_data = ws.recv()
		print(api_data)
	except KeyboardInterrupt:
		ws.close()
		sys.exit(1)

ws.close()
sys.exit(0)