Pesquisar
Example REST API request using bash

Introduction

Implementing a Kraken REST API interface using the Bourne Again shell (bash) and some associated commands (openssl, base64, xxd, etc.) can be a convenient and efficient solution for interacting with Kraken's markets (although it would not be the fastest implementation compared to compiled code).
Bash and the required commands are readily available (pre-installed) on most UNIX, Linux, and macOS systems, and can be easily installed on Windows and other systems, so all of the required software is available without using any additional third party API tools or libraries.
Using a plain text editor (even an old school editor such as vi) and a few lines of code, it is possible to access both public market data and private account data, and also to place/cancel orders on Kraken's markets.
 

Example Code

The following shows a bash code implementation for calling the REST API Balance endpoint (with syntax highlighting for ease of understanding):
 
Screenshot_2023-02-26_at_08.39.11.png
 
while the following provides the same code as plain text for copying/pasting:
 
#!/usr/bin/env bash

# API public/private keys copied from account management web site

api_key='gxTXpC4Ag/N0QPKlYnRhL1qVB2G/HZV1eB2drl7eOXga30dEKoB+EUMs'
api_private='62kRfRX7BI8G8T/jl7clnZ+vSfJt7YmQN23JQkJfHCE6oxecJX4fN4i2RitmRhyFzfJ4efKy2yCo4H068rfv0A=='

# API variables (URL, endpoint, nonce, etc.)

api_host='https://api.kraken.com'
api_endpoint='/0/private/Balance'
api_nonce=`date +%s`
api_post="nonce=$api_nonce"

# Authentication algorithm (SHA256 and HMAC SHA512)

api_private_hex=`echo -n $api_private | base64 -d | xxd -p | tr -d "\n"`
echo -n $api_endpoint > kapi_bash.bin
echo -n $api_nonce$api_post | openssl dgst -sha256 -binary >> kapi_bash.bin
api_sign=`cat kapi_bash.bin | openssl dgst -binary -sha512 -mac HMAC -macopt hexkey:$api_private_hex | base64`

# HTTP request (POST)

curl --header "API-Key: $api_key" --header "API-Sign: $api_sign" --data $api_post $api_host$api_endpoint
 
and the matching code can also be downloaded (as a kapi_bash.sh file).
 

Further Use/Development

To retrieve your own account balances, the example API key should be replaced with an API key from your own Kraken account, and additional API endpoints can be enabled simply by modifying the api_endpoint variable and the api_post variable (if needed).