Skip to content

Commit f1a1cfb

Browse files
author
tiagosiebler
committed
chore: update tutorial to latest
1 parent d9dbd75 commit f1a1cfb

1 file changed

Lines changed: 235 additions & 4 deletions

File tree

docs/BYBIT_SDK_QUICKSTART_GUIDE.md

Lines changed: 235 additions & 4 deletions
Original file line numberDiff line numberDiff 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.'
45+
highlights:
46+
- Complete REST API coverage
47+
- Public and private WebSocket streams
48+
- Demo trading, testnet, and regional routing
49+
- WebSocket API command flow
50+
exampleHref: /examples/Bybit/Websocket/WS-API/ws-api-client
51+
exampleLabel: WebSocket API example
52+
architectureClientSummary: RestClientV5, WebsocketClient, WebsocketAPIClient
53+
architectureApiTitle: Bybit API
54+
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.
205+
href: /examples/Bybit/Websocket/WS-API/ws-api-client
206+
- heading: Position manager guide
207+
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+
-->
1213
# Bybit API JavaScript Tutorial for Node.js and TypeScript
2214

215+
<!-- siebly:website-omit:start -->
3216
> [!TIP]
4217
> 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 -->
5219
6220
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.
7221

@@ -12,14 +226,15 @@ The SDK handles the repetitive parts: HMAC and RSA request signing, Bybit API en
12226
- Bybit JavaScript SDK by Siebly: [`bybit-api`](https://www.npmjs.com/package/bybit-api)
13227
- GitHub Repository: [`tiagosiebler/bybit-api`](https://github.com/tiagosiebler/bybit-api)
14228
- SDK function-endpoint map: [Bybit JavaScript Endpoint Reference](./endpointFunctionList.md)
15-
- REST API examples: [Bybit SDK REST API examples](../examples/Rest)
16-
- WebSocket examples: [Bybit SDK WebSocket examples](../examples/Websocket)
229+
- REST API examples: [Bybit SDK REST API examples](../examples/Rest/rest-v5-public.ts)
230+
- WebSocket examples: [Bybit SDK WebSocket examples](../examples/Websocket/Public/ws-public-v5.ts)
17231
- Bybit API docs: [Bybit API Documentation](https://bybit-exchange.github.io/docs/v5/intro)
18232
- Position Management with Bybit APIs & WebSockets: [Siebly Position Management with Bybit APIs & WebSockets](https://siebly.io/ai/exchange-state/bybit)
19233
- More SDKs: [Siebly.io](https://siebly.io)
20234

21235
---
22236

237+
<!-- siebly:section id="why-use-the-sdk" -->
23238
## Why use the SDK
24239

25240
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
45260

46261
---
47262

263+
<!-- siebly:section id="install-and-api-keys" -->
48264
## Install and API keys
49265

50266
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
113329

114330
---
115331

332+
<!-- siebly:section id="products-and-clients" -->
116333
## Products and clients
117334

118335
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:
142359

143360
For a complete method map, see [docs/endpointFunctionList.md](./endpointFunctionList.md).
144361

362+
<!-- siebly:section id="rest-api-streams-and-websocket-api" -->
145363
### REST API, streams, and WebSocket API
146364

147365
Bybit exposes several different integration flows. Keep them separate in your architecture:
@@ -157,12 +375,14 @@ Use the REST API when you want maximum endpoint coverage or a simple one-off req
157375

158376
---
159377

378+
<!-- siebly:section id="start-building-first-calls" -->
160379
## Start building: first calls
161380

162381
If you only want the fastest path to a working integration, start here.
163382

164383
### 1. First public REST API request
165384

385+
<!-- siebly:snippet id="rest-api" -->
166386
```typescript
167387
import { RestClientV5 } from 'bybit-api';
168388

@@ -208,6 +428,7 @@ See also: [public REST API example](../examples/Rest/rest-v5-public.ts)
208428

209429
### 2. First public WebSocket stream
210430

431+
<!-- siebly:snippet id="public-websocket" -->
211432
```typescript
212433
import { WebsocketClient, isWsOrderbookEventV5 } from 'bybit-api';
213434

@@ -236,6 +457,7 @@ See also: [public WebSocket example](../examples/Websocket/Public/ws-public-v5.t
236457

237458
### 3. First private account WebSocket stream
238459

460+
<!-- siebly:snippet id="private-websocket" -->
239461
```typescript
240462
import { WebsocketClient } from 'bybit-api';
241463

@@ -274,6 +496,7 @@ See also: [private WebSocket example](../examples/Websocket/Private/ws-private-v
274496

275497
Use demo trading before placing live orders. Demo trading uses a separate Bybit demo account and separate API keys.
276498

499+
<!-- siebly:snippet id="demo-order" -->
277500
```typescript
278501
import { RestClientV5 } from 'bybit-api';
279502

@@ -307,14 +530,15 @@ placeDemoOrder().catch(console.error);
307530

308531
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.
309532

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.
311534

312535
See also: [Demo trading example](../examples/Rest/demo-trading.ts)
313536

314537
### 5. First WebSocket API order command
315538

316539
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.
317540

541+
<!-- siebly:snippet id="ws-api" -->
318542
```typescript
319543
import { WebsocketAPIClient } from 'bybit-api';
320544

@@ -364,6 +588,7 @@ See also: [WebSocket API client example](../examples/Websocket/WS-API/ws-api-cli
364588

365589
---
366590

591+
<!-- siebly:section id="rest-api" -->
367592
## REST API
368593

369594
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
693918

694919
---
695920

921+
<!-- siebly:section id="websocket-streams" -->
696922
## WebSocket Streams
697923

698924
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
8141040

8151041
---
8161042

1043+
<!-- siebly:section id="websocket-api" -->
8171044
## WebSocket API
8181045

8191046
Bybit's WebSocket API is a request/response API over a persistent WebSocket connection. In this SDK, you can use it in two ways:
@@ -917,6 +1144,7 @@ See also:
9171144

9181145
---
9191146

1147+
<!-- siebly:section id="environments-and-regions" -->
9201148
## Environments and regions
9211149

9221150
### Live
@@ -1008,6 +1236,7 @@ See also: [custom REST API URL example](../examples/Rest/rest-v5-custom-url.ts)
10081236

10091237
---
10101238

1239+
<!-- siebly:section id="production-notes" -->
10111240
## Production notes
10121241

10131242
Before a Bybit integration trades unattended, make these decisions explicit.
@@ -1205,6 +1434,7 @@ For raw HTTP request/response tracing during local debugging, the repo also supp
12051434

12061435
---
12071436

1437+
<!-- siebly:section id="faq" -->
12081438
## FAQ
12091439

12101440
**Do I need API keys for public market data?**
@@ -1246,10 +1476,11 @@ No. This guide covers the common first steps and production concerns. For full m
12461476

12471477
- [Bybit JavaScript endpoint reference](./endpointFunctionList.md)
12481478
- [Bybit SDK examples](../examples)
1249-
- [TSDoc documentation](https://tsdocs.dev/docs/bybit-api)
1479+
- [Bybit SDK source on GitHub](https://github.com/tiagosiebler/bybit-api)
12501480

12511481
---
12521482

1483+
<!-- siebly:section id="next-steps" -->
12531484
## Next steps
12541485

12551486
If you want to learn more about integrating with the Bybit API and WebSockets:

0 commit comments

Comments
 (0)