Skip to main content

ELM Bittrade's Kraken REST package & optional CLI

Install

pip install bittrade-kraken-rest or poetry add bittrade-kraken-rest

Not all Kraken endpoints are implemented yet.

Public endpoints

from bittrade_kraken_rest import get_server_time

server_time = get_server_time().run()
print(server_time) # GetServerTimeResult(unixtime=1673053481, rfc1123='Sat, 07 Jan 23 01:04:41 +0000')

The above example is complete, it should run as is

Bring Your Own Credentials Signature (Private endpoints)

TLDR; Don't agree to pass your API secret to third-party code; instead sign the requests yourself, with your own code. It's safer.

This library doesn't want to ever access your Kraken secret keys.

Most libraries expect you to provide your api key and secret. I'm not comfortable doing that with third-party code, even open sourced.

Here instead, the library prepares the request, which you then sign using your own code and the library finishes the job. It has NO access to your secret.

Thankfully this is quite straightforward: you need to implement a sign(x: tuple[PreparedRequest, str, dict[str, Any]]) -> PreparedRequest method which sets the correct headers. Below is an example of such a signature function:

from os import getenv
import urllib, hmac, base64, hashlib
from pathlib import Path

# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature
def generate_kraken_signature(urlpath, data, secret):
    post_data = urllib.parse.urlencode(data)
    encoded = (str(data["nonce"]) + post_data).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    signature_digest = base64.b64encode(mac.digest())
    return signature_digest.decode()

# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration
def sign(x: tuple[PreparedRequest, str, dict[str, Any]]):
    request, url, data = x
    request.headers["API-Key"] = Path("./config_local/key").read_text()
    request.headers["API-Sign"] = generate_kraken_signature(
        url, data, Path("./config_local/secret").read_text()
    )
    return request

With that in place, a observable pipe will get you the result you need:

from bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result
from reactivex import operators

result = get_websockets_token_request().pipe(
    operators.map(sign),
    get_websockets_token_result()
).run()

Full example with signature function

from os import getenv
import urllib, hmac, base64, hashlib
from pathlib import Path
from bittrade_kraken_rest import get_websockets_token_request, get_websockets_token_result
from reactivex import operators

# Taken from https://docs.kraken.com/rest/#section/Authentication/Headers-and-Signature
def generate_kraken_signature(urlpath, data, secret):
    post_data = urllib.parse.urlencode(data)
    encoded = (str(data["nonce"]) + post_data).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    signature_digest = base64.b64encode(mac.digest())
    return signature_digest.decode()

# Here the key/secret are loaded from a .gitignored folder, but you can use environment variables or other method of configuration
def sign(x: tuple[PreparedRequest, str, dict[str, Any]]):
    request, url, data = x
    request.headers["API-Key"] = Path("./config_local/key").read_text()
    request.headers["API-Sign"] = generate_kraken_signature(
        url, data, Path("./config_local/secret").read_text()
    )
    return request

result = get_websockets_token_request().pipe(
    operators.map(sign),
    get_websockets_token_result()
).run()

The above example is complete, it should run as is

Observables

The above examples use .run() to trigger the observable subscription but Observables make it very easy to create pipes, retries and more. All operators can be found on the RxPy read the docs.

Tests

pytest

Note that integration tests require a valid key/secret pair saved as key and secret files in a .config_local folder placed at the root of the repo.

CLI

The CLI has been moved to its own repo

Metadata

Release files for bittrade-kraken-rest 0.13.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bittrade-kraken-rest 0.13.15
File Size Uploaded Provenance
bittrade_kraken_rest-0.13.15.tar.gz 13.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for bittrade-kraken-rest 0.13.15
File Interpreter ABI Platform
bittrade_kraken_rest-0.13.15-py3-none-any.whl Python 3 none any Details

Total release size: 37.4 kB

Release files / bittrade_kraken_rest-0.13.15.tar.gz

Download URL bittrade_kraken_rest-0.13.15.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
b879bbfe36ebaaf023e27ecab54018c3dd06ef41c40660516265fa6e1faf57f2
BLAKE2b-256 checksum
How to use checksums
a33d6ef26676948952f71a3682384109f88a233d6423510b1de15705a020f059
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.5 Darwin/22.4.0

Release files / bittrade_kraken_rest-0.13.15-py3-none-any.whl

Download URL bittrade_kraken_rest-0.13.15-py3-none-any.whl
Size 24.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b16e84ef24ea8e76a1cd34d952319cc6c6fd514326a260433e95aaab18b48c6e
BLAKE2b-256 checksum
How to use checksums
cff3a9dbad79ec96ecacdc9a13ab685b43dfdef0b0ddf7a12c7fb19f93fe8eef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.1 CPython/3.10.5 Darwin/22.4.0

Security for bittrade-kraken-rest 0.13.15

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page