This step-by-step guide is a continuation of a series of articles about WebSockets. I recommend reading WebSocket, Shrek, and AsyncAPI - An Opinionated Intro first.
If you do not want to read this article, then watch the recording of the live stream about the same:
All roads lead to Rome, but all those roads are different. First, you need to identify where you are and what is the purpose of your journey. What is your goal? What do you want to use AsyncAPI for?
You may invest in using the specification for many different reasons, like for example:
- documentation
- testing
- mocking
- code generation
- message validation
Depending on your goal, you might need to take different roads to get there. If your only goal is documentation, you might take a different approach to writing an AsyncAPI file than you would take while thinking about code generation.
Choosing the right road to Rome
Let's say AsyncAPI does not fully cover your use case. You are missing some extra property. You are disappointed that you cannot explicitly provide information that your production servers both support different channels. Server A supports channel AA and AB, while Server B supports channel BA and BB. It is not currently possible with the specification as the assumption is that your application communicates with servers that support the same channels.
There are two roads to Rome:
Road docs-only: You need AsyncAPI for docs generation only and have no intention of sharing the source document with anyone. It means you do not need to bother much about inventing some specification extension. You can just add missing information to the description of a given object.
Road automation: You need AsyncAPI for docs and code generation, which means that all details in your AsyncAPI document must be machine-readable. You can't just put unsupported information in the description.
Kraken API use case
I'm going to guide you through the process of creating an AsyncAPI document. I'll use the example of Kraken API mentioned in my previous article.
The challenge I had here was that I'm trying to document an API basing on public docs with no access to a subject matter expert. I also have zero understanding of the cryptocurrency industry and still do not fully understand the vocabulary.
Message to Kraken API developers and technical writers
In case you want to continue the work I started on the AsyncAPI document for Kraken API, feel free to do that. I'm happy to help, just let me know. Reach me out in our AsyncAPI Slack workspace.
More interesting here are the technical challenges though, caused by the fact that Kraken's API:
- has two production servers for non-secure and secure message exchange
- some messages are supported only by the public and some only by a private server
- has just one entry point for communication. You do not get specific messages from one of many endpoints. You get specific messages after first sending a subscription message. Meaning you have a request message and you get a reply message, so something that is not yet possible to describe with AsyncAPI in a machine-readable way
Writing a single AsyncAPI document
Because of all these different challenges, I took the docs-only road described in section Choosing the right road to Rome. No worries though, I give tips for the automation road too.
Basic information about the API
First, provide some basic information that every good AsyncAPI file should have:
- What AsyncAPI version do you use?
- What is the name of your API?
- What version of the API you describe?
- Do not underestimate the description. Optional != not needed. AsyncAPI supports markdown in descriptions. Provide long generic documentation for your API. Benefit from markdown features to structure it, so it is easier to read
In case you think using just one property to add overarching documentation for your API is very limiting, I agree with you 😃 Join discussion here. I believe spec should have better support for docs, and we should first explore it with specification extensions. To be honest, I always thought documentation deserves its specification, but I don't want to bother you with my wicked visions now.
1asyncapi: 2.0.0
2info:
3 title: Kraken Websockets API
4 version: '1.8'
5 description: |
6 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.
7
8 ### General Considerations
9
10 - 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.
11 - All messages sent and received via WebSockets are encoded in JSON format.
12 - All decimal fields (including timestamps) are quoted to preserve precision.
13 - 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.
14 - At least one private message should be subscribed to keep the authenticated client connection open.
15 - 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.
16 - 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.
17 - 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.
Provide server information
Describe how to connect to the API:
- What is the URL of the server?
- Is there any authorization in place?
- What is the protocol requirement, is SSL connection required?
The Kraken API is an excellent example of how different WebSocket implementations can be and that there is never one way to design your architecture. It all depends on your requirements, the use cases that drive your product.
Describing multiple servers
Below you can notice two different servers. These are not, as you might think, production and development servers. Here you have a clear division between publicly available data and private-only data. In other words, users use two different servers, not channels/paths/endpoints, to talk to the API.
1servers:
2 public:
3 url: ws.kraken.com
4 protocol: wss
5 description: |
6 Public server available without authorization.
7 Once the socket is open you can subscribe to a public channel by sending a subscribe request message.
8 private:
9 url: ws-auth.kraken.com
10 protocol: wss
11 description: |
12 Private server that requires authorization.
13 Once the socket is open you can subscribe to private-data channels by sending an authenticated subscribe request message.
You can verify if above is true by connecting to ws.kraken.com and trying to subscribe to one of the event streams that require a token:
{ "event": "subscribe", "subscription": { "name": "ownTrades", "token": "WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu" } }
In response you get an error:
{"errorMessage":"Private data and trading are unavailable on this endpoint. Try ws-auth.kraken.com","event":"subscriptionStatus","status":"error","subscription":{"name":"ownTrades","token":"WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu"}}
In the documentation, they also indicate beta servers like
beta-ws.kraken.com
. It is hard to understand their purpose, so I did not put them in the AsyncAPI document. For me, beta means something new, some upgrades, and I would consider writing a separate AsyncAPI document.
Is it reasonable to describe API that has two different production servers in one AsyncAPI? As usual, it depends. For docs-only road described in section Choosing the right road to Rome, you can "workaround" some AsyncAPI features if they do not support your use case. Check out, for example, what I had to do in section Server security where I was not sure how to describe the specific security of the private server. Short answer: just extend the description.
For automation road described in Choosing the right road to Rome section, you need a machine-readable structure. In case you have messages that can be consumed only by the private server, you need a way to specify that the given message can be published only to the private server. It is exactly the case with Kraken API.
Imagine you want to read the AsyncAPI document in real-time in your server and validate all incoming messages. Take server ws.kraken.com. The only way to emit errors like Private data and trading are unavailable on this endpoint. Try ws-auth.kraken.com
is by writing the code that handles validation manually. You can't generate that as the AsyncAPI file does not specify what messages can go to ws.kraken.com and what messages can't.
Why?
At the moment, in AsyncAPI, you don't have a way to "wire" a server with a message, operation, or a channel. There are no default properties that allow you to provide information that message with the name ownTrades can only be sent to ws-auth.kraken.com
server.
Solution?
Create two AsyncAPI documents. Treat those two servers as separate services that share messages and schemas. Use $ref feature to cross-reference schemas.
Server security
You can use AsyncAPI also to describe the security of your API. You can describe in a machine-readable way the security mechanism that protects the server. Several security schemes are supported. In Kraken's case, I could not figure out what kind of security scheme they use from their docs. They seem to have a non-standard set up for getting the authorization token, which is why the only option was to put a human-readable-only description there.
1servers:
2 public:
3 url: ws.kraken.com
4 protocol: wss
5 description: |
6 Public server available without authorization.
7 Once the socket is open, you can subscribe to a public channel by sending a subscribe request message.
8 private:
9 url: ws-auth.kraken.com
10 protocol: wss
11 description: |
12 Private server that requires authorization.
13 Once the socket is open, you can subscribe to private-data channels by sending an authenticated subscribe request message.
14
15 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
16
17 The resulting token must be provided in the "token" field of any new private WebSocket feed subscription:
18 ```
19 {
20 "event": "subscribe",
21 "subscription":
22 {
23 "name": "ownTrades",
24 "token": "WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu"
25 }
26 }
27 ```
Endpoints aka Channels
I saw WebSocket APIs that provide different streams of messages on separate endpoints. It is often the case when you build the WebSocket API for the frontend only and design it for different UI views. In the case of Kraken API we have no endpoints. You connect to the root of the server.
No matter what setup you have, just remember you should use channels to describe it. In the case of connecting to the root, it is as simple as:
1channels:
2 /:
Multiple different messages on the same channel
You can have one or many different messages coming to your channel. Like in the case of Kraken API, you can even have multiple messages, incoming and outgoing. You can describe it using oneOf on message object as you can see below:
1channels:
2 /:
3 publish:
4 operationId: sendMessage
5 message:
6 oneOf:
7 - $ref: '#/components/messages/ping'
8 - $ref: '#/components/messages/subscribe'
9 - $ref: '#/components/messages/unsubscribe'
10 subscribe:
11 operationId: processMessage
12 message:
13 oneOf:
14 - $ref: '#/components/messages/pong'
15 - $ref: '#/components/messages/heartbeat'
16 - $ref: '#/components/messages/systemStatus'
17 - $ref: '#/components/messages/subscriptionStatus'
Hold on! Where did these publish and subscribe keywords came from.
When we talk about WebSocket, we usually do not use words like subscribe and publish, as we do not think about producers and consumers. Just check out the protocol RfC. We are used to sending and receiving messages.
WebSocket term | AsyncAPI term | Meaning from API server perspective | Meaning from API user perspective |
---|---|---|---|
Send | Publish | The API server receives the given message. | The API user can send a given message to the API server. |
Receive | Subscribe | The API server sends a given message. | The API user can receive a given message from the API server. |
Messages definition
In event-driven architectures (EDA) it's all about the event, right? The message passed in the system. You need to specify many details about the message, like its payload structure, headers, purpose, and many others.
Above all, always remember to have good examples. Please don't count on the autogenerated ones, as in most cases, they're useless.
1messages:
2 systemStatus:
3 description: Status sent on connection or system status changes.
4 payload:
5 $ref: '#/components/schemas/systemStatus'
6 examples:
7 - payload:
8 connectionID: 8628615390848610000
9 event: systemStatus
10 status: online
11 version: 1.0.0
Describe responses - specification extensions
Describe responses? What responses?
It is EDA. Who cares about responses, right? Fire and forget rules!
The thing is that request and reply pattern is also used in EDA. This is also the case with Kraken API where communication goes through a single channel with multiple different messages. One message triggers another message in response.
The simplest example is the message ping that triggers a pong reply. The current AsyncAPI limitation is that you cannot specify that once the user sends (publish) message ping, the pong message is received in a reply. Look at this thread to participate in an ongoing discussion about request/reply pattern support in AsyncAPI.
For docs-only road from section Choosing the right road to Rome, I would be lazy and just put such info in the description of both messages. Even though this is an error-prone approach, I would just make my life easier. For automation road I would choose to use a specification extension.
What is specification extension?
You can extend every AsyncAPI object in the AsyncAPI document with extra properties. You only need to prefix them with x-
. You can also share extensions or reuse extensions from others thanks to extensions catalog.
In the below document, you will notice that for the request/reply pattern, I use AsyncAPI specification extensions called x-response.
1messages:
2 ping:
3 summary: Ping server to determine whether connection is alive
4 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
5 payload:
6 $ref: '#/components/schemas/ping'
7 x-response:
8 $ref: '#/components/messages/pong'
Even though the reference to another object is provided inside the extension that is not part of AsyncAPI, our parser will resolve it correctly. It means that under x-response property, I will have access to the entire message object.
Schemas vs JSON Schema
Because the message itself is most important in the entire EDA, you need to describe the message payload properly.
AsyncAPI allows you to provide payload information in different schema formats. The default format is AsyncAPI Schema that is a superset of JSON Schema. You can use others too, like Avro, for example.
From the AsyncAPI document point of view, the most important is that you can reuse schemas. In other words, instead of providing data directly to the payload object, you can $ref them from components.schemas or even an external document. Just DRY, right?
The rest, I would say, has nothing to do with AsyncAPI itself. How you structure schemas depends on you and the schema format that you use. It is why the next sections of my article describe something specific, not for the AsyncAPI itself but rather JSON Schema.
Simplest example of schemas from Kraken API is a payload for ping message:
1schemas:
2 ping:
3 type: object
4 properties:
5 event:
6 type: string
7 const: ping
8 reqid:
9 $ref: '#/components/schemas/reqid'
10 required:
11 - event
12 reqid:
13 type: integer
14 description: client originated ID reflected in response message.
You can see that ping message is an object that has two properties where only one is required. One property is used across other messages, so is part of many different schemas, so better to keep its definition as a separate schema and reference where needed.
Schemas complexity
Splitting schemas into reusable chunks with $ref usage is not something complex. It gets complex when messages are complex, when you get different message payload depending on system behavior.
Kraken API has a subscriptionStatus message where payload depends on the success of the subscription. In case of successful subscription, you get a message with channelID and channelName properties, but in case of failure, the message doesn't contain these properties but in exchange has errorMessage. In other words, some properties are mutually exclusive.
1 subscriptionStatus:
2 type: object
3 oneOf:
4 - required:
5 - errorMessage
6 not:
7 required:
8 - channelID
9 - channelName
10 - required:
11 - channelID
12 - channelName
13 not:
14 required:
15 - errorMessage
16 properties:
17 channelID:
18 type: integer
19 description: ChannelID on successful subscription, applicable to public messages only.
20 channelName:
21 type: string
22 description: Channel Name on successful subscription. For payloads 'ohlc' and 'book', respective interval or depth will be added as suffix.
23 errorMessage:
24 type: string
25 event:
26 type: string
27 const: subscriptionStatus
28 reqid:
29 $ref: '#/components/schemas/reqid'
30 pair:
31 $ref: '#/components/schemas/pair'
32 status:
33 $ref: '#/components/schemas/status'
34 subscription:
35 type: object
36 properties:
37 depth:
38 $ref: '#/components/schemas/depth'
39 interval:
40 $ref: '#/components/schemas/interval'
41 maxratecount:
42 $ref: '#/components/schemas/maxratecount'
43 name:
44 $ref: '#/components/schemas/name'
45 token:
46 $ref: '#/components/schemas/token'
47 required:
48 - name
49 required:
50 - event
It is what I call a complex schema, where good JSON Schema knowledge is needed. The problem with complex schemas is that not many tools support these kinds of schemas. By the time I write this article, our AsyncAPI tools for documentation rendering will fail to render the above schema correctly.
It is why you sometimes need compromises and adjusts schemas, so they get proper tooling support. Below you can see the same schema but structured in a more straightforward way supported by most tools.
1 subscriptionStatus:
2 type: object
3 oneOf:
4 - $ref: '#/components/schemas/subscriptionStatusError'
5 - $ref: '#/components/schemas/subscriptionStatusSuccess'
6 subscriptionStatusError:
7 allOf:
8 - properties:
9 errorMessage:
10 type: string
11 required:
12 - errorMessage
13 - $ref: '#/components/schemas/subscriptionStatusCommon'
14 subscriptionStatusSuccess:
15 allOf:
16 - properties:
17 channelID:
18 type: integer
19 description: ChannelID on successful subscription, applicable to public messages only.
20 channelName:
21 type: string
22 description: Channel Name on successful subscription. For payloads 'ohlc' and 'book', respective interval or depth will be added as suffix.
23 required:
24 - channelID
25 - channelName
26 - $ref: '#/components/schemas/subscriptionStatusCommon'
27 subscriptionStatusCommon:
28 type: object
29 required:
30 - event
31 properties:
32 event:
33 type: string
34 const: subscriptionStatus
35 reqid:
36 $ref: '#/components/schemas/reqid'
37 pair:
38 $ref: '#/components/schemas/pair'
39 status:
40 $ref: '#/components/schemas/status'
41 subscription:
42 required:
43 - name
44 type: object
45 properties:
46 depth:
47 $ref: '#/components/schemas/depth'
48 interval:
49 $ref: '#/components/schemas/interval'
50 maxratecount:
51 $ref: '#/components/schemas/maxratecount'
52 name:
53 $ref: '#/components/schemas/name'
54 token:
55 $ref: '#/components/schemas/token'
I managed to get a structure that will be nicely rendered in the UI. Even code generation will work well. It is a bit more complex than initial structure, although this is rather subjective personal-taste-like opinion.
Let's have a look at the final document
Websocket protocol is very flexible, and therefore you can implement the server in many different ways. The path that Kraken API took is complex but not impossible to describe with the AsyncAPI document. Look at the document's final structure and keep in mind that it is not a complete document for Kraken API and the road that I chose to get to Rome was to focus on documentation rendering only.
For automation road described in section Choosing the right road to Rome, the document should be split into two documents: one for private and one for public servers. Common parts, like common messages and schemas, should be stored in separate files and referred from these two AsyncAPI documents using $ref. Another solution would be to use specification extensions to describe relations between messages and servers.
You can open this document directly in AsyncAPI Studio by clicking this link. Compare it also with the original documentation.
1asyncapi: 2.0.0
2
3info:
4 title: Kraken Websockets API
5 version: '1.8.0'
6 description: |
7 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.
8
9 ### General Considerations
10
11 - 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.
12 - All messages sent and received via WebSockets are encoded in JSON format
13 - All decimal fields (including timestamps) are quoted to preserve precision.
14 - 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.
15 - At least one private message should be subscribed to keep the authenticated client connection open.
16 - 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.
17 - 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.
18 - 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.
19
20servers:
21 public:
22 url: ws.kraken.com
23 protocol: wss
24 description: |
25 Public server available without authorization.
26 Once the socket is open you can subscribe to a public channel by sending a subscribe request message.
27 private:
28 url: ws-auth.kraken.com
29 protocol: wss
30 description: |
31 Private server that requires authorization.
32 Once the socket is open you can subscribe to private-data channels by sending an authenticated subscribe request message.
33
34 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
35
36 The resulting token must be provided in the "token" field of any new private WebSocket feed subscription:
37 ```
38 {
39 "event": "subscribe",
40 "subscription":
41 {
42 "name": "ownTrades",
43 "token": "WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu"
44 }
45 }
46 ```
47
48channels:
49 /:
50 publish:
51 description: Send messages to the API
52 operationId: processReceivedMessage
53 message:
54 oneOf:
55 - $ref: '#/components/messages/ping'
56 - $ref: '#/components/messages/subscribe'
57 - $ref: '#/components/messages/unsubscribe'
58
59 subscribe:
60 description: Messages that you receive from the API
61 operationId: sendMessage
62 message:
63 oneOf:
64 - $ref: '#/components/messages/pong'
65 - $ref: '#/components/messages/heartbeat'
66 - $ref: '#/components/messages/systemStatus'
67 - $ref: '#/components/messages/subscriptionStatus'
68
69components:
70 messages:
71 ping:
72 summary: Ping server to determine whether connection is alive
73 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
74 payload:
75 $ref: '#/components/schemas/ping'
76 x-response:
77 $ref: '#/components/messages/pong'
78 heartbeat:
79 description: Server heartbeat sent if no subscription traffic within 1 second (approximately)
80 payload:
81 $ref: '#/components/schemas/heartbeat'
82 pong:
83 summary: Pong is a response to ping message
84 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
85 payload:
86 $ref: '#/components/schemas/pong'
87 systemStatus:
88 description: Status sent on connection or system status changes.
89 payload:
90 $ref: '#/components/schemas/systemStatus'
91 examples:
92 - payload:
93 connectionID: 8628615390848610000
94 event: systemStatus
95 status: online
96 version: 1.0.0
97 subscribe:
98 description: Subscribe to a topic on a single or multiple currency pairs.
99 payload:
100 $ref: '#/components/schemas/subscribe'
101 examples:
102 - payload:
103 event: subscribe
104 pair:
105 - XBT/USD
106 - XBT/EUR
107 subscription:
108 name: ticker
109 - payload:
110 event: subscribe
111 subscription:
112 name: ownTrades
113 token: WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu
114 x-response:
115 $ref: '#/components/messages/subscriptionStatus'
116 unsubscribe:
117 description: Unsubscribe, can specify a channelID or multiple currency pairs.
118 payload:
119 $ref: '#/components/schemas/subscribe'
120 examples:
121 - payload:
122 event: unsubscribe
123 pair:
124 - XBT/EUR
125 - XBT/USD
126 subscription:
127 name: ticker
128 - payload:
129 event: unsubscribe
130 subscription:
131 name: ownTrades
132 token: WW91ciBhdXRoZW50aWNhdGlvbiB0b2tlbiBnb2VzIGhlcmUu
133 x-response:
134 $ref: '#/components/messages/subscriptionStatus'
135 subscriptionStatus:
136 description: Subscription status response to subscribe, unsubscribe or exchange initiated unsubscribe.
137 payload:
138 $ref: '#/components/schemas/subscriptionStatus'
139 examples:
140 - payload:
141 channelID: 10001
142 channelName: ohlc-5
143 event: subscriptionStatus
144 pair: XBT/EUR
145 reqid: 42
146 status: unsubscribed
147 subscription:
148 interval: 5
149 name: ohlc
150 - payload:
151 errorMessage: Subscription depth not supported
152 event: subscriptionStatus
153 pair: XBT/USD
154 status: error
155 subscription:
156 depth: 42
157 name: book
158
159 schemas:
160 ping:
161 type: object
162 properties:
163 event:
164 type: string
165 const: ping
166 reqid:
167 $ref: '#/components/schemas/reqid'
168 required:
169 - event
170 heartbeat:
171 type: object
172 properties:
173 event:
174 type: string
175 const: heartbeat
176 pong:
177 type: object
178 properties:
179 event:
180 type: string
181 const: pong
182 reqid:
183 $ref: '#/components/schemas/reqid'
184 systemStatus:
185 type: object
186 properties:
187 event:
188 type: string
189 const: systemStatus
190 connectionID:
191 type: integer
192 description: The ID of the connection
193 status:
194 $ref: '#/components/schemas/status'
195 version:
196 type: string
197 status:
198 type: string
199 enum:
200 - online
201 - maintenance
202 - cancel_only
203 - limit_only
204 - post_only
205 subscribe:
206 type: object
207 properties:
208 event:
209 type: string
210 const: subscribe
211 reqid:
212 $ref: '#/components/schemas/reqid'
213 pair:
214 $ref: '#/components/schemas/pair'
215 subscription:
216 type: object
217 properties:
218 depth:
219 $ref: '#/components/schemas/depth'
220 interval:
221 $ref: '#/components/schemas/interval'
222 name:
223 $ref: '#/components/schemas/name'
224 ratecounter:
225 $ref: '#/components/schemas/ratecounter'
226 snapshot:
227 $ref: '#/components/schemas/snapshot'
228 token:
229 $ref: '#/components/schemas/token'
230 required:
231 - name
232 required:
233 - event
234 unsubscribe:
235 type: object
236 properties:
237 event:
238 type: string
239 const: unsubscribe
240 reqid:
241 $ref: '#/components/schemas/reqid'
242 pair:
243 $ref: '#/components/schemas/pair'
244 subscription:
245 type: object
246 properties:
247 depth:
248 $ref: '#/components/schemas/depth'
249 interval:
250 $ref: '#/components/schemas/interval'
251 name:
252 $ref: '#/components/schemas/name'
253 token:
254 $ref: '#/components/schemas/token'
255 required:
256 - name
257 required:
258 - event
259 subscriptionStatus:
260 type: object
261 oneOf:
262 - $ref: '#/components/schemas/subscriptionStatusError'
263 - $ref: '#/components/schemas/subscriptionStatusSuccess'
264 subscriptionStatusError:
265 allOf:
266 - properties:
267 errorMessage:
268 type: string
269 required:
270 - errorMessage
271 - $ref: '#/components/schemas/subscriptionStatusCommon'
272 subscriptionStatusSuccess:
273 allOf:
274 - properties:
275 channelID:
276 type: integer
277 description: ChannelID on successful subscription, applicable to public messages only.
278 channelName:
279 type: string
280 description: Channel Name on successful subscription. For payloads 'ohlc' and 'book', respective interval or depth will be added as suffix.
281 required:
282 - channelID
283 - channelName
284 - $ref: '#/components/schemas/subscriptionStatusCommon'
285 subscriptionStatusCommon:
286 type: object
287 required:
288 - event
289 properties:
290 event:
291 type: string
292 const: subscriptionStatus
293 reqid:
294 $ref: '#/components/schemas/reqid'
295 pair:
296 $ref: '#/components/schemas/pair'
297 status:
298 $ref: '#/components/schemas/status'
299 subscription:
300 required:
301 - name
302 type: object
303 properties:
304 depth:
305 $ref: '#/components/schemas/depth'
306 interval:
307 $ref: '#/components/schemas/interval'
308 maxratecount:
309 $ref: '#/components/schemas/maxratecount'
310 name:
311 $ref: '#/components/schemas/name'
312 token:
313 $ref: '#/components/schemas/token'
314 interval:
315 type: integer
316 description: Time interval associated with ohlc subscription in minutes.
317 default: 1
318 enum:
319 - 1
320 - 5
321 - 15
322 - 30
323 - 60
324 - 240
325 - 1440
326 - 10080
327 - 21600
328 name:
329 type: string
330 description: The name of the channel you subscribe too.
331 enum:
332 - book
333 - ohlc
334 - openOrders
335 - ownTrades
336 - spread
337 - ticker
338 - trade
339 token:
340 type: string
341 description: base64-encoded authentication token for private-data endpoints.
342 depth:
343 type: integer
344 default: 10
345 enum:
346 - 10
347 - 25
348 - 100
349 - 500
350 - 1000
351 description: Depth associated with book subscription in number of levels each side.
352 maxratecount:
353 type: integer
354 description: Max rate-limit budget. Compare to the ratecounter field in the openOrders updates to check whether you are approaching the rate limit.
355 ratecounter:
356 type: boolean
357 default: false
358 description: Whether to send rate-limit counter in updates (supported only for openOrders subscriptions)
359 snapshot:
360 type: boolean
361 default: true
362 description: Whether to send historical feed data snapshot upon subscription (supported only for ownTrades subscriptions)
363 reqid:
364 type: integer
365 description: client originated ID reflected in response message.
366 pair:
367 type: array
368 description: Array of currency pairs.
369 items:
370 type: string
371 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.
372 pattern: '[A-Z\s]+\/[A-Z\s]+'
Stay tuned for more articles around WebSocket and AsyncAPI. Share your feedback and connect with the AsyncAPI community in our Slack workspace.