Skip to main content

Documentation Index

Fetch the complete documentation index at: https://kraken-sandbox.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The Spot WebSocket API uses token-based authentication. To subscribe to private channels (ownTrades, openOrders), first obtain a short-lived token via the REST API, then include it in each subscription message.
Tokens are valid for 15 minutes. Obtain a fresh token before reconnecting or when the current one nears expiry.

Step 1: Obtain a WebSocket token

Call the GetWebSocketsToken REST endpoint using your API key and secret. Authentication uses the same HMAC-SHA512 signing as all other private REST endpoints — see REST Authentication for the full algorithm.
import urllib.parse
import hashlib
import hmac
import base64
import time
import requests

def get_kraken_signature(urlpath, data, secret):
    encoded = (str(data["nonce"]) + urllib.parse.urlencode(data)).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()
    mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    return base64.b64encode(mac.digest()).decode()

api_key = "YOUR_API_KEY"
api_secret = "YOUR_API_SECRET"

nonce = str(int(time.time() * 1000))
data = {"nonce": nonce}

headers = {
    "API-Key": api_key,
    "API-Sign": get_kraken_signature("/0/private/GetWebSocketsToken", data, api_secret),
}

response = requests.post(
    "https://api.kraken.com/0/private/GetWebSocketsToken",
    headers=headers,
    data=data,
)
token = response.json()["result"]["token"]
print(f"Token: {token}")

Step 2: Subscribe to a private channel

Include the token in your WebSocket subscription message:
{
  "event": "subscribe",
  "subscription": {
    "name": "ownTrades",
    "token": "WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu"
  }
}
A single token can be used for multiple subscriptions within the same session (e.g., subscribing to both ownTrades and openOrders simultaneously).

Token expiry and refresh

Tokens expire 15 minutes after creation. To maintain uninterrupted access:
  • Obtain a new token before the current one expires by calling GetWebSocketsToken again
  • After reconnecting, re-subscribe using the fresh token
For WebSocket reconnection patterns and session management, see the Reconnection guide.

Error handling

If you subscribe with an invalid or expired token, the server returns an error in the subscription status message:
{
  "errorMessage": "Token is expired",
  "event": "subscriptionStatus",
  "status": "error",
  "subscription": { "name": "ownTrades" }
}
On receiving this error, call GetWebSocketsToken to obtain a fresh token, then re-subscribe.