You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/BYBIT_SDK_QUICKSTART_GUIDE.md
+235-4Lines changed: 235 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,221 @@
1
+
<!-- siebly:metadata
2
+
siebly:
3
+
version: 1
4
+
hero:
5
+
headline: Bybit API JavaScript Tutorial for Node.js and TypeScript
6
+
badges:
7
+
- Bybit API
8
+
- Spot
9
+
- Linear
10
+
- Inverse
11
+
- Options
12
+
- WebSocket API
13
+
summary: Build Bybit API integrations without writing your own request signing, category endpoint routing, WebSocket authentication, reconnect loops, resubscribe logic, or WebSocket API response matching.
14
+
codeFilename: first-bybit-api-call.ts
15
+
codeStatus: public REST API
16
+
startSectionId: start-building-first-calls
17
+
software:
18
+
description: Node.js and JavaScript SDK for the Bybit REST API, public and private WebSockets, demo trading, regional routing, and WebSocket API command workflows.
19
+
topics:
20
+
- Bybit REST API
21
+
- Spot REST API
22
+
- Linear contract REST API
23
+
- Inverse contract REST API
24
+
- Options REST API
25
+
- Public WebSockets
26
+
- Private WebSockets
27
+
- WebSocket API
28
+
- Demo trading
29
+
- Regional routing
30
+
machineCatalog:
31
+
label: Bybit API JavaScript Tutorial
32
+
topics:
33
+
- REST API
34
+
- category-based product routing
35
+
- demo trading
36
+
- testnet
37
+
- regional REST API routing
38
+
- public WebSockets
39
+
- private WebSockets
40
+
- WebSocket API commands
41
+
- production reconnect handling
42
+
sdkPagePromo:
43
+
descriptionBeforePackage: 'A practical JavaScript guide to using '
44
+
descriptionAfterPackage: ' across the Bybit REST API, category-based Spot and derivatives routing, public and private streams, demo trading, testnet, regional routing, WebSocket streams and WebSocket API commands.'
architectureApiSummary: REST API, market data streams, account streams, WebSocket API
55
+
surfaceMap:
56
+
heading: One Bybit SDK, complete API coverage
57
+
summary: The Bybit API includes REST API calls, public streams, private streams, and WebSocket API command paths. Use the SDK surface that matches each workflow.
58
+
appLabel: Bot, dashboard, worker, tool
59
+
appSummary: Any Node.js or JavaScript-compatible service that needs Bybit market data, account state, order management, or reconciliation.
60
+
apiLabel: Bybit API
61
+
apiItems:
62
+
- REST API calls across market, trade, position, account, and asset APIs
63
+
- Public streams by product category
64
+
- Private account streams
65
+
- WebSocket API commands
66
+
packageNodes:
67
+
- label: RestClientV5
68
+
summary: Current REST API client
69
+
- label: WebsocketClient
70
+
summary: Public and private streams
71
+
- label: WebsocketAPIClient
72
+
summary: Awaitable WebSocket API commands
73
+
- label: SpotClientV3
74
+
summary: Legacy compatibility only
75
+
routing:
76
+
id: category-router
77
+
eyebrow: API Categories
78
+
heading: The Bybit API often routes by category
79
+
summary: The same SDK method can cover more than one product family. Make the category value a first-class part of your request builder, logs, tests, and reconciliation keys.
80
+
rows:
81
+
- code: "category: 'spot'"
82
+
heading: Spot
83
+
summary: Spot market data and spot order workflows where the endpoint supports category-based routing.
84
+
- code: "category: 'linear'"
85
+
heading: Linear
86
+
summary: USDT and USDC linear contracts, including common perpetual and futures workflows.
87
+
- code: "category: 'inverse'"
88
+
heading: Inverse
89
+
summary: Inverse perpetual and inverse futures contracts with inverse symbols and account state.
90
+
- code: "category: 'option'"
91
+
heading: Options
92
+
summary: Options market data, orders, positions, greeks, and WebSocket topics where supported.
93
+
coverage:
94
+
heading: What to get right in a Bybit integration
95
+
summary: Start with a working public request, then build through credentials, category-based routing, private streams, demo and testnet behavior, WebSocket API commands, reconnect recovery, and production rollout checks.
96
+
cards:
97
+
- heading: REST API, categories, and products
98
+
summary: Use one current REST API client while keeping category, symbol, account mode, and product behavior explicit.
99
+
- heading: Auth and environments
100
+
summary: Separate live, testnet, demo trading, HMAC, RSA, and regional routing decisions before trading.
101
+
- heading: Public and private streams
102
+
summary: Subscribe to market and account topics with reconnect-aware state handling and REST API reconciliation.
103
+
- heading: WebSocket API commands
104
+
summary: Use promise-wrapped WebSocket API commands, then confirm state changes through streams or the REST API.
105
+
snippets:
106
+
heading: First REST API calls, streams, demo trading, and WebSocket API commands, with just a few lines of code.
107
+
summary: Run one focused example first, then add the surrounding account-state and recovery workflow once the client, credentials, and category are correct.
108
+
labels:
109
+
rest-api: REST API
110
+
public-websocket: Public stream
111
+
private-websocket: Private stream
112
+
demo-order: Demo order
113
+
ws-api: WebSocket API
114
+
workflows:
115
+
heading: REST API, WebSocket Stream & WebSocket API workflows
116
+
summary: A REST API response, a stream update, and a WebSocket API acknowledgement each tell you something different. The diagrams below show where routing, subscription state, reconnect recovery, and command acknowledgement fit.
117
+
diagrams:
118
+
- heading: REST API category routing
119
+
summary: Most current Bybit workflows start with RestClientV5 and a product category rather than a separate product client.
120
+
steps:
121
+
- label: Choose category
122
+
owner: app
123
+
- label: Build typed request
124
+
owner: app
125
+
- label: Call RestClientV5 method
126
+
owner: app
127
+
- label: Sign and route request
128
+
owner: sdk
129
+
- label: Return Bybit response
130
+
owner: exchange
131
+
- label: Normalize result
132
+
owner: app
133
+
- heading: Private stream recovery
134
+
summary: After a private connection drops, stream resubscription is only part of recovery. Rebuild account state before risky actions resume.
135
+
steps:
136
+
- label: on(reconnect)
137
+
owner: event
138
+
- label: Pause order logic
139
+
owner: app
140
+
- label: Reconnect and authenticate
141
+
owner: sdk
142
+
- label: Resubscribe cached topics
143
+
owner: sdk
144
+
- label: on(reconnected)
145
+
owner: event
146
+
- label: Backfill wallet, positions, orders
147
+
owner: app
148
+
- label: Resume from known state
149
+
owner: app
150
+
- heading: WebSocket API command flow
151
+
summary: The WebSocket API acknowledgement is not a fill. Treat it as command acceptance, then watch order and execution state.
152
+
steps:
153
+
- label: connectWSAPI()
154
+
owner: app
155
+
- label: Authenticate v5PrivateTrade
156
+
owner: sdk
157
+
- label: await submitNewOrder()
158
+
owner: app
159
+
- label: Send signed order.create
160
+
owner: sdk
161
+
- label: Receive acknowledgement
162
+
owner: event
163
+
- label: Track order/execution stream
164
+
owner: app
165
+
- label: Reconcile with REST API when needed
166
+
owner: app
167
+
production:
168
+
heading: Before a Bybit integration trades unattended
169
+
summary: "The important work starts after the first request succeeds: credentials, account mode, category routing, reconnect behavior, final order state, and exchange-region availability all need to be predictable and observable."
170
+
items:
171
+
- Keep live, testnet, and demo trading credentials separate.
172
+
- Make category, symbol, account type, and position mode explicit in order code.
173
+
- Use private order and execution streams to confirm final order state.
174
+
- Backfill wallet, positions, open orders, and executions after private stream reconnects.
175
+
- "Prefer throwExceptions: true for RestClientV5 order workflows; if disabled, treat retCode === 0 as REST business acceptance."
176
+
- Include triggerDirection for triggered stop-loss orders and normalize hydrated defaults before deciding keep, amend, cancel_place, cancel, or place.
177
+
- Check regional API routing and exchange-side availability before production rollout.
178
+
journeys:
179
+
eyebrow: Choose your path
180
+
heading: Jump to the Bybit workflow you are building
181
+
actionLabel: Open section
182
+
cards:
183
+
- heading: Route by category
184
+
summary: Keep spot, linear, inverse, and option category values explicit in shared workflow code.
185
+
href: "#products-and-clients"
186
+
- heading: Stream live data
187
+
summary: Use subscribeV5 with the right category for public topics such as order books, trades, tickers, and klines.
188
+
href: "#websocket-streams"
189
+
- heading: Track private state
190
+
summary: Subscribe to order, execution, position, and wallet topics, then reconcile with the REST API after reconnects.
191
+
href: "#websocket-streams"
192
+
- heading: Send commands
193
+
summary: Use the REST API for broad coverage and demo trading, or WebsocketAPIClient for awaitable WebSocket API commands.
194
+
href: "#websocket-api"
195
+
article:
196
+
heading: Build around Bybit API categories and account state
197
+
summary: "This tutorial focuses on the Bybit API pieces developers usually need first: REST API calls, category-specific public streams, private account topics, WebSocket API commands, demo trading, testnet, regional routing, reconnects, and rollout checks."
198
+
related:
199
+
cards:
200
+
- heading: Bybit SDK page
201
+
summary: Return to install snippets, direct examples, endpoint maps, and package links.
202
+
href: /sdk/bybit/javascript
203
+
- heading: WebSocket API example
204
+
summary: Open the runnable Bybit WebSocket API client example referenced in the tutorial.
summary: Use the Bybit guide for orderLinkId context lookup, retCode gates, triggerDirection stops, private streams, and demo execution boundaries.
208
+
href: /ai/exchange-state/bybit
209
+
- heading: Source repository
210
+
summary: Browse SDK source, releases, issues, and endpoint coverage from GitHub.
211
+
href: https://github.com/tiagosiebler/bybit-api
212
+
-->
1
213
# Bybit API JavaScript Tutorial for Node.js and TypeScript
2
214
215
+
<!-- siebly:website-omit:start -->
3
216
> [!TIP]
4
217
> This guide can be read in tutorial format on the Siebly Website: [Bybit JavaScript REST API & WebSocket Tutorial](https://siebly.io/sdk/bybit/javascript/tutorial)
218
+
<!-- siebly:website-omit:end -->
5
219
6
220
This tutorial walks through a practical Bybit REST API, WebSocket stream, and WebSocket API integration using [`bybit-api`](https://www.npmjs.com/package/bybit-api), the Bybit JavaScript and TypeScript SDK by Siebly.io.
7
221
@@ -12,14 +226,15 @@ The SDK handles the repetitive parts: HMAC and RSA request signing, Bybit API en
12
226
- Bybit JavaScript SDK by Siebly: [`bybit-api`](https://www.npmjs.com/package/bybit-api)
- Bybit API docs: [Bybit API Documentation](https://bybit-exchange.github.io/docs/v5/intro)
18
232
- Position Management with Bybit APIs & WebSockets: [Siebly Position Management with Bybit APIs & WebSockets](https://siebly.io/ai/exchange-state/bybit)
19
233
- More SDKs: [Siebly.io](https://siebly.io)
20
234
21
235
---
22
236
237
+
<!-- siebly:section id="why-use-the-sdk" -->
23
238
## Why use the SDK
24
239
25
240
The Bybit API is unified, but a real integration still has several moving parts:
@@ -45,6 +260,7 @@ The method names stay close to Bybit's endpoint names, while the SDK handles bas
45
260
46
261
---
47
262
263
+
<!-- siebly:section id="install-and-api-keys" -->
48
264
## Install and API keys
49
265
50
266
If you do not have Node.js installed yet, install it first. The SDK is published to both [GitHub](https://github.com/tiagosiebler/bybit-api) and [npm](https://www.npmjs.com/package/bybit-api).
@@ -113,6 +329,7 @@ For RSA setup details, see [examples/Auth/RSA-sign.md](../examples/Auth/RSA-sign
113
329
114
330
---
115
331
332
+
<!-- siebly:section id="products-and-clients" -->
116
333
## Products and clients
117
334
118
335
For new Bybit integrations, start with the current API. Older Bybit SDK surfaces were split into many product-specific clients; this SDK now centers the current API around one REST API client plus WebSocket clients.
@@ -142,6 +359,7 @@ As a rule of thumb:
142
359
143
360
For a complete method map, see [docs/endpointFunctionList.md](./endpointFunctionList.md).
This submits to Bybit demo trading because `demoTrading: true` is set. Do not remove that option or switch to live keys until you are ready to place real orders.
309
532
310
-
For order workflows, prefer `throwExceptions: true` so non-zero Bybit API responses throw and can be handled in one structured catch path. If you intentionally set `throwExceptions: false`, a resolved REST promise can still be an exchange business rejection. Treat `retCode === 0` as acceptance and any non-zero `retCode` as a rejected or unknown submission state.
533
+
For order workflows, prefer `throwExceptions: true` so non-zero Bybit business responses throw and can be handled in one structured catch path. If you intentionally set `throwExceptions: false`, a resolved REST promise can still be an exchange business rejection. Treat `retCode === 0` as acceptance and any non-zero `retCode` as a rejected or unknown submission state.
311
534
312
535
See also: [Demo trading example](../examples/Rest/demo-trading.ts)
313
536
314
537
### 5. First WebSocket API order command
315
538
316
539
The WebSocket API lets you submit order commands over a persistent WebSocket connection and await responses. Bybit supports WebSocket API order commands in live and testnet environments, but not demo trading.
317
540
541
+
<!-- siebly:snippet id="ws-api" -->
318
542
```typescript
319
543
import { WebsocketAPIClient } from'bybit-api';
320
544
@@ -364,6 +588,7 @@ See also: [WebSocket API client example](../examples/Websocket/WS-API/ws-api-cli
364
588
365
589
---
366
590
591
+
<!-- siebly:section id="rest-api" -->
367
592
## REST API
368
593
369
594
Most Bybit integrations start with `RestClientV5`. It covers the current REST API surface and uses Bybit's `category` parameter to distinguish product groups where the endpoint requires it.
@@ -693,6 +918,7 @@ If an endpoint exists in Bybit's API docs, search for the endpoint path or metho
693
918
694
919
---
695
920
921
+
<!-- siebly:section id="websocket-streams" -->
696
922
## WebSocket Streams
697
923
698
924
Use `WebsocketClient` when you want event-driven updates instead of REST API polling. The same client handles public streams, private account streams, and raw WebSocket API commands.
@@ -814,6 +1040,7 @@ Do not subscribe to the same topic in multiple clients unless you intentionally
814
1040
815
1041
---
816
1042
1043
+
<!-- siebly:section id="websocket-api" -->
817
1044
## WebSocket API
818
1045
819
1046
Bybit's WebSocket API is a request/response API over a persistent WebSocket connection. In this SDK, you can use it in two ways:
0 commit comments