Skip to content

Instantly share code, notes, and snippets.

@derberg
Last active December 15, 2022 10:49
Show Gist options
  • Save derberg/4e419d6ff5870c7c3f5f443e8bd30535 to your computer and use it in GitHub Desktop.
Save derberg/4e419d6ff5870c7c3f5f443e8bd30535 to your computer and use it in GitHub Desktop.
Experiment where I try to write AsyncAPI document for public API that belongs to Kraken. It is not full document as the API has some private parts and also a lot of additional messages that are also very specific to currency trading and I didn't want to deal with those.
asyncapi: 2.0.0
info:
title: Kraken Websockets API
version: '1.8.0'
description: |
WebSockets API offers real-time market data updates. WebSockets is a bidirectional protocol offering fastest real-time data, helping you build real-time applications. The public message types presented below do not require authentication. Private-data messages can be subscribed on a separate authenticated endpoint.
### General Considerations
- TLS with SNI (Server Name Indication) is required in order to establish a Kraken WebSockets API connection. See Cloudflare's [What is SNI?](https://www.cloudflare.com/learning/ssl/what-is-sni/) guide for more details.
- All messages sent and received via WebSockets are encoded in JSON format
- All decimal fields (including timestamps) are quoted to preserve precision.
- Timestamps should not be considered unique and not be considered as aliases for transaction IDs. Also, the granularity of timestamps is not representative of transaction rates.
- At least one private message should be subscribed to keep the authenticated client connection open.
- Please use REST API endpoint [AssetPairs](https://www.kraken.com/features/api#get-tradable-pairs) to fetch the list of pairs which can be subscribed via WebSockets API. For example, field 'wsname' gives the supported pairs name which can be used to subscribe.
- Cloudflare imposes a connection/re-connection rate limit (per IP address) of approximately 150 attempts per rolling 10 minutes. If this is exceeded, the IP is banned for 10 minutes.
- Recommended reconnection behaviour is to (1) attempt reconnection instantly up to a handful of times if the websocket is dropped randomly during normal operation but (2) after maintenance or extended downtime, attempt to reconnect no more quickly than once every 5 seconds. There is no advantage to reconnecting more rapidly after maintenance during cancel_only mode.
servers:
public:
url: ws.kraken.com
protocol: wss
description: |
Public server available without authorization.
Once the socket is open you can subscribe to a public channel by sending a subscribe request message.
private:
url: ws-auth.kraken.com
protocol: wss
description: |
Private server that requires authorization.
Once the socket is open you can subscribe to private-data channels by sending an authenticated subscribe request message.
The API client must request an authentication "token" via the following REST API endpoint "GetWebSocketsToken" to connect to WebSockets Private endpoints. For more details read https://support.kraken.com/hc/en-us/articles/360034437672-How-to-retrieve-a-WebSocket-authentication-token-Example-code-in-Python-3
The resulting token must be provided in the "token" field of any new private WebSocket feed subscription:
```
{
"event": "subscribe",
"subscription":
{
"name": "ownTrades",
"token": "WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu"
}
}
```
channels:
/:
publish:
description: Send messages to the API
operationId: processReceivedMessage
message:
oneOf:
- $ref: '#/components/messages/ping'
- $ref: '#/components/messages/subscribe'
- $ref: '#/components/messages/unsubscribe'
subscribe:
description: Messages that you receive from the API
operationId: sendMessage
message:
oneOf:
- $ref: '#/components/messages/pong'
- $ref: '#/components/messages/heartbeat'
- $ref: '#/components/messages/systemStatus'
- $ref: '#/components/messages/subscriptionStatus'
components:
messages:
ping:
summary: Ping server to determine whether connection is alive
description: Client can ping server to determine whether connection is alive, server responds with pong. This is an application level ping as opposed to default ping in websockets standard which is server initiated
payload:
$ref: '#/components/schemas/ping'
x-response:
$ref: '#/components/messages/pong'
heartbeat:
description: Server heartbeat sent if no subscription traffic within 1 second (approximately)
payload:
$ref: '#/components/schemas/heartbeat'
pong:
summary: Pong is a response to ping message
description: Server pong response to a ping to determine whether connection is alive. This is an application level pong as opposed to default pong in websockets standard which is sent by client in response to a ping
payload:
$ref: '#/components/schemas/pong'
systemStatus:
description: Status sent on connection or system status changes.
payload:
$ref: '#/components/schemas/systemStatus'
examples:
- payload:
connectionID: 8628615390848610000
event: systemStatus
status: online
version: 1.0.0
subscribe:
description: Subscribe to a topic on a single or multiple currency pairs.
payload:
$ref: '#/components/schemas/subscribe'
examples:
- payload:
event: subscribe
pair:
- XBT/USD
- XBT/EUR
subscription:
name: ticker
- payload:
event: subscribe
subscription:
name: ownTrades
token: WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu
x-response:
$ref: '#/components/messages/subscriptionStatus'
unsubscribe:
description: Unsubscribe, can specify a channelID or multiple currency pairs.
payload:
$ref: '#/components/schemas/unsubscribe'
examples:
- payload:
event: unsubscribe
pair:
- XBT/EUR
- XBT/USD
subscription:
name: ticker
- payload:
event: unsubscribe
subscription:
name: ownTrades
token: WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu
x-response:
$ref: '#/components/messages/subscriptionStatus'
subscriptionStatus:
description: Subscription status response to subscribe, unsubscribe or exchange initiated unsubscribe.
payload:
$ref: '#/components/schemas/subscriptionStatus'
examples:
- payload:
channelID: 10001
channelName: ohlc-5
event: subscriptionStatus
pair: ['XBT/EUR']
reqid: 42
status: unsubscribed
subscription:
interval: 5
name: ohlc
- payload:
errorMessage: Subscription depth not supported
event: subscriptionStatus
pair: ['XBT/USD']
status: error
subscription:
depth: 25
name: book
schemas:
ping:
type: object
properties:
event:
type: string
const: ping
reqid:
$ref: '#/components/schemas/reqid'
required:
- event
heartbeat:
type: object
properties:
event:
type: string
const: heartbeat
pong:
type: object
properties:
event:
type: string
const: pong
reqid:
$ref: '#/components/schemas/reqid'
systemStatus:
type: object
properties:
event:
type: string
const: systemStatus
connectionID:
type: integer
description: The ID of the connection
status:
$ref: '#/components/schemas/status'
version:
type: string
status:
type: string
enum:
- online
- maintenance
- cancel_only
- limit_only
- post_only
- error
- unsubscribed
subscribe:
type: object
properties:
event:
type: string
const: subscribe
reqid:
$ref: '#/components/schemas/reqid'
pair:
$ref: '#/components/schemas/pair'
subscription:
type: object
properties:
depth:
$ref: '#/components/schemas/depth'
interval:
$ref: '#/components/schemas/interval'
name:
$ref: '#/components/schemas/name'
ratecounter:
$ref: '#/components/schemas/ratecounter'
snapshot:
$ref: '#/components/schemas/snapshot'
token:
$ref: '#/components/schemas/token'
required:
- name
required:
- event
unsubscribe:
type: object
properties:
event:
type: string
const: unsubscribe
reqid:
$ref: '#/components/schemas/reqid'
pair:
$ref: '#/components/schemas/pair'
subscription:
type: object
properties:
depth:
$ref: '#/components/schemas/depth'
interval:
$ref: '#/components/schemas/interval'
name:
$ref: '#/components/schemas/name'
token:
$ref: '#/components/schemas/token'
required:
- name
required:
- event
subscriptionStatus:
type: object
oneOf:
- $ref: '#/components/schemas/subscriptionStatusError'
- $ref: '#/components/schemas/subscriptionStatusSuccess'
subscriptionStatusError:
allOf:
- properties:
errorMessage:
type: string
required:
- errorMessage
- $ref: '#/components/schemas/subscriptionStatusCommon'
subscriptionStatusSuccess:
allOf:
- properties:
channelID:
type: integer
description: ChannelID on successful subscription, applicable to public messages only.
channelName:
type: string
description: Channel Name on successful subscription. For payloads 'ohlc' and 'book', respective interval or depth will be added as suffix.
required:
- channelID
- channelName
- $ref: '#/components/schemas/subscriptionStatusCommon'
subscriptionStatusCommon:
type: object
required:
- event
properties:
event:
type: string
const: subscriptionStatus
reqid:
$ref: '#/components/schemas/reqid'
pair:
$ref: '#/components/schemas/pair'
status:
$ref: '#/components/schemas/status'
subscription:
required:
- name
type: object
properties:
depth:
$ref: '#/components/schemas/depth'
interval:
$ref: '#/components/schemas/interval'
maxratecount:
$ref: '#/components/schemas/maxratecount'
name:
$ref: '#/components/schemas/name'
token:
$ref: '#/components/schemas/token'
interval:
type: integer
description: Time interval associated with ohlc subscription in minutes.
default: 1
enum:
- 1
- 5
- 15
- 30
- 60
- 240
- 1440
- 10080
- 21600
name:
type: string
description: The name of the channel you subscribe too.
enum:
- book
- ohlc
- openOrders
- ownTrades
- spread
- ticker
- trade
token:
type: string
description: base64-encoded authentication token for private-data endpoints.
depth:
type: integer
default: 10
enum:
- 10
- 25
- 100
- 500
- 1000
description: Depth associated with book subscription in number of levels each side.
maxratecount:
type: integer
description: Max rate-limit budget. Compare to the ratecounter field in the openOrders updates to check whether you are approaching the rate limit.
ratecounter:
type: boolean
default: false
description: Whether to send rate-limit counter in updates (supported only for openOrders subscriptions)
snapshot:
type: boolean
default: true
description: Whether to send historical feed data snapshot upon subscription (supported only for ownTrades subscriptions)
reqid:
type: integer
description: client originated ID reflected in response message.
pair:
type: array
description: Array of currency pairs.
items:
type: string
description: Format of each pair is "A/B", where A and B are ISO 4217-A3 for standardized assets and popular unique symbol if not standardized.
pattern: '[A-Z\s]+\/[A-Z\s]+'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment