How to create the krakenapi.py file

Our command line API client is provided as a single small Python 2 script. It is completely self-contained, so does not require any API wrappers or API libraries.

The command line API client can be downloaded as krakenapi.py.

The Python 2 code for the command line API client is as follows:


#!/usr/bin/env python

# Kraken Rest API Explorer
# Usage: krakenapi method parameters
# Example: krakenapi Time
# Example: krakenapi OHLC pair=xxbtzusd interval=1440
# Example: krakenapi Balance
# Example: krakenapi OpenPositions
# Example: krakenapi AddOrder pair=xxbtzusd type=buy ordertype=market volume=0.003 leverage=5
# For private methods (anything accessing an account), API key and API secret must be in text files called API_Key and API_Secret respectively.

import sys
import time
import base64
import hashlib
import hmac
import urllib2

api_public = {"Time", "Assets", "AssetPairs", "Ticker", "OHLC", "Depth", "Trades", "Spread"}
api_private = {"Balance", "BalanceEx", "TradeBalance", "OpenOrders", "ClosedOrders", "QueryOrders", "TradesHistory", "QueryTrades", "OpenPositions", "Ledgers", "QueryLedgers", "TradeVolume", "AddExport", "ExportStatus", "RetrieveExport", "RemoveExport"}
api_trading = {"AddOrder", "CancelOrder"}
api_funding = {"DepositMethods", "DepositAddresses", "DepositStatus", "WithdrawInfo", "Withdraw", "WithdrawStatus", "WithdrawCancel"}

api_domain = "https://api.kraken.com"
api_data = ""

if len(sys.argv) < 2:
	api_method = "Time"
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 or api_method in api_trading or api_method in api_funding:
	api_path = "/0/private/"
	api_nonce = str(int(time.time()*1000))
	try:
		api_key = open("API_Key").read().strip()
		api_secret = base64.b64decode(open("API_Secret").read().strip())
	except:
		print("API key and API secret must be in text files called API_Key and API_Secret")
		sys.exit(1)
	api_postdata = "nonce=" + api_nonce + "&" + api_data
	api_sha256 = hashlib.sha256(api_nonce+api_postdata).digest()
	api_hmacsha512 = hmac.new(api_secret, api_path + api_method + api_sha256, hashlib.sha512)
	api_request = urllib2.Request(api_domain + api_path + api_method, api_postdata)
	api_request.add_header("API-Key", api_key)
	api_request.add_header("API-Sign", base64.b64encode(api_hmacsha512.digest()))
elif api_method in api_public:
	api_path = "/0/public/"
	api_request = urllib2.Request(api_domain + api_path + api_method, api_data)
else:
	print("Usage: {} method parameters".format(sys.argv[0]))
	print("Example: {} OHLC pair=xxbtzusd interval=1440".format(sys.argv[0]))
	sys.exit(1)

try:
	api_reply = urllib2.urlopen(api_request).read()
except:
	print("API could not be accessed because web server returned an error")
	sys.exit(1)

if '"error":[]' in api_reply:
	print(api_reply)
	sys.exit(0)
else:
	print(api_reply)
	sys.exit(1)