Base Clients and Internals

The following classes and data structures are listed for completeness. Please avoid using them since these are internals and may change without any warning.

They are the base classes for Spot and Futures REST and websocket clients.

class kraken.base_api.KrakenSpotBaseAPI(key: str = '', secret: str = '', url: str = '', *, sandbox: bool = False, use_custom_exceptions: bool = True)

Bases: object

This class the the base for all Spot clients, handles un-/signed requests and returns exception handled results.

If you are facing timeout errors on derived clients, you can make use of the TIMEOUT attribute to deviate from the default 10 seconds.

Parameters:
  • key (str, optional) – Spot API public key (default: "")

  • secret (str, optional) – Spot API secret key (default: "")

  • url (str, optional) – URL to access the Kraken API (default: https://api.kraken.com)

  • sandbox (bool, optional) – Use the sandbox (not supported for Spot trading so far, default: False)

property return_unique_id: str

Returns a unique uuid string

Returns:

uuid

Return type:

str

class kraken.base_api.KrakenFuturesBaseAPI(key: str = '', secret: str = '', url: str = '', *, sandbox: bool = False, use_custom_exceptions: bool = True)

Bases: object

The base class for all Futures clients handles un-/signed requests and returns exception handled results.

If you are facing timeout errors on derived clients, you can make use of the TIMEOUT attribute to deviate from the default 10 seconds.

If the sandbox environment is chosen, the keys must be generated from here:

https://demo-futures.kraken.com/settings/api

Parameters:
  • key (str, optional) – Futures API public key (default: "")

  • secret (str, optional) – Futures API secret key (default: "")

  • url (str, optional) – The URL to access the Futures Kraken API (default: https://futures.kraken.com)

  • sandbox (bool, optional) – If set to True the URL will be https://demo-futures.kraken.com (default: False)

class kraken.spot.websocket.KrakenSpotWSClientBase(key: str = '', secret: str = '', callback: Callable | None = None, api_version: str = 'v2', *, no_public: bool = False, beta: bool = False)

Bases: KrakenSpotBaseAPI

This is the base class for kraken.spot.KrakenSpotWSClientV1 and kraken.spot.KrakenSpotWSClientV2. It extends the REST API base class and is used to provide the base functionalities that are used for Kraken Websocket API v1 and v2.

This is an internal class and should not be used outside.

Parameters:
  • key (str, optional) – API Key for the Kraken Spot API (default: "")

  • secret (str, optional) – Secret API Key for the Kraken Spot API (default: "")

  • url (str, optional) – Set a specific URL to access the Kraken REST API

  • no_public – Disables public connection (default: False). If not set or set to False, the client will create a public and a private connection per default. If only a private connection is required, this parameter should be set to True.

  • beta (bool) – Use the beta websocket channels (maybe not supported anymore, default: False)

property active_private_subscriptions: list[dict] | Any

Returns the active private subscriptions

Returns:

List of active private subscriptions

Return type:

list[dict] | Any

Raises:

ConnectionError – If there is no active private connection

property active_public_subscriptions: list[dict] | Any

Returns the active public subscriptions

Returns:

List of active public subscriptions

Return type:

list[dict] | Any

Raises:

ConnectionError – If there is no active public connection.

get_ws_token() dict

Get the authentication token to establish the authenticated websocket connection. This is used internally and in most cases not needed outside.

Returns:

The authentication token

Return type:

dict

async on_message(message: dict | list) None

Calls the defined callback function (if defined). In most cases you have to overwrite this function since it will receive all incoming messages that will be sent by Kraken.

See kraken.spot.KrakenSpotWSClientV1 and kraken.spot.KrakenSpotWSClientV2 for examples to use this function.

Parameters:

message (dict | list) – The message received sent by Kraken via the websocket connection

property private_channel_names: list[str]

This function must be overloaded and return a list of names that can be subscribed to (for authenticated connections).

property public_channel_names: list[str]

This function must be overloaded and return a list of names that can be subscribed to (for unauthenticated connections).

property return_unique_id: str

Returns a unique uuid string

Returns:

uuid

Return type:

str

async send_message(*args: Any, **kwargs: Any) None

This functions must be overloaded and should be used to send messages via the websocket connection(s).

async subscribe(*args: Any, **kwargs: Any) None

This function must be overloaded and should be used to subscribe to websocket channels/feeds.

async unsubscribe(*args: Any, **kwargs: Any) None

This function must be overloaded and should be used to unsubscribe from websocket channels/feeds.

class kraken.spot.websocket.connectors.ConnectSpotWebsocketV1(client: KrakenSpotWSClientBase, endpoint: str, callback: Any, *, is_auth: bool = False)

Bases: ConnectSpotWebsocketBase

This class extends the kraken.spot.websocket.connectors.ConnectSpotWebsocketBase and can be instantiated to create and maintain a websocket connection using the Kraken Websocket API v1.

This is an internal class and should not be used outside.

Parameters:
  • client (kraken.spot.KrakenSpotWSClientBase) – The websocket client that wants to connect

  • endpoint (str) – The websocket endpoint

  • callback (function) – Callback function that receives the websocket messages

  • is_auth (bool, optional) – If the websocket connects to endpoints that require authentication (default: False)

property client: KrakenSpotWSClientBase

Return the websocket client

async close_connection() None

Closes the websocket connection and thus forces a reconnect

property is_auth: bool

Returns True if the connection can access privat endpoints

async send_ping() None

Sends ping to Kraken

property subscriptions: list[dict]

Returns a copy of active subscriptions

class kraken.spot.websocket.connectors.ConnectSpotWebsocketV2(client: KrakenSpotWSClientBase, endpoint: str, callback: Any, *, is_auth: bool = False)

Bases: ConnectSpotWebsocketBase

This class extends the kraken.spot.websocket.connectors.ConnectSpotWebsocketBase and can be instantiated to create and maintain a websocket connection using the Kraken Websocket API v2.

This is an internal class and should not be used outside.

Parameters:
  • client (kraken.spot.KrakenSpotWSClientBase) – The websocket client that wants to connect

  • endpoint (str) – The websocket endpoint

  • callback (function) – Callback function that receives the websocket messages

  • is_auth (bool, optional) – If the websocket connects to endpoints that require authentication (default: False)

property client: KrakenSpotWSClientBase

Return the websocket client

async close_connection() None

Closes the websocket connection and thus forces a reconnect

property is_auth: bool

Returns True if the connection can access privat endpoints

async send_ping() None

Sends ping to Kraken

property subscriptions: list[dict]

Returns a copy of active subscriptions