Skip to main content

Python client for Kraken API REST and Kraken Websockets API using httpx and websockets. Supports both sync and async for API REST.

Project description

Total alerts Language grade: Python

Kraky

Python client for Kraken API REST and Kraken Websockets API using httpx and websockets. Supports both sync and async for API REST.

Installation

pip install kraky

Docs

https://kraky.readthedocs.io/en/latest/

Usage

CLI

Kraky provides a CLI that matches the API function names and args.

You can use it like the following:

kraky get_ohlc_data pair=XBTUSD interval=240

You can replace get_ohlc_data by any kraky API function and pair=XBTUSD or interval=240 by any function argument. Please respect the format key=value.

It also supports .env files so you can also create a .env file with your api_key and secret and kraky will pick them up.

Example:

KRAKEN_API_KEY=""
KRAKEN_SECRET=""

Sync REST API

from kraky import KrakyApiClient


def get_web_sockets_token():
    kraken_api_key = ""
    kraken_secret = ""
    kraky_api_client = KrakyApiClient(
        api_key=kraken_api_key, secret=kraken_secret
    )

    ws_token = kraky_api_client.get_web_sockets_token()
    return ws_token

get_web_sockets_token()

Async REST API

from kraky import KrakyApiAsyncClient


async def get_web_sockets_token():
    kraken_api_key = ""
    kraken_secret = ""
    kraky_api_client = KrakyApiAsyncClient(
        api_key=kraken_api_key, secret=kraken_secret
    )

    ws_token = await kraky_api_client.get_web_sockets_token()
    return ws_token

asyncio.run(get_web_sockets_token)

Websocket

import asyncio
from kraky import KrakyApiAsyncClient, KrakyWsClient


async def get_web_sockets_token():
    kraken_api_key = ""
    kraken_secret = ""
    kraky_api_client = KrakyApiAsyncClient(
        api_key=kraken_api_key, secret=kraken_secret
    )

    ws_token = await kraky_api_client.get_web_sockets_token()
    return ws_token


async def public_handler(response):
    print(response)


async def private_handler(response):
    print(response)


async def main():

    interval = 30

    ws_pairs = ["XBT/USD", "ETH/USD"]

    ws_token = await get_ws_token()

    kraky_public_ws_client = KrakyWsClient("production")
    kraky_private_ws_client = KrakyWsClient("production-auth")

    asyncio.create_task(
        kraky_public_ws_client.connect(public_handler, connection_name="public")
    )

    asyncio.create_task(
        kraky_private_ws_client.connect(private_handler, connection_name="private")
    )

    await kraky_public_ws_client.subscribe(
        {"name": "ohlc", "interval": interval},
        ws_pairs,
        connection_name="public",
    )

    await kraky_private_ws_client.subscribe(
        {
            "interval": interval,
            "token": ws_token,
            "name": "openOrders",
        },
        connection_name="private",
    )


if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.create_task(main())
    loop.run_forever()

Compatibility

  • Python 3.7 and above

Licence

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kraky-2022.3.26.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

kraky-2022.3.26-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file kraky-2022.3.26.tar.gz.

File metadata

  • Download URL: kraky-2022.3.26.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for kraky-2022.3.26.tar.gz
Algorithm Hash digest
SHA256 2bf9b8e25f23b3d66f6136b2aa063a995f30a32784b0565fc01e462147266235
MD5 3db3cc2b3578f4ee89c9680044c56eac
BLAKE2b-256 951987fc1794cd71a30e97f4c88aaddbe3cf8eac949531560e9852f53a749eb1

See more details on using hashes here.

File details

Details for the file kraky-2022.3.26-py3-none-any.whl.

File metadata

  • Download URL: kraky-2022.3.26-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.1 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.12

File hashes

Hashes for kraky-2022.3.26-py3-none-any.whl
Algorithm Hash digest
SHA256 54536c3c773a445ab5c2c398b547d85dd8387be120e0e0196c8f024aca54bc4d
MD5 915a848bf6941d638f1b092abd67eeb8
BLAKE2b-256 8504f2d1f18b65154252ae6b886a93bc9d36a94dcc6ef4d476c47b39cc61c96e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page