All
Filter by:
How do I deposit cash into my account?
I need help with account verification
Why can't I access my account?
Are there any crypto withdrawal fees?
I need help signing into my account
Required Legal Notice: Virtual currencies, real risks. The only guarantee in crypto is risk. Read more
Required Legal Notice: Virtual currencies, real risks. The only guarantee in crypto is risk. Read more
Required Legal Notice: Virtual currencies, real risks. The only guarantee in crypto is risk. Warning: The value of your virtual currencies can rise or fall sharply, and your initial investment may be lost completely; virtual currencies are not covered by the guarantee funds that cover bank deposits; there is no legal mechanism on the virtual currencies market to prevent market manipulation or insider dealing; virtual currencies depend entirely on a specific computer technology and infrastructure, which in certain cases may be very recent and not yet adequately tested; if one loses the identification code or password giving access to the virtual wallet in which the virtual currency is stored, the currency held therein will be irretrievably lost; virtual currencies are currently accepted as a means of payment to a limited extent, and in most countries there is no legal obligation to accept them; for more information about the risks associated with an investment in virtual currencies, we advise you to read the Wikifin page What is a cryptocurrency? | Wikifin.
Required Legal Notice: Virtual currencies, real risks. The only guarantee in crypto is risk. Warning: The value of your virtual currencies can rise or fall sharply, and your initial investment may be lost completely; virtual currencies are not covered by the guarantee funds that cover bank deposits; there is no legal mechanism on the virtual currencies market to prevent market manipulation or insider dealing; virtual currencies depend entirely on a specific computer technology and infrastructure, which in certain cases may be very recent and not yet adequately tested; if one loses the identification code or password giving access to the virtual wallet in which the virtual currency is stored, the currency held therein will be irretrievably lost; virtual currencies are currently accepted as a means of payment to a limited extent, and in most countries there is no legal obligation to accept them; for more information about the risks associated with an investment in virtual currencies, we advise you to read the Wikifin page What is a cryptocurrency? | Wikifin.
The following Python code implements a command line API client for our exchange WebSocket API v1. The code can be used as a standalone API client, or can be called from other interpreted or compiled programming languages (Bash, C++, etc.).
The standard Python WebSocket library (websocket-client) is required for the WebSocket networking, but no other API libraries/modules are required.
The WebSocket API command line client can also be downloaded as krakenws.py.
All public market data feeds (ticker, book, spread, ohlc, trade), private account management feeds (openOrders and ownTrades), and trading endpoints (addOrder, cancelOrder, cancelAll) are supported.
For the private feeds/endpoints, a WebSocket authentication token must be provided by retrieving the token from the REST API GetWebSocketsToken endpoint, and then copying/pasting the token value into a text file named WS_Token (in the same directory/folder as the command line API client).
krakenws.py feed/endpoint [parameters]
./krakenws.py ticker XBT/USD ETH/EUR XDG/XBT
./krakenws.py book ETH/USD 10
./krakenws.py ohlc LTC/EUR XBT/USD 60
./krakenws.py openOrders
./krakenws.py ownTrades
./krakenws.py addOrder pair=XBT/USD type=buy ordertype=market volume=0.002 leverage=5
./krakenws.py cancelOrder txid=O6L3CW-FD5CZ-NE373X
The usage of the WebSocket API v1 command line client is as follows:such as:
#!/usr/bin/env python
# Kraken WebSocket API
#
# Usage: ./krakenws.py feed/endpoint [parameters]
# Example: ./krakenws.py ticker XBT/USD
# Example: ./krakenws.py spread XBT/USD XBT/EUR ETH/USD LTC/EUR
# Example: ./krakenws.py book XBT/USD XBT/EUR 10
# Example: ./krakenws.py openOrders
# Example: ./krakenws.py ownTrades
# Example: ./krakenws.py addOrder pair=XBT/EUR type=sell ordertype=limit price=7500 volume=0.125
# Example: ./krakenws.py cancelOrder txid=OADMSD-7AGC3-IMB46A,OD6VRE-HCSPM-CKORER
#
# For account management and trading, a valid WebSocket authentication token (from the REST API GetWebSocketsToken endpoint) must be provided in a plain text file named WS_Token.
import sys
import signal
from websocket import create_connection
def timeoutfunction(signalnumber, frame):
raise KeyboardInterrupt
signal.signal(signal.SIGALRM, timeoutfunction)
api_status = {"ping"}
api_public = {"trade", "book", "ticker", "spread", "ohlc"}
api_private = {"openOrders", "ownTrades", "balances"}
api_trading = {"addOrder", "editOrder", "cancelOrder", "cancelAll", "cancelAllOrdersAfter"}
api_domain_public = "wss://ws.kraken.com/"
api_domain_private = "wss://ws-auth.kraken.com/"
api_symbols = ""
api_number = 0
if len(sys.argv) < 2:
api_feed = "ping"
else:
api_feed = sys.argv[1]
if api_feed in api_status:
api_domain = api_domain_public
api_data = '{"event":"%(feed)s"}' % {"feed":api_feed}
signal.alarm(3)
elif api_feed in api_public:
if len(sys.argv) < 3:
print("Usage: %s feed/endpoint [parameters]" % sys.argv[0])
print("Example: %s ticker XBT/USD" % sys.argv[0])
sys.exit(1)
for count in range(2, len(sys.argv)):
if sys.argv[count].isdecimal() == True:
api_number = int(sys.argv[count])
else:
if len(api_symbols) == 0:
api_symbols += sys.argv[count].upper()
else:
api_symbols += '","' + sys.argv[count].upper()
if api_feed == 'book':
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "depth":%(depth)d}, "pair":["%(symbols)s"]}' % {"feed":api_feed, "symbols":api_symbols, "depth":api_number if api_number != 0 else 10}
elif api_feed == 'ohlc':
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "interval":%(interval)d}, "pair":["%(symbols)s"]}' % {"feed":api_feed, "symbols":api_symbols, "interval":api_number if api_number != 0 else 1}
else:
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s"}, "pair":["%(symbols)s"]}' % {"feed":api_feed, "symbols":api_symbols}
api_domain = api_domain_public
elif api_feed in api_private:
api_domain = api_domain_private
try:
api_token = open("WS_Token").read().strip()
except:
print("WebSocket authentication token missing (WS_Token)")
sys.exit(1)
if len(sys.argv) >= 3:
if api_feed == 'openOrders':
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "ratecounter":%(ratecounter)s, "token":"%(token)s"}}' % {"feed":api_feed, "ratecounter":sys.argv[2].split('=')[1], "token":api_token}
elif api_feed == 'ownTrades':
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "snapshot":%(snapshot)s, "token":"%(token)s"}}' % {"feed":api_feed, "snapshot":sys.argv[2].split('=')[1], "token":api_token}
else:
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "token":"%(token)s"}}' % {"feed":api_feed, "token":api_token}
else:
api_data = '{"event":"subscribe", "subscription":{"name":"%(feed)s", "token":"%(token)s"}}' % {"feed":api_feed, "token":api_token}
elif api_feed in api_trading:
api_domain = api_domain_private
try:
api_token = open("WS_Token").read().strip()
except:
print("WebSocket authentication token missing (WS_Token)")
sys.exit(1)
api_data = '{"event":"%(feed)s", "token":"%(token)s"' % {"feed":api_feed, "token":api_token}
for count in range(2, len(sys.argv)):
if sys.argv[count].split('=')[0] == 'txid':
api_data = api_data + ', "%(name)s":["%(value)s"]' % {"name":sys.argv[count].split('=')[0], "value":sys.argv[count].split('=')[1].replace(',', '","')}
elif sys.argv[count].split('=')[0] == 'reqid':
api_data = api_data + ', "%(name)s":%(value)s' % {"name":sys.argv[count].split('=')[0], "value":sys.argv[count].split('=')[1]}
elif sys.argv[count].split('=')[0] == 'timeout':
api_data = api_data + ', "%(name)s":%(value)s' % {"name":sys.argv[count].split('=')[0], "value":sys.argv[count].split('=')[1]}
else:
api_data = api_data + ', "%(name)s":"%(value)s"' % {"name":sys.argv[count].split('=')[0], "value":sys.argv[count].split('=')[1]}
api_data = api_data + '}'
signal.alarm(3)
else:
print("Usage: %s feed/endpoint [parameters]" % sys.argv[0])
print("Example: %s ticker XBT/USD" % sys.argv[0])
sys.exit(1)
try:
ws = create_connection(api_domain)
print("WebSocket -> Client: %s" % ws.recv())
except Exception as error:
print("WebSocket connection failed (%s)" % error)
sys.exit(1)
try:
print("Client -> WebSocket: %s" % api_data)
ws.send(api_data)
print("WebSocket -> Client: %s" % ws.recv())
except Exception as error:
print("WebSocket subscription/request failed (%s)" % error)
ws.close()
sys.exit(1)
while True:
try:
print("WebSocket -> Client: %s" % ws.recv())
except KeyboardInterrupt:
ws.close()
sys.exit(0)
except Exception as error:
print("WebSocket messages failed (%s)" % error)
sys.exit(1)
sys.exit(1)