kucoin-api 2.1.16 → 2.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -8
- package/llms.txt +446 -391
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -20,18 +20,53 @@
|
|
|
20
20
|
|
|
21
21
|
Updated & performant JavaScript & Node.js SDK for the Kucoin REST APIs and WebSockets:
|
|
22
22
|
|
|
23
|
+
- Professional, robust & performant Kucoin SDK with extensive production use in live trading environments.
|
|
23
24
|
- Complete integration with all Kucoin REST APIs and WebSockets.
|
|
24
|
-
-
|
|
25
|
+
- Dedicated REST clients for Spot, Futures, and Broker operations
|
|
26
|
+
- Unified WebSocket client for all markets
|
|
27
|
+
- Complete TypeScript support (with type declarations for most API requests & responses).
|
|
28
|
+
- Strongly typed requests and responses.
|
|
29
|
+
- Automated end-to-end tests ensuring reliability.
|
|
30
|
+
- Actively maintained with a modern, promise-driven interface.
|
|
25
31
|
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.
|
|
32
|
+
- Event driven messaging.
|
|
33
|
+
- Smart WebSocket persistence with automatic reconnection handling.
|
|
34
|
+
- Emit `reconnected` event when dropped connection is restored.
|
|
35
|
+
- Support for both public and private WebSocket streams.
|
|
26
36
|
- Browser-friendly HMAC signature mechanism.
|
|
27
37
|
- Automatically supports both ESM and CJS projects.
|
|
38
|
+
- Heavy automated end-to-end testing with real API calls.
|
|
28
39
|
- Proxy support via axios integration.
|
|
29
40
|
- Active community support & collaboration in telegram: [Node.js Algo Traders](https://t.me/nodetraders).
|
|
30
41
|
|
|
42
|
+
## Table of Contents
|
|
43
|
+
|
|
44
|
+
- [Installation](#installation)
|
|
45
|
+
- [Examples](#examples)
|
|
46
|
+
- [Issues & Discussion](#issues--discussion)
|
|
47
|
+
- [Related Projects](#related-projects)
|
|
48
|
+
- [Documentation](#documentation)
|
|
49
|
+
- [Structure](#structure)
|
|
50
|
+
- [Usage](#usage)
|
|
51
|
+
- [REST API Clients](#rest-api)
|
|
52
|
+
- [Spot & Margin Trading](#spot--margin-trading)
|
|
53
|
+
- [Futures Trading](#futures-trading)
|
|
54
|
+
- [Broker Operations](#broker-operations)
|
|
55
|
+
- [WebSocket Client](#websockets)
|
|
56
|
+
- [Public WebSocket Streams](#public-websocket-streams)
|
|
57
|
+
- [Private WebSocket Streams](#private-websocket-streams)
|
|
58
|
+
- [Customise Logging](#customise-logging)
|
|
59
|
+
- [LLMs & AI](#use-with-llms--ai)
|
|
60
|
+
- [Contributions & Thanks](#contributions--thanks)
|
|
61
|
+
|
|
31
62
|
## Installation
|
|
32
63
|
|
|
33
64
|
`npm install --save kucoin-api`
|
|
34
65
|
|
|
66
|
+
## Examples
|
|
67
|
+
|
|
68
|
+
Refer to the [examples](./examples) folder for implementation demos.
|
|
69
|
+
|
|
35
70
|
## Issues & Discussion
|
|
36
71
|
|
|
37
72
|
- Issues? Check the [issues tab](https://github.com/tiagosiebler/kucoin-api/issues).
|
|
@@ -62,16 +97,19 @@ Check out my related JavaScript/TypeScript/Node.js projects:
|
|
|
62
97
|
|
|
63
98
|
## Documentation
|
|
64
99
|
|
|
65
|
-
Most methods accept JS objects. These can be populated using parameters specified by Kucoin's API documentation.
|
|
100
|
+
Most methods accept JS objects. These can be populated using parameters specified by Kucoin's API documentation, or check the type definition in each class within this repository.
|
|
101
|
+
|
|
102
|
+
### API Documentation Links
|
|
66
103
|
|
|
67
104
|
- [Kucoin API Documentation](https://www.kucoin.com/docs-new/introduction)
|
|
105
|
+
|
|
106
|
+
### SDK Documentation & Guides
|
|
107
|
+
|
|
68
108
|
- Node.js Quick Start Guides
|
|
69
109
|
- [Spot Node.js Kucoin Quick Start Guide](./examples/kucoin-SPOT-examples-nodejs.md)
|
|
70
110
|
- [Futures Node.js Kucoin Quick Start Guide](./examples/kucoin-FUTURES-examples-nodejs.md)
|
|
71
111
|
- [Futures Node.js Kucoin Order Placement Guide](./examples/rest-futures-orders-guide.ts)
|
|
72
|
-
|
|
73
112
|
- [REST Endpoint Function List](./docs/endpointFunctionList.md)
|
|
74
|
-
<!-- - [TSDoc Documentation (autogenerated using typedoc)](https://tsdocs.dev/docs/kucoin-api) -->
|
|
75
113
|
|
|
76
114
|
## Structure
|
|
77
115
|
|
|
@@ -84,13 +122,21 @@ This project uses typescript. Resources are stored in 2 key structures:
|
|
|
84
122
|
|
|
85
123
|
# Usage
|
|
86
124
|
|
|
87
|
-
Create API credentials
|
|
125
|
+
Create API credentials on Kucoin's website:
|
|
88
126
|
|
|
89
127
|
- [Kucoin API Key Management](https://www.kucoin.com/account/api)
|
|
90
128
|
|
|
91
|
-
|
|
129
|
+
## REST API
|
|
130
|
+
|
|
131
|
+
The SDK provides dedicated REST clients for different trading products:
|
|
132
|
+
|
|
133
|
+
- **SpotClient** - for spot trading and margin operations
|
|
134
|
+
- **FuturesClient** - for futures trading operations
|
|
135
|
+
- **BrokerClient** - for broker and sub-account management
|
|
92
136
|
|
|
93
|
-
|
|
137
|
+
### Spot & Margin Trading
|
|
138
|
+
|
|
139
|
+
To use Kucoin's Spot and Margin APIs, import (or require) the `SpotClient`:
|
|
94
140
|
|
|
95
141
|
```javascript
|
|
96
142
|
const { SpotClient, FuturesClient } = require('kucoin-api');
|
|
@@ -124,10 +170,33 @@ try {
|
|
|
124
170
|
}
|
|
125
171
|
```
|
|
126
172
|
|
|
127
|
-
See [SpotClient](./src/SpotClient.ts)
|
|
173
|
+
See [SpotClient](./src/SpotClient.ts) for further information, or the [examples](./examples/) for lots of usage examples.
|
|
174
|
+
|
|
175
|
+
### Futures Trading
|
|
176
|
+
|
|
177
|
+
Use the `FuturesClient` for futures trading operations. See [FuturesClient](./src/FuturesClient.ts) for complete API coverage.
|
|
178
|
+
|
|
179
|
+
### Broker Operations
|
|
180
|
+
|
|
181
|
+
Use the `BrokerClient` for broker and sub-account management operations. See [BrokerClient](./src/BrokerClient.ts) for complete API coverage.
|
|
128
182
|
|
|
129
183
|
## WebSockets
|
|
130
184
|
|
|
185
|
+
All WebSocket functionality is supported via the unified `WebsocketClient`. This client handles both spot and futures WebSocket streams with automatic connection management and reconnection.
|
|
186
|
+
|
|
187
|
+
Key WebSocket features:
|
|
188
|
+
|
|
189
|
+
- Event driven messaging
|
|
190
|
+
- Smart WebSocket persistence with automatic reconnection
|
|
191
|
+
- Heartbeat mechanisms to detect disconnections
|
|
192
|
+
- Automatic resubscription after reconnection
|
|
193
|
+
- Support for both public and private WebSocket streams
|
|
194
|
+
- Unified client for spot and futures markets
|
|
195
|
+
|
|
196
|
+
### Public WebSocket Streams
|
|
197
|
+
|
|
198
|
+
For public market data, API credentials are not required:
|
|
199
|
+
|
|
131
200
|
All available WebSockets can be used via a shared `WebsocketClient`. The WebSocket client will automatically open/track/manage connections as needed. Each unique connection (one per server URL) is tracked using a WsKey (each WsKey is a string - see [WS_KEY_MAP](src/lib/websocket/websocket-util.ts) for a list of supported values).
|
|
132
201
|
|
|
133
202
|
Any subscribe/unsubscribe events will need to include a WsKey, so the WebSocket client understands which connection the event should be routed to. See examples below or in the [examples](./examples/) folder on GitHub.
|
|
@@ -208,6 +277,10 @@ try {
|
|
|
208
277
|
}
|
|
209
278
|
```
|
|
210
279
|
|
|
280
|
+
### Private WebSocket Streams
|
|
281
|
+
|
|
282
|
+
For private account data streams, API credentials are required. The WebsocketClient will automatically handle authentication when you provide API credentials.
|
|
283
|
+
|
|
211
284
|
See [WebsocketClient](./src/WebsocketClient.ts) for further information and make sure to check the [examples](./examples/) folder for much more detail, especially [ws-spot-public.ts](./examples/ws-spot-public.ts), which explains a lot of detail.
|
|
212
285
|
|
|
213
286
|
---
|
package/llms.txt
CHANGED
|
@@ -117,7 +117,6 @@ jest.config.ts
|
|
|
117
117
|
package.json
|
|
118
118
|
postBuild.sh
|
|
119
119
|
README.md
|
|
120
|
-
repomix.sh
|
|
121
120
|
tea.yaml
|
|
122
121
|
tsconfig.cjs.json
|
|
123
122
|
tsconfig.esm.json
|
|
@@ -1889,6 +1888,26 @@ export interface GetAnnouncementsRequest {
|
|
|
1889
1888
|
endTime?: number;
|
|
1890
1889
|
}
|
|
1891
1890
|
|
|
1891
|
+
================
|
|
1892
|
+
File: src/types/response/shared.types.ts
|
|
1893
|
+
================
|
|
1894
|
+
export interface APISuccessResponse<TData> {
|
|
1895
|
+
code: '200000';
|
|
1896
|
+
data: TData;
|
|
1897
|
+
}
|
|
1898
|
+
⋮----
|
|
1899
|
+
export interface APIErrorResponse {
|
|
1900
|
+
msg: string;
|
|
1901
|
+
code: string;
|
|
1902
|
+
}
|
|
1903
|
+
⋮----
|
|
1904
|
+
export type APIResponse<TData> = APISuccessResponse<TData> | APIErrorResponse;
|
|
1905
|
+
⋮----
|
|
1906
|
+
export interface ServiceStatus {
|
|
1907
|
+
msg: string;
|
|
1908
|
+
status: 'cancelonly' | 'close' | 'open';
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1892
1911
|
================
|
|
1893
1912
|
File: src/types/response/ws.ts
|
|
1894
1913
|
================
|
|
@@ -1947,24 +1966,6 @@ export interface WsAPITopicResponseMap {
|
|
|
1947
1966
|
[k: string]: never;
|
|
1948
1967
|
}
|
|
1949
1968
|
|
|
1950
|
-
================
|
|
1951
|
-
File: .gitignore
|
|
1952
|
-
================
|
|
1953
|
-
examples/futures-private-test.ts
|
|
1954
|
-
examples/spot-private-test.ts
|
|
1955
|
-
node_modules
|
|
1956
|
-
|
|
1957
|
-
rest-spot-private-ts-test.ts
|
|
1958
|
-
|
|
1959
|
-
localtest.sh
|
|
1960
|
-
testfile.ts
|
|
1961
|
-
dist
|
|
1962
|
-
|
|
1963
|
-
coverage
|
|
1964
|
-
|
|
1965
|
-
privaterepotracker
|
|
1966
|
-
restClientRegex.ts
|
|
1967
|
-
|
|
1968
1969
|
================
|
|
1969
1970
|
File: .prettierrc
|
|
1970
1971
|
================
|
|
@@ -2185,31 +2186,6 @@ cat >dist/mjs/package.json <<!EOF
|
|
|
2185
2186
|
find src -name '*.d.ts' -exec cp {} dist/mjs \;
|
|
2186
2187
|
find src -name '*.d.ts' -exec cp {} dist/cjs \;
|
|
2187
2188
|
|
|
2188
|
-
================
|
|
2189
|
-
File: repomix.sh
|
|
2190
|
-
================
|
|
2191
|
-
# Pack current directory
|
|
2192
|
-
#repomix
|
|
2193
|
-
|
|
2194
|
-
# Pack specific directory
|
|
2195
|
-
#repomix path/to/directory
|
|
2196
|
-
|
|
2197
|
-
# Pack remote repository (like gitingest!)
|
|
2198
|
-
# repomix --remote https://github.com/user/repo
|
|
2199
|
-
#repomix --remote user/repo # shorthand
|
|
2200
|
-
|
|
2201
|
-
# Pack with compression (reduces tokens by ~70%)
|
|
2202
|
-
# repomix --compress
|
|
2203
|
-
|
|
2204
|
-
# Different output formats
|
|
2205
|
-
# repomix --style markdown
|
|
2206
|
-
# repomix --style xml
|
|
2207
|
-
# repomix --style plain
|
|
2208
|
-
|
|
2209
|
-
# Include/exclude patterns
|
|
2210
|
-
# repomix --include "src/**/*.ts,**/*.md"
|
|
2211
|
-
npx repomix@latest --ignore ".github/,examples/apidoc/,docs/images/,docs/endpointFunctionList.md,test/,src/util/" --style plain --output llms.txt --compress
|
|
2212
|
-
|
|
2213
2189
|
================
|
|
2214
2190
|
File: tea.yaml
|
|
2215
2191
|
================
|
|
@@ -2245,42 +2221,6 @@ File: tsconfig.esm.json
|
|
|
2245
2221
|
"include": ["src/**/*.*"]
|
|
2246
2222
|
}
|
|
2247
2223
|
|
|
2248
|
-
================
|
|
2249
|
-
File: tsconfig.json
|
|
2250
|
-
================
|
|
2251
|
-
{
|
|
2252
|
-
"compilerOptions": {
|
|
2253
|
-
"allowSyntheticDefaultImports": true,
|
|
2254
|
-
"baseUrl": "src",
|
|
2255
|
-
"noEmitOnError": true,
|
|
2256
|
-
"declaration": true,
|
|
2257
|
-
"esModuleInterop": true,
|
|
2258
|
-
"forceConsistentCasingInFileNames": false,
|
|
2259
|
-
"inlineSourceMap": false,
|
|
2260
|
-
"lib": ["esnext"],
|
|
2261
|
-
"listEmittedFiles": false,
|
|
2262
|
-
"listFiles": false,
|
|
2263
|
-
"moduleResolution": "node",
|
|
2264
|
-
"noFallthroughCasesInSwitch": true,
|
|
2265
|
-
"noImplicitAny": true,
|
|
2266
|
-
"noUnusedParameters": true,
|
|
2267
|
-
"pretty": true,
|
|
2268
|
-
"removeComments": false,
|
|
2269
|
-
"resolveJsonModule": true,
|
|
2270
|
-
"skipLibCheck": false,
|
|
2271
|
-
"sourceMap": true,
|
|
2272
|
-
"strict": true,
|
|
2273
|
-
"strictNullChecks": true,
|
|
2274
|
-
"types": ["node", "jest"],
|
|
2275
|
-
"module": "commonjs",
|
|
2276
|
-
"outDir": "dist/cjs",
|
|
2277
|
-
"target": "esnext"
|
|
2278
|
-
},
|
|
2279
|
-
"compileOnSave": true,
|
|
2280
|
-
"exclude": ["node_modules", "dist"],
|
|
2281
|
-
"include": ["src/**/*.*", "test/**/*.*", ".eslintrc.cjs"]
|
|
2282
|
-
}
|
|
2283
|
-
|
|
2284
2224
|
================
|
|
2285
2225
|
File: tsconfig.linting.json
|
|
2286
2226
|
================
|
|
@@ -2946,26 +2886,6 @@ export interface BrokerWithdrawalRecord {
|
|
|
2946
2886
|
updatedAt: number;
|
|
2947
2887
|
}
|
|
2948
2888
|
|
|
2949
|
-
================
|
|
2950
|
-
File: src/types/response/shared.types.ts
|
|
2951
|
-
================
|
|
2952
|
-
export interface APISuccessResponse<TData> {
|
|
2953
|
-
code: '200000';
|
|
2954
|
-
data: TData;
|
|
2955
|
-
}
|
|
2956
|
-
⋮----
|
|
2957
|
-
export interface APIErrorResponse {
|
|
2958
|
-
msg: string;
|
|
2959
|
-
code: string;
|
|
2960
|
-
}
|
|
2961
|
-
⋮----
|
|
2962
|
-
export type APIResponse<TData> = APISuccessResponse<TData> | APIErrorResponse;
|
|
2963
|
-
⋮----
|
|
2964
|
-
export interface ServiceStatus {
|
|
2965
|
-
msg: string;
|
|
2966
|
-
status: 'cancelonly' | 'close' | 'open';
|
|
2967
|
-
}
|
|
2968
|
-
|
|
2969
2889
|
================
|
|
2970
2890
|
File: src/types/response/spot-affiliate.ts
|
|
2971
2891
|
================
|
|
@@ -3476,11 +3396,66 @@ export interface OtcLoanAccount {
|
|
|
3476
3396
|
isParent: boolean;
|
|
3477
3397
|
}
|
|
3478
3398
|
|
|
3399
|
+
================
|
|
3400
|
+
File: .gitignore
|
|
3401
|
+
================
|
|
3402
|
+
examples/futures-private-test.ts
|
|
3403
|
+
examples/spot-private-test.ts
|
|
3404
|
+
node_modules
|
|
3405
|
+
|
|
3406
|
+
rest-spot-private-ts-test.ts
|
|
3407
|
+
|
|
3408
|
+
localtest.sh
|
|
3409
|
+
testfile.ts
|
|
3410
|
+
dist
|
|
3411
|
+
repomix.sh
|
|
3412
|
+
|
|
3413
|
+
coverage
|
|
3414
|
+
|
|
3415
|
+
privaterepotracker
|
|
3416
|
+
restClientRegex.ts
|
|
3417
|
+
|
|
3479
3418
|
================
|
|
3480
3419
|
File: .nvmrc
|
|
3481
3420
|
================
|
|
3482
3421
|
v22.13.0
|
|
3483
3422
|
|
|
3423
|
+
================
|
|
3424
|
+
File: tsconfig.json
|
|
3425
|
+
================
|
|
3426
|
+
{
|
|
3427
|
+
"compilerOptions": {
|
|
3428
|
+
"allowSyntheticDefaultImports": true,
|
|
3429
|
+
"baseUrl": ".",
|
|
3430
|
+
"noEmitOnError": true,
|
|
3431
|
+
"declaration": true,
|
|
3432
|
+
"esModuleInterop": true,
|
|
3433
|
+
"forceConsistentCasingInFileNames": false,
|
|
3434
|
+
"inlineSourceMap": false,
|
|
3435
|
+
"lib": ["esnext"],
|
|
3436
|
+
"listEmittedFiles": false,
|
|
3437
|
+
"listFiles": false,
|
|
3438
|
+
"moduleResolution": "node",
|
|
3439
|
+
"noFallthroughCasesInSwitch": true,
|
|
3440
|
+
"noImplicitAny": true,
|
|
3441
|
+
"noUnusedParameters": true,
|
|
3442
|
+
"pretty": true,
|
|
3443
|
+
"removeComments": false,
|
|
3444
|
+
"resolveJsonModule": true,
|
|
3445
|
+
"skipLibCheck": false,
|
|
3446
|
+
"sourceMap": true,
|
|
3447
|
+
"strict": true,
|
|
3448
|
+
"strictNullChecks": true,
|
|
3449
|
+
"types": ["node", "jest"],
|
|
3450
|
+
"module": "commonjs",
|
|
3451
|
+
"outDir": "dist/cjs",
|
|
3452
|
+
"target": "esnext"
|
|
3453
|
+
},
|
|
3454
|
+
"compileOnSave": true,
|
|
3455
|
+
"exclude": ["node_modules", "dist"],
|
|
3456
|
+
"include": ["src/**/*.*", "test/**/*.*", ".eslintrc.cjs"]
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3484
3459
|
================
|
|
3485
3460
|
File: src/types/request/spot-account.ts
|
|
3486
3461
|
================
|
|
@@ -4017,295 +3992,7 @@ File: .eslintrc.cjs
|
|
|
4017
3992
|
// 'no-unused-vars': ['warn'],
|
|
4018
3993
|
|
|
4019
3994
|
================
|
|
4020
|
-
File:
|
|
4021
|
-
================
|
|
4022
|
-
# Node.js & JavaScript SDK for Kucoin REST APIs & Websockets
|
|
4023
|
-
|
|
4024
|
-
<p align="center">
|
|
4025
|
-
<a href="https://www.npmjs.com/package/kucoin-api">
|
|
4026
|
-
<picture>
|
|
4027
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/tiagosiebler/kucoin-api/blob/master/docs/images/logoDarkMode2.svg?raw=true#gh-dark-mode-only">
|
|
4028
|
-
<img alt="SDK Logo" src="https://github.com/tiagosiebler/kucoin-api/blob/master/docs/images/logoBrightMode2.svg?raw=true#gh-light-mode-only">
|
|
4029
|
-
</picture>
|
|
4030
|
-
</a>
|
|
4031
|
-
</p>
|
|
4032
|
-
|
|
4033
|
-
[][1]
|
|
4034
|
-
[][1]
|
|
4035
|
-
[][1]
|
|
4036
|
-
[](https://github.com/tiagosiebler/kucoin-api/actions/workflows/e2etest.yml)
|
|
4037
|
-
[][1]
|
|
4038
|
-
[](https://t.me/nodetraders)
|
|
4039
|
-
|
|
4040
|
-
[1]: https://www.npmjs.com/package/kucoin-api
|
|
4041
|
-
|
|
4042
|
-
Updated & performant JavaScript & Node.js SDK for the Kucoin REST APIs and WebSockets:
|
|
4043
|
-
|
|
4044
|
-
- Complete integration with all Kucoin REST APIs and WebSockets.
|
|
4045
|
-
- TypeScript support (with type declarations for most API requests & responses)
|
|
4046
|
-
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.
|
|
4047
|
-
- Browser-friendly HMAC signature mechanism.
|
|
4048
|
-
- Automatically supports both ESM and CJS projects.
|
|
4049
|
-
- Proxy support via axios integration.
|
|
4050
|
-
- Active community support & collaboration in telegram: [Node.js Algo Traders](https://t.me/nodetraders).
|
|
4051
|
-
|
|
4052
|
-
## Installation
|
|
4053
|
-
|
|
4054
|
-
`npm install --save kucoin-api`
|
|
4055
|
-
|
|
4056
|
-
## Issues & Discussion
|
|
4057
|
-
|
|
4058
|
-
- Issues? Check the [issues tab](https://github.com/tiagosiebler/kucoin-api/issues).
|
|
4059
|
-
- Discuss & collaborate with other node devs? Join our [Node.js Algo Traders](https://t.me/nodetraders) engineering community on telegram.
|
|
4060
|
-
- Follow our announcement channel for real-time updates on [X/Twitter](https://x.com/sieblyio)
|
|
4061
|
-
|
|
4062
|
-
<!-- template_related_projects -->
|
|
4063
|
-
|
|
4064
|
-
## Related projects
|
|
4065
|
-
|
|
4066
|
-
Check out my related JavaScript/TypeScript/Node.js projects:
|
|
4067
|
-
|
|
4068
|
-
- Try my REST API & WebSocket SDKs:
|
|
4069
|
-
- [Bybit-api Node.js SDK](https://www.npmjs.com/package/bybit-api)
|
|
4070
|
-
- [Okx-api Node.js SDK](https://www.npmjs.com/package/okx-api)
|
|
4071
|
-
- [Binance Node.js SDK](https://www.npmjs.com/package/binance)
|
|
4072
|
-
- [Gateio-api Node.js SDK](https://www.npmjs.com/package/gateio-api)
|
|
4073
|
-
- [Bitget-api Node.js SDK](https://www.npmjs.com/package/bitget-api)
|
|
4074
|
-
- [Kucoin-api Node.js SDK](https://www.npmjs.com/package/kucoin-api)
|
|
4075
|
-
- [Coinbase-api Node.js SDK](https://www.npmjs.com/package/coinbase-api)
|
|
4076
|
-
- [Bitmart-api Node.js SDK](https://www.npmjs.com/package/bitmart-api)
|
|
4077
|
-
- Try my misc utilities:
|
|
4078
|
-
- [OrderBooks Node.js](https://www.npmjs.com/package/orderbooks)
|
|
4079
|
-
- [Crypto Exchange Account State Cache](https://www.npmjs.com/package/accountstate)
|
|
4080
|
-
- Check out my examples:
|
|
4081
|
-
- [awesome-crypto-examples Node.js](https://github.com/tiagosiebler/awesome-crypto-examples)
|
|
4082
|
-
<!-- template_related_projects_end -->
|
|
4083
|
-
|
|
4084
|
-
## Documentation
|
|
4085
|
-
|
|
4086
|
-
Most methods accept JS objects. These can be populated using parameters specified by Kucoin's API documentation.
|
|
4087
|
-
|
|
4088
|
-
- [Kucoin API Documentation](https://www.kucoin.com/docs-new/introduction)
|
|
4089
|
-
- Node.js Quick Start Guides
|
|
4090
|
-
- [Spot Node.js Kucoin Quick Start Guide](./examples/kucoin-SPOT-examples-nodejs.md)
|
|
4091
|
-
- [Futures Node.js Kucoin Quick Start Guide](./examples/kucoin-FUTURES-examples-nodejs.md)
|
|
4092
|
-
- [Futures Node.js Kucoin Order Placement Guide](./examples/rest-futures-orders-guide.ts)
|
|
4093
|
-
|
|
4094
|
-
- [REST Endpoint Function List](./docs/endpointFunctionList.md)
|
|
4095
|
-
<!-- - [TSDoc Documentation (autogenerated using typedoc)](https://tsdocs.dev/docs/kucoin-api) -->
|
|
4096
|
-
|
|
4097
|
-
## Structure
|
|
4098
|
-
|
|
4099
|
-
This project uses typescript. Resources are stored in 2 key structures:
|
|
4100
|
-
|
|
4101
|
-
- [src](./src) - the whole connector written in typescript
|
|
4102
|
-
- [examples](./examples) - some implementation examples & demonstrations. Contributions are welcome!
|
|
4103
|
-
|
|
4104
|
-
---
|
|
4105
|
-
|
|
4106
|
-
# Usage
|
|
4107
|
-
|
|
4108
|
-
Create API credentials
|
|
4109
|
-
|
|
4110
|
-
- [Kucoin API Key Management](https://www.kucoin.com/account/api)
|
|
4111
|
-
|
|
4112
|
-
### REST API
|
|
4113
|
-
|
|
4114
|
-
To use any of Kucoin's REST APIs in JavaScript/TypeScript/Node.js, import (or require) the `SpotClient` (for spot and margin APIs) or `FuturesClient` (for futures APIs):
|
|
4115
|
-
|
|
4116
|
-
```javascript
|
|
4117
|
-
const { SpotClient, FuturesClient } = require('kucoin-api');
|
|
4118
|
-
|
|
4119
|
-
const client = new SpotClient({
|
|
4120
|
-
apiKey: 'apiKeyHere',
|
|
4121
|
-
apiSecret: 'apiSecretHere',
|
|
4122
|
-
apiPassphrase: 'apiPassPhraseHere',
|
|
4123
|
-
});
|
|
4124
|
-
|
|
4125
|
-
try {
|
|
4126
|
-
const spotBuyResult = await client.submitHFOrder({
|
|
4127
|
-
clientOid: client.generateNewOrderID(),
|
|
4128
|
-
side: 'buy',
|
|
4129
|
-
type: 'market',
|
|
4130
|
-
symbol: 'BTC-USDT',
|
|
4131
|
-
size: '0.00001',
|
|
4132
|
-
});
|
|
4133
|
-
console.log('spotBuy ', JSON.stringify(spotBuyResult, null, 2));
|
|
4134
|
-
|
|
4135
|
-
const spotSellResult = await client.submitHFOrder({
|
|
4136
|
-
clientOid: client.generateNewOrderID(),
|
|
4137
|
-
side: 'sell',
|
|
4138
|
-
type: 'market',
|
|
4139
|
-
symbol: 'BTC-USDT',
|
|
4140
|
-
size: '0.00001',
|
|
4141
|
-
});
|
|
4142
|
-
console.log('spotSellResult ', JSON.stringify(spotSellResult, null, 2));
|
|
4143
|
-
} catch (e) {
|
|
4144
|
-
console.error(`Req error: `, e);
|
|
4145
|
-
}
|
|
4146
|
-
```
|
|
4147
|
-
|
|
4148
|
-
See [SpotClient](./src/SpotClient.ts) and [FuturesClient](./src/FuturesClient.ts) for further information, or the [examples](./examples/) for lots of usage examples.
|
|
4149
|
-
|
|
4150
|
-
## WebSockets
|
|
4151
|
-
|
|
4152
|
-
All available WebSockets can be used via a shared `WebsocketClient`. The WebSocket client will automatically open/track/manage connections as needed. Each unique connection (one per server URL) is tracked using a WsKey (each WsKey is a string - see [WS_KEY_MAP](src/lib/websocket/websocket-util.ts) for a list of supported values).
|
|
4153
|
-
|
|
4154
|
-
Any subscribe/unsubscribe events will need to include a WsKey, so the WebSocket client understands which connection the event should be routed to. See examples below or in the [examples](./examples/) folder on GitHub.
|
|
4155
|
-
|
|
4156
|
-
Data events are emitted from the WebsocketClient via the `update` event, see example below:
|
|
4157
|
-
|
|
4158
|
-
```javascript
|
|
4159
|
-
const { WebsocketClient } = require('kucoin-api');
|
|
4160
|
-
|
|
4161
|
-
const client = new WebsocketClient();
|
|
4162
|
-
|
|
4163
|
-
client.on('open', (data) => {
|
|
4164
|
-
console.log('open: ', data?.wsKey);
|
|
4165
|
-
});
|
|
4166
|
-
|
|
4167
|
-
// Data received
|
|
4168
|
-
client.on('update', (data) => {
|
|
4169
|
-
console.info('data received: ', JSON.stringify(data));
|
|
4170
|
-
});
|
|
4171
|
-
|
|
4172
|
-
// Something happened, attempting to reconenct
|
|
4173
|
-
client.on('reconnect', (data) => {
|
|
4174
|
-
console.log('reconnect: ', data);
|
|
4175
|
-
});
|
|
4176
|
-
|
|
4177
|
-
// Reconnect successful
|
|
4178
|
-
client.on('reconnected', (data) => {
|
|
4179
|
-
console.log('reconnected: ', data);
|
|
4180
|
-
});
|
|
4181
|
-
|
|
4182
|
-
// Connection closed. If unexpected, expect reconnect -> reconnected.
|
|
4183
|
-
client.on('close', (data) => {
|
|
4184
|
-
console.error('close: ', data);
|
|
4185
|
-
});
|
|
4186
|
-
|
|
4187
|
-
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
|
|
4188
|
-
client.on('response', (data) => {
|
|
4189
|
-
console.info('response: ', data);
|
|
4190
|
-
// throw new Error('res?');
|
|
4191
|
-
});
|
|
4192
|
-
|
|
4193
|
-
client.on('exception', (data) => {
|
|
4194
|
-
console.error('exception: ', {
|
|
4195
|
-
msg: data.msg,
|
|
4196
|
-
errno: data.errno,
|
|
4197
|
-
code: data.code,
|
|
4198
|
-
syscall: data.syscall,
|
|
4199
|
-
hostname: data.hostname,
|
|
4200
|
-
});
|
|
4201
|
-
});
|
|
4202
|
-
|
|
4203
|
-
try {
|
|
4204
|
-
// Optional: await a connection to be ready before subscribing (this is not necessary)
|
|
4205
|
-
// await client.connect('futuresPublicV1');
|
|
4206
|
-
|
|
4207
|
-
/**
|
|
4208
|
-
* Examples for public futures websocket topics (that don't require authentication).
|
|
4209
|
-
*
|
|
4210
|
-
* These should all subscribe via the "futuresPublicV1" wsKey. For detailed usage, refer to the ws-spot-public.ts example.
|
|
4211
|
-
*/
|
|
4212
|
-
client.subscribe(
|
|
4213
|
-
[
|
|
4214
|
-
'/contractMarket/tickerV2:XBTUSDM',
|
|
4215
|
-
'/contractMarket/ticker:XBTUSDM',
|
|
4216
|
-
'/contractMarket/level2:XBTUSDM',
|
|
4217
|
-
'/contractMarket/execution:XBTUSDM',
|
|
4218
|
-
'/contractMarket/level2Depth5:XBTUSDM',
|
|
4219
|
-
'/contractMarket/level2Depth50:XBTUSDM',
|
|
4220
|
-
'/contractMarket/limitCandle:XBTUSDTM_1hour',
|
|
4221
|
-
'/contract/instrument:XBTUSDM',
|
|
4222
|
-
'/contract/announcement',
|
|
4223
|
-
'/contractMarket/snapshot:XBTUSDM',
|
|
4224
|
-
],
|
|
4225
|
-
'futuresPublicV1',
|
|
4226
|
-
);
|
|
4227
|
-
} catch (e) {
|
|
4228
|
-
console.error(`Subscribe exception: `, e);
|
|
4229
|
-
}
|
|
4230
|
-
```
|
|
4231
|
-
|
|
4232
|
-
See [WebsocketClient](./src/WebsocketClient.ts) for further information and make sure to check the [examples](./examples/) folder for much more detail, especially [ws-spot-public.ts](./examples/ws-spot-public.ts), which explains a lot of detail.
|
|
4233
|
-
|
|
4234
|
-
---
|
|
4235
|
-
|
|
4236
|
-
## Customise Logging
|
|
4237
|
-
|
|
4238
|
-
Pass a custom logger which supports the log methods `trace`, `info` and `error`, or override methods from the default logger as desired.
|
|
4239
|
-
|
|
4240
|
-
```javascript
|
|
4241
|
-
const { WebsocketClient, DefaultLogger } = require('kucoin-api');
|
|
4242
|
-
|
|
4243
|
-
// E.g. customise logging for only the trace level:
|
|
4244
|
-
const logger = {
|
|
4245
|
-
// Inherit existing logger methods, using an object spread
|
|
4246
|
-
...DefaultLogger,
|
|
4247
|
-
// Define a custom trace function to override only that function
|
|
4248
|
-
trace: (...params) => {
|
|
4249
|
-
if (
|
|
4250
|
-
[
|
|
4251
|
-
'Sending ping',
|
|
4252
|
-
// 'Sending upstream ws message: ',
|
|
4253
|
-
'Received pong',
|
|
4254
|
-
].includes(params[0])
|
|
4255
|
-
) {
|
|
4256
|
-
return;
|
|
4257
|
-
}
|
|
4258
|
-
console.log('trace', JSON.stringify(params, null, 2));
|
|
4259
|
-
},
|
|
4260
|
-
};
|
|
4261
|
-
|
|
4262
|
-
const ws = new WebsocketClient(
|
|
4263
|
-
{
|
|
4264
|
-
apiKey: 'apiKeyHere',
|
|
4265
|
-
apiSecret: 'apiSecretHere',
|
|
4266
|
-
apiPassphrase: 'apiPassPhraseHere',
|
|
4267
|
-
},
|
|
4268
|
-
logger,
|
|
4269
|
-
);
|
|
4270
|
-
```
|
|
4271
|
-
|
|
4272
|
-
---
|
|
4273
|
-
|
|
4274
|
-
<!-- template_contributions -->
|
|
4275
|
-
|
|
4276
|
-
### Contributions & Thanks
|
|
4277
|
-
|
|
4278
|
-
Have my projects helped you? Share the love, there are many ways you can show your thanks:
|
|
4279
|
-
|
|
4280
|
-
- Star & share my projects.
|
|
4281
|
-
- Are my projects useful? Sponsor me on Github and support my effort to maintain & improve them: https://github.com/sponsors/tiagosiebler
|
|
4282
|
-
- Have an interesting project? Get in touch & invite me to it.
|
|
4283
|
-
- Or buy me all the coffee:
|
|
4284
|
-
- ETH(ERC20): `0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C` <!-- metamask -->
|
|
4285
|
-
|
|
4286
|
-
<!---
|
|
4287
|
-
old ones:
|
|
4288
|
-
- BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk`
|
|
4289
|
-
- BTC(SegWit): `bc1ql64wr9z3khp2gy7dqlmqw7cp6h0lcusz0zjtls`
|
|
4290
|
-
- ETH(ERC20): `0xe0bbbc805e0e83341fadc210d6202f4022e50992`
|
|
4291
|
-
- USDT(TRC20): `TA18VUywcNEM9ahh3TTWF3sFpt9rkLnnQa
|
|
4292
|
-
-->
|
|
4293
|
-
<!-- template_contributions_end -->
|
|
4294
|
-
|
|
4295
|
-
### Contributions & Pull Requests
|
|
4296
|
-
|
|
4297
|
-
Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
|
|
4298
|
-
|
|
4299
|
-
<!-- template_star_history -->
|
|
4300
|
-
|
|
4301
|
-
## Star History
|
|
4302
|
-
|
|
4303
|
-
[](https://star-history.com/#tiagosiebler/bybit-api&tiagosiebler/okx-api&tiagosiebler/binance&tiagosiebler/bitget-api&tiagosiebler/bitmart-api&tiagosiebler/gateio-api&tiagosiebler/kucoin-api&tiagosiebler/coinbase-api&tiagosiebler/orderbooks&tiagosiebler/accountstate&tiagosiebler/awesome-crypto-examples&Date)
|
|
4304
|
-
|
|
4305
|
-
<!-- template_star_history_end -->
|
|
4306
|
-
|
|
4307
|
-
================
|
|
4308
|
-
File: src/lib/BaseRestClient.ts
|
|
3995
|
+
File: src/lib/BaseRestClient.ts
|
|
4309
3996
|
================
|
|
4310
3997
|
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
|
|
4311
3998
|
import https from 'https';
|
|
@@ -5854,6 +5541,373 @@ export interface LendingRedemption {
|
|
|
5854
5541
|
items: LendingOrder[];
|
|
5855
5542
|
}
|
|
5856
5543
|
|
|
5544
|
+
================
|
|
5545
|
+
File: README.md
|
|
5546
|
+
================
|
|
5547
|
+
# Node.js & JavaScript SDK for Kucoin REST APIs & Websockets
|
|
5548
|
+
|
|
5549
|
+
<p align="center">
|
|
5550
|
+
<a href="https://www.npmjs.com/package/kucoin-api">
|
|
5551
|
+
<picture>
|
|
5552
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/tiagosiebler/kucoin-api/blob/master/docs/images/logoDarkMode2.svg?raw=true#gh-dark-mode-only">
|
|
5553
|
+
<img alt="SDK Logo" src="https://github.com/tiagosiebler/kucoin-api/blob/master/docs/images/logoBrightMode2.svg?raw=true#gh-light-mode-only">
|
|
5554
|
+
</picture>
|
|
5555
|
+
</a>
|
|
5556
|
+
</p>
|
|
5557
|
+
|
|
5558
|
+
[][1]
|
|
5559
|
+
[][1]
|
|
5560
|
+
[][1]
|
|
5561
|
+
[](https://github.com/tiagosiebler/kucoin-api/actions/workflows/e2etest.yml)
|
|
5562
|
+
[][1]
|
|
5563
|
+
[](https://t.me/nodetraders)
|
|
5564
|
+
|
|
5565
|
+
[1]: https://www.npmjs.com/package/kucoin-api
|
|
5566
|
+
|
|
5567
|
+
Updated & performant JavaScript & Node.js SDK for the Kucoin REST APIs and WebSockets:
|
|
5568
|
+
|
|
5569
|
+
- Professional, robust & performant Kucoin SDK with extensive production use in live trading environments.
|
|
5570
|
+
- Complete integration with all Kucoin REST APIs and WebSockets.
|
|
5571
|
+
- Dedicated clients for Spot, Futures, and Broker operations
|
|
5572
|
+
- Unified WebSocket client for all markets
|
|
5573
|
+
- Complete TypeScript support (with type declarations for most API requests & responses).
|
|
5574
|
+
- Strongly typed requests and responses.
|
|
5575
|
+
- Automated end-to-end tests ensuring reliability.
|
|
5576
|
+
- Actively maintained with a modern, promise-driven interface.
|
|
5577
|
+
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.
|
|
5578
|
+
- Event driven messaging.
|
|
5579
|
+
- Smart WebSocket persistence with automatic reconnection handling.
|
|
5580
|
+
- Emit `reconnected` event when dropped connection is restored.
|
|
5581
|
+
- Support for both public and private WebSocket streams.
|
|
5582
|
+
- Browser-friendly HMAC signature mechanism.
|
|
5583
|
+
- Automatically supports both ESM and CJS projects.
|
|
5584
|
+
- Heavy automated end-to-end testing with real API calls.
|
|
5585
|
+
- Proxy support via axios integration.
|
|
5586
|
+
- Active community support & collaboration in telegram: [Node.js Algo Traders](https://t.me/nodetraders).
|
|
5587
|
+
|
|
5588
|
+
## Table of Contents
|
|
5589
|
+
|
|
5590
|
+
- [Installation](#installation)
|
|
5591
|
+
- [Examples](#examples)
|
|
5592
|
+
- [Issues & Discussion](#issues--discussion)
|
|
5593
|
+
- [Related Projects](#related-projects)
|
|
5594
|
+
- [Documentation](#documentation)
|
|
5595
|
+
- [Structure](#structure)
|
|
5596
|
+
- [Usage](#usage)
|
|
5597
|
+
- [REST API Clients](#rest-api)
|
|
5598
|
+
- [Spot & Margin Trading](#spot--margin-trading)
|
|
5599
|
+
- [Futures Trading](#futures-trading)
|
|
5600
|
+
- [Broker Operations](#broker-operations)
|
|
5601
|
+
- [WebSocket Client](#websockets)
|
|
5602
|
+
- [Public WebSocket Streams](#public-websocket-streams)
|
|
5603
|
+
- [Private WebSocket Streams](#private-websocket-streams)
|
|
5604
|
+
- [Customise Logging](#customise-logging)
|
|
5605
|
+
- [LLMs & AI](#use-with-llms--ai)
|
|
5606
|
+
- [Contributions & Thanks](#contributions--thanks)
|
|
5607
|
+
|
|
5608
|
+
## Installation
|
|
5609
|
+
|
|
5610
|
+
`npm install --save kucoin-api`
|
|
5611
|
+
|
|
5612
|
+
## Examples
|
|
5613
|
+
|
|
5614
|
+
Refer to the [examples](./examples) folder for implementation demos.
|
|
5615
|
+
|
|
5616
|
+
## Issues & Discussion
|
|
5617
|
+
|
|
5618
|
+
- Issues? Check the [issues tab](https://github.com/tiagosiebler/kucoin-api/issues).
|
|
5619
|
+
- Discuss & collaborate with other node devs? Join our [Node.js Algo Traders](https://t.me/nodetraders) engineering community on telegram.
|
|
5620
|
+
- Follow our announcement channel for real-time updates on [X/Twitter](https://x.com/sieblyio)
|
|
5621
|
+
|
|
5622
|
+
<!-- template_related_projects -->
|
|
5623
|
+
|
|
5624
|
+
## Related projects
|
|
5625
|
+
|
|
5626
|
+
Check out my related JavaScript/TypeScript/Node.js projects:
|
|
5627
|
+
|
|
5628
|
+
- Try my REST API & WebSocket SDKs:
|
|
5629
|
+
- [Bybit-api Node.js SDK](https://www.npmjs.com/package/bybit-api)
|
|
5630
|
+
- [Okx-api Node.js SDK](https://www.npmjs.com/package/okx-api)
|
|
5631
|
+
- [Binance Node.js SDK](https://www.npmjs.com/package/binance)
|
|
5632
|
+
- [Gateio-api Node.js SDK](https://www.npmjs.com/package/gateio-api)
|
|
5633
|
+
- [Bitget-api Node.js SDK](https://www.npmjs.com/package/bitget-api)
|
|
5634
|
+
- [Kucoin-api Node.js SDK](https://www.npmjs.com/package/kucoin-api)
|
|
5635
|
+
- [Coinbase-api Node.js SDK](https://www.npmjs.com/package/coinbase-api)
|
|
5636
|
+
- [Bitmart-api Node.js SDK](https://www.npmjs.com/package/bitmart-api)
|
|
5637
|
+
- Try my misc utilities:
|
|
5638
|
+
- [OrderBooks Node.js](https://www.npmjs.com/package/orderbooks)
|
|
5639
|
+
- [Crypto Exchange Account State Cache](https://www.npmjs.com/package/accountstate)
|
|
5640
|
+
- Check out my examples:
|
|
5641
|
+
- [awesome-crypto-examples Node.js](https://github.com/tiagosiebler/awesome-crypto-examples)
|
|
5642
|
+
<!-- template_related_projects_end -->
|
|
5643
|
+
|
|
5644
|
+
## Documentation
|
|
5645
|
+
|
|
5646
|
+
Most methods accept JS objects. These can be populated using parameters specified by Kucoin's API documentation, or check the type definition in each class within this repository.
|
|
5647
|
+
|
|
5648
|
+
### API Documentation Links
|
|
5649
|
+
|
|
5650
|
+
- [Kucoin API Documentation](https://www.kucoin.com/docs-new/introduction)
|
|
5651
|
+
|
|
5652
|
+
### SDK Documentation & Guides
|
|
5653
|
+
|
|
5654
|
+
- Node.js Quick Start Guides
|
|
5655
|
+
- [Spot Node.js Kucoin Quick Start Guide](./examples/kucoin-SPOT-examples-nodejs.md)
|
|
5656
|
+
- [Futures Node.js Kucoin Quick Start Guide](./examples/kucoin-FUTURES-examples-nodejs.md)
|
|
5657
|
+
- [Futures Node.js Kucoin Order Placement Guide](./examples/rest-futures-orders-guide.ts)
|
|
5658
|
+
- [REST Endpoint Function List](./docs/endpointFunctionList.md)
|
|
5659
|
+
|
|
5660
|
+
## Structure
|
|
5661
|
+
|
|
5662
|
+
This project uses typescript. Resources are stored in 2 key structures:
|
|
5663
|
+
|
|
5664
|
+
- [src](./src) - the whole connector written in typescript
|
|
5665
|
+
- [examples](./examples) - some implementation examples & demonstrations. Contributions are welcome!
|
|
5666
|
+
|
|
5667
|
+
---
|
|
5668
|
+
|
|
5669
|
+
# Usage
|
|
5670
|
+
|
|
5671
|
+
Create API credentials on Kucoin's website:
|
|
5672
|
+
|
|
5673
|
+
- [Kucoin API Key Management](https://www.kucoin.com/account/api)
|
|
5674
|
+
|
|
5675
|
+
## REST API
|
|
5676
|
+
|
|
5677
|
+
The SDK provides dedicated REST clients for different trading products:
|
|
5678
|
+
|
|
5679
|
+
- **SpotClient** - for spot trading and margin operations
|
|
5680
|
+
- **FuturesClient** - for futures trading operations
|
|
5681
|
+
- **BrokerClient** - for broker and sub-account management
|
|
5682
|
+
|
|
5683
|
+
### Spot & Margin Trading
|
|
5684
|
+
|
|
5685
|
+
To use Kucoin's Spot and Margin APIs, import (or require) the `SpotClient`:
|
|
5686
|
+
|
|
5687
|
+
```javascript
|
|
5688
|
+
const { SpotClient, FuturesClient } = require('kucoin-api');
|
|
5689
|
+
|
|
5690
|
+
const client = new SpotClient({
|
|
5691
|
+
apiKey: 'apiKeyHere',
|
|
5692
|
+
apiSecret: 'apiSecretHere',
|
|
5693
|
+
apiPassphrase: 'apiPassPhraseHere',
|
|
5694
|
+
});
|
|
5695
|
+
|
|
5696
|
+
try {
|
|
5697
|
+
const spotBuyResult = await client.submitHFOrder({
|
|
5698
|
+
clientOid: client.generateNewOrderID(),
|
|
5699
|
+
side: 'buy',
|
|
5700
|
+
type: 'market',
|
|
5701
|
+
symbol: 'BTC-USDT',
|
|
5702
|
+
size: '0.00001',
|
|
5703
|
+
});
|
|
5704
|
+
console.log('spotBuy ', JSON.stringify(spotBuyResult, null, 2));
|
|
5705
|
+
|
|
5706
|
+
const spotSellResult = await client.submitHFOrder({
|
|
5707
|
+
clientOid: client.generateNewOrderID(),
|
|
5708
|
+
side: 'sell',
|
|
5709
|
+
type: 'market',
|
|
5710
|
+
symbol: 'BTC-USDT',
|
|
5711
|
+
size: '0.00001',
|
|
5712
|
+
});
|
|
5713
|
+
console.log('spotSellResult ', JSON.stringify(spotSellResult, null, 2));
|
|
5714
|
+
} catch (e) {
|
|
5715
|
+
console.error(`Req error: `, e);
|
|
5716
|
+
}
|
|
5717
|
+
```
|
|
5718
|
+
|
|
5719
|
+
See [SpotClient](./src/SpotClient.ts) for further information, or the [examples](./examples/) for lots of usage examples.
|
|
5720
|
+
|
|
5721
|
+
### Futures Trading
|
|
5722
|
+
|
|
5723
|
+
Use the `FuturesClient` for futures trading operations. See [FuturesClient](./src/FuturesClient.ts) for complete API coverage.
|
|
5724
|
+
|
|
5725
|
+
### Broker Operations
|
|
5726
|
+
|
|
5727
|
+
Use the `BrokerClient` for broker and sub-account management operations. See [BrokerClient](./src/BrokerClient.ts) for complete API coverage.
|
|
5728
|
+
|
|
5729
|
+
## WebSockets
|
|
5730
|
+
|
|
5731
|
+
All WebSocket functionality is supported via the unified `WebsocketClient`. This client handles both spot and futures WebSocket streams with automatic connection management and reconnection.
|
|
5732
|
+
|
|
5733
|
+
Key WebSocket features:
|
|
5734
|
+
|
|
5735
|
+
- Event driven messaging
|
|
5736
|
+
- Smart WebSocket persistence with automatic reconnection
|
|
5737
|
+
- Heartbeat mechanisms to detect disconnections
|
|
5738
|
+
- Automatic resubscription after reconnection
|
|
5739
|
+
- Support for both public and private WebSocket streams
|
|
5740
|
+
- Unified client for spot and futures markets
|
|
5741
|
+
|
|
5742
|
+
### Public WebSocket Streams
|
|
5743
|
+
|
|
5744
|
+
For public market data, API credentials are not required:
|
|
5745
|
+
|
|
5746
|
+
All available WebSockets can be used via a shared `WebsocketClient`. The WebSocket client will automatically open/track/manage connections as needed. Each unique connection (one per server URL) is tracked using a WsKey (each WsKey is a string - see [WS_KEY_MAP](src/lib/websocket/websocket-util.ts) for a list of supported values).
|
|
5747
|
+
|
|
5748
|
+
Any subscribe/unsubscribe events will need to include a WsKey, so the WebSocket client understands which connection the event should be routed to. See examples below or in the [examples](./examples/) folder on GitHub.
|
|
5749
|
+
|
|
5750
|
+
Data events are emitted from the WebsocketClient via the `update` event, see example below:
|
|
5751
|
+
|
|
5752
|
+
```javascript
|
|
5753
|
+
const { WebsocketClient } = require('kucoin-api');
|
|
5754
|
+
|
|
5755
|
+
const client = new WebsocketClient();
|
|
5756
|
+
|
|
5757
|
+
client.on('open', (data) => {
|
|
5758
|
+
console.log('open: ', data?.wsKey);
|
|
5759
|
+
});
|
|
5760
|
+
|
|
5761
|
+
// Data received
|
|
5762
|
+
client.on('update', (data) => {
|
|
5763
|
+
console.info('data received: ', JSON.stringify(data));
|
|
5764
|
+
});
|
|
5765
|
+
|
|
5766
|
+
// Something happened, attempting to reconenct
|
|
5767
|
+
client.on('reconnect', (data) => {
|
|
5768
|
+
console.log('reconnect: ', data);
|
|
5769
|
+
});
|
|
5770
|
+
|
|
5771
|
+
// Reconnect successful
|
|
5772
|
+
client.on('reconnected', (data) => {
|
|
5773
|
+
console.log('reconnected: ', data);
|
|
5774
|
+
});
|
|
5775
|
+
|
|
5776
|
+
// Connection closed. If unexpected, expect reconnect -> reconnected.
|
|
5777
|
+
client.on('close', (data) => {
|
|
5778
|
+
console.error('close: ', data);
|
|
5779
|
+
});
|
|
5780
|
+
|
|
5781
|
+
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
|
|
5782
|
+
client.on('response', (data) => {
|
|
5783
|
+
console.info('response: ', data);
|
|
5784
|
+
// throw new Error('res?');
|
|
5785
|
+
});
|
|
5786
|
+
|
|
5787
|
+
client.on('exception', (data) => {
|
|
5788
|
+
console.error('exception: ', {
|
|
5789
|
+
msg: data.msg,
|
|
5790
|
+
errno: data.errno,
|
|
5791
|
+
code: data.code,
|
|
5792
|
+
syscall: data.syscall,
|
|
5793
|
+
hostname: data.hostname,
|
|
5794
|
+
});
|
|
5795
|
+
});
|
|
5796
|
+
|
|
5797
|
+
try {
|
|
5798
|
+
// Optional: await a connection to be ready before subscribing (this is not necessary)
|
|
5799
|
+
// await client.connect('futuresPublicV1');
|
|
5800
|
+
|
|
5801
|
+
/**
|
|
5802
|
+
* Examples for public futures websocket topics (that don't require authentication).
|
|
5803
|
+
*
|
|
5804
|
+
* These should all subscribe via the "futuresPublicV1" wsKey. For detailed usage, refer to the ws-spot-public.ts example.
|
|
5805
|
+
*/
|
|
5806
|
+
client.subscribe(
|
|
5807
|
+
[
|
|
5808
|
+
'/contractMarket/tickerV2:XBTUSDM',
|
|
5809
|
+
'/contractMarket/ticker:XBTUSDM',
|
|
5810
|
+
'/contractMarket/level2:XBTUSDM',
|
|
5811
|
+
'/contractMarket/execution:XBTUSDM',
|
|
5812
|
+
'/contractMarket/level2Depth5:XBTUSDM',
|
|
5813
|
+
'/contractMarket/level2Depth50:XBTUSDM',
|
|
5814
|
+
'/contractMarket/limitCandle:XBTUSDTM_1hour',
|
|
5815
|
+
'/contract/instrument:XBTUSDM',
|
|
5816
|
+
'/contract/announcement',
|
|
5817
|
+
'/contractMarket/snapshot:XBTUSDM',
|
|
5818
|
+
],
|
|
5819
|
+
'futuresPublicV1',
|
|
5820
|
+
);
|
|
5821
|
+
} catch (e) {
|
|
5822
|
+
console.error(`Subscribe exception: `, e);
|
|
5823
|
+
}
|
|
5824
|
+
```
|
|
5825
|
+
|
|
5826
|
+
### Private WebSocket Streams
|
|
5827
|
+
|
|
5828
|
+
For private account data streams, API credentials are required. The WebsocketClient will automatically handle authentication when you provide API credentials.
|
|
5829
|
+
|
|
5830
|
+
See [WebsocketClient](./src/WebsocketClient.ts) for further information and make sure to check the [examples](./examples/) folder for much more detail, especially [ws-spot-public.ts](./examples/ws-spot-public.ts), which explains a lot of detail.
|
|
5831
|
+
|
|
5832
|
+
---
|
|
5833
|
+
|
|
5834
|
+
## Customise Logging
|
|
5835
|
+
|
|
5836
|
+
Pass a custom logger which supports the log methods `trace`, `info` and `error`, or override methods from the default logger as desired.
|
|
5837
|
+
|
|
5838
|
+
```javascript
|
|
5839
|
+
const { WebsocketClient, DefaultLogger } = require('kucoin-api');
|
|
5840
|
+
|
|
5841
|
+
// E.g. customise logging for only the trace level:
|
|
5842
|
+
const logger = {
|
|
5843
|
+
// Inherit existing logger methods, using an object spread
|
|
5844
|
+
...DefaultLogger,
|
|
5845
|
+
// Define a custom trace function to override only that function
|
|
5846
|
+
trace: (...params) => {
|
|
5847
|
+
if (
|
|
5848
|
+
[
|
|
5849
|
+
'Sending ping',
|
|
5850
|
+
// 'Sending upstream ws message: ',
|
|
5851
|
+
'Received pong',
|
|
5852
|
+
].includes(params[0])
|
|
5853
|
+
) {
|
|
5854
|
+
return;
|
|
5855
|
+
}
|
|
5856
|
+
console.log('trace', JSON.stringify(params, null, 2));
|
|
5857
|
+
},
|
|
5858
|
+
};
|
|
5859
|
+
|
|
5860
|
+
const ws = new WebsocketClient(
|
|
5861
|
+
{
|
|
5862
|
+
apiKey: 'apiKeyHere',
|
|
5863
|
+
apiSecret: 'apiSecretHere',
|
|
5864
|
+
apiPassphrase: 'apiPassPhraseHere',
|
|
5865
|
+
},
|
|
5866
|
+
logger,
|
|
5867
|
+
);
|
|
5868
|
+
```
|
|
5869
|
+
|
|
5870
|
+
## Use with LLMs & AI
|
|
5871
|
+
|
|
5872
|
+
This SDK includes a bundled `llms.txt` file in the root of the repository. If you're developing with LLMs, use the included `llms.txt` with your LLM - it will significantly improve the LLMs understanding of how to correctly use this SDK.
|
|
5873
|
+
|
|
5874
|
+
This file contains AI optimised structure of all the functions in this package, and their parameters for easier use with any learning models or artificial intelligence.
|
|
5875
|
+
|
|
5876
|
+
---
|
|
5877
|
+
|
|
5878
|
+
<!-- template_contributions -->
|
|
5879
|
+
|
|
5880
|
+
### Contributions & Thanks
|
|
5881
|
+
|
|
5882
|
+
Have my projects helped you? Share the love, there are many ways you can show your thanks:
|
|
5883
|
+
|
|
5884
|
+
- Star & share my projects.
|
|
5885
|
+
- Are my projects useful? Sponsor me on Github and support my effort to maintain & improve them: https://github.com/sponsors/tiagosiebler
|
|
5886
|
+
- Have an interesting project? Get in touch & invite me to it.
|
|
5887
|
+
- Or buy me all the coffee:
|
|
5888
|
+
- ETH(ERC20): `0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C` <!-- metamask -->
|
|
5889
|
+
|
|
5890
|
+
<!---
|
|
5891
|
+
old ones:
|
|
5892
|
+
- BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk`
|
|
5893
|
+
- BTC(SegWit): `bc1ql64wr9z3khp2gy7dqlmqw7cp6h0lcusz0zjtls`
|
|
5894
|
+
- ETH(ERC20): `0xe0bbbc805e0e83341fadc210d6202f4022e50992`
|
|
5895
|
+
- USDT(TRC20): `TA18VUywcNEM9ahh3TTWF3sFpt9rkLnnQa
|
|
5896
|
+
-->
|
|
5897
|
+
<!-- template_contributions_end -->
|
|
5898
|
+
|
|
5899
|
+
### Contributions & Pull Requests
|
|
5900
|
+
|
|
5901
|
+
Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
|
|
5902
|
+
|
|
5903
|
+
<!-- template_star_history -->
|
|
5904
|
+
|
|
5905
|
+
## Star History
|
|
5906
|
+
|
|
5907
|
+
[](https://star-history.com/#tiagosiebler/bybit-api&tiagosiebler/okx-api&tiagosiebler/binance&tiagosiebler/bitget-api&tiagosiebler/bitmart-api&tiagosiebler/gateio-api&tiagosiebler/kucoin-api&tiagosiebler/coinbase-api&tiagosiebler/orderbooks&tiagosiebler/accountstate&tiagosiebler/awesome-crypto-examples&Date)
|
|
5908
|
+
|
|
5909
|
+
<!-- template_star_history_end -->
|
|
5910
|
+
|
|
5857
5911
|
================
|
|
5858
5912
|
File: src/types/response/spot-trading.ts
|
|
5859
5913
|
================
|
|
@@ -11185,7 +11239,7 @@ File: package.json
|
|
|
11185
11239
|
================
|
|
11186
11240
|
{
|
|
11187
11241
|
"name": "kucoin-api",
|
|
11188
|
-
"version": "2.1.
|
|
11242
|
+
"version": "2.1.16",
|
|
11189
11243
|
"description": "Complete & robust Node.js SDK for Kucoin's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
|
|
11190
11244
|
"scripts": {
|
|
11191
11245
|
"clean": "rm -rf dist",
|
|
@@ -11205,7 +11259,8 @@ File: package.json
|
|
|
11205
11259
|
},
|
|
11206
11260
|
"type": "module",
|
|
11207
11261
|
"files": [
|
|
11208
|
-
"dist/*"
|
|
11262
|
+
"dist/*",
|
|
11263
|
+
"llms.txt"
|
|
11209
11264
|
],
|
|
11210
11265
|
"author": "Tiago Siebler (https://github.com/tiagosiebler)",
|
|
11211
11266
|
"contributors": [
|
package/package.json
CHANGED