REST API Command Line Client

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

All API endpoints are accessible including:

  • public market data methods
  • private account management methods
  • trading methods

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 krakenfuturesapi.py file to your computer in the folder (directory) of your choosing.

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


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/KrakenFuturesAPI


4. Make the krakenfuturesapi.py executable.

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

chmod 755 krakenfuturesapi.py


5. Optional: Add your API keys to the same folder where you are keeping the krakenfuturesapi.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 REST API methods to access your Kraken Futures account (such as balance inquiries, account history, placing/cancelling orders, etc.).

Usage

The command line REST API client is used by typing commands like "./krakenfuturesapi.py api_method method_parameters" at a command prompt. For example:

./krakenfuturesapi.py instruments

./krakenfuturesapi.py tickers

./krakenfuturesapi.py orderbook symbol=pi_xbtusd

./krakenfuturesapi.py accounts

./krakenfuturesapi.py openpositions

./krakenfuturesapi.py sendorder orderType=lmt symbol=pi_xbtusd side=buy size=500 limitPrice=4000

Complete details of all of the Kraken Futures REST API methods and their parameters are available via our API documentation.

Python 2 Code

The REST 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 REST API
#
# Usage: ./krakenfuturesapi.py method parameters
# Example: ./krakenfuturesapi.py instruments
# Example: ./krakenfuturesapi.py orderbook symbol=pi_xbtusd
# Example: ./krakenfuturesapi.py accounts
# Example: ./krakenfuturesapi.py transfer fromAccount=cash toAccount=fi_xbtusd unit=xbt amount=0.5
#
# For private methods (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 time
import base64
import hashlib
import hmac
import urllib2
 
api_public = {"instruments", "tickers", "orderbook", "history"}
api_private_get = {"accounts", "openorders", "fills", "openpositions", "transfers", "notifications"}
api_private_post = {"transfer", "sendorder", "cancelorder", "cancelallorders", "cancelallordersafter", "batchorder"}
 
api_domain = "https://futures.kraken.com/derivatives"
api_data = ""
 
if len(sys.argv) < 2:
    api_method = "instruments"
elif len(sys.argv) == 2:
    api_method = sys.argv[1]
else:
    api_method = sys.argv[1]
    for count in range(2, len(sys.argv)):
        if count == 2:
            api_data = sys.argv[count]
        else:
            api_data = api_data + "&" + sys.argv[count]
 
if api_method in api_private_get or api_method in api_private_post:
    api_path = "/api/v3/"
    api_nonce = str(int(time.time()*1000))
    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")
        sys.exit(1)
    api_postdata = api_data
    api_sha256 = hashlib.sha256(api_postdata + api_nonce + api_path + api_method).digest()
    api_hmacsha512 = hmac.new(api_secret, api_sha256, hashlib.sha512)
    if api_method in api_private_get:
        api_request = urllib2.Request(api_domain + api_path + api_method + "?" + api_postdata)
    else:
        api_request = urllib2.Request(api_domain + api_path + api_method, api_postdata)
    api_request.add_header("APIKey", api_key)
    api_request.add_header("Nonce", api_nonce)
    api_request.add_header("Authent", base64.b64encode(api_hmacsha512.digest()))
elif api_method in api_public:
    api_path = "/api/v3/"
    api_request = urllib2.Request(api_domain + api_path + api_method + "?" + api_data)
else:
    print("Usage: {} method parameters".format(sys.argv[0]))
    print("Example: {} orderbook symbol=pi_xbtusd".format(sys.argv[0]))
    sys.exit(1)
 
try:
    api_reply = urllib2.urlopen(api_request).read()
except Exception as error:
    print("API request could not be completed because of an unexpected error (%s)" % error)
    sys.exit(1)
 
if '"result":"success"' in api_reply:
    print(api_reply)
    sys.exit(0)
else:
    print(api_reply)
    sys.exit(1)