@siebly/kraken-api 1.0.2 → 1.0.4
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 +17 -2
- package/dist/cjs/lib/BaseRestClient.js +5 -0
- package/dist/cjs/lib/BaseRestClient.js.map +1 -1
- package/dist/cjs/lib/websocket/WsStore.js +2 -2
- package/dist/cjs/lib/websocket/WsStore.js.map +1 -1
- package/dist/cjs/types/websockets/ws-general.d.ts +3 -1
- package/dist/mjs/lib/BaseRestClient.js +5 -0
- package/dist/mjs/lib/BaseRestClient.js.map +1 -1
- package/dist/mjs/lib/websocket/WsStore.js +2 -2
- package/dist/mjs/lib/websocket/WsStore.js.map +1 -1
- package/dist/mjs/types/websockets/ws-general.d.ts +3 -1
- package/llms.txt +2041 -1983
- package/package.json +7 -1
package/llms.txt
CHANGED
|
@@ -38,7 +38,7 @@ Notes:
|
|
|
38
38
|
------
|
|
39
39
|
- Some files may have been excluded based on .gitignore rules and Repomix's configuration
|
|
40
40
|
- Binary files are not included in this packed representation. Please refer to the Repository Structure section for a complete list of file paths, including binary files
|
|
41
|
-
- Files matching these patterns are excluded: .github/, examples/apidoc/, docs/images/, docs/endpointFunctionList.md, test/, src/util/
|
|
41
|
+
- Files matching these patterns are excluded: .github/, examples/apidoc/, docs/images/, docs/endpointFunctionList.md, test/, src/util/, dist/, lib/
|
|
42
42
|
- Files matching patterns in .gitignore are excluded
|
|
43
43
|
- Files matching default ignore patterns are excluded
|
|
44
44
|
- Content has been compressed - code blocks are separated by ⋮---- delimiter
|
|
@@ -114,6 +114,8 @@ src/
|
|
|
114
114
|
SpotClient.ts
|
|
115
115
|
WebsocketAPIClient.ts
|
|
116
116
|
WebsocketClient.ts
|
|
117
|
+
webpack/
|
|
118
|
+
webpack.config.cjs
|
|
117
119
|
.eslintrc.cjs
|
|
118
120
|
.gitignore
|
|
119
121
|
.nvmrc
|
|
@@ -132,6 +134,178 @@ tsconfig.linting.json
|
|
|
132
134
|
Files
|
|
133
135
|
================================================================
|
|
134
136
|
|
|
137
|
+
================
|
|
138
|
+
File: examples/Derivatives/Public/marketData.ts
|
|
139
|
+
================
|
|
140
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
141
|
+
import { DerivativesClient } from '../../../src/index.js';
|
|
142
|
+
⋮----
|
|
143
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
144
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
145
|
+
// for FUTURES PUBLIC MARKET DATA that requires no authentication
|
|
146
|
+
⋮----
|
|
147
|
+
/**
|
|
148
|
+
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
149
|
+
*/
|
|
150
|
+
⋮----
|
|
151
|
+
// you can initialise public client without api keys as public calls do not require auth
|
|
152
|
+
⋮----
|
|
153
|
+
async function getAllTickers()
|
|
154
|
+
⋮----
|
|
155
|
+
// Get all tickers (all Futures contracts and indices)
|
|
156
|
+
⋮----
|
|
157
|
+
// Response includes for each ticker:
|
|
158
|
+
// - symbol: Market symbol (e.g., PF_BTCUSD)
|
|
159
|
+
// - last: Last fill price
|
|
160
|
+
// - markPrice: Current mark price for margining
|
|
161
|
+
// - bid/ask: Best bid/ask prices
|
|
162
|
+
// - vol24h: 24h volume
|
|
163
|
+
// - openInterest: Current open interest
|
|
164
|
+
// - fundingRate: Current funding rate (perpetuals only)
|
|
165
|
+
⋮----
|
|
166
|
+
async function getTickerBySymbol()
|
|
167
|
+
⋮----
|
|
168
|
+
// Get ticker for specific Futures symbol
|
|
169
|
+
⋮----
|
|
170
|
+
symbol: 'PF_ETHUSD', // Perpetual BTC/USD
|
|
171
|
+
⋮----
|
|
172
|
+
async function getOrderBook()
|
|
173
|
+
⋮----
|
|
174
|
+
// Get order book for specific Futures contract
|
|
175
|
+
⋮----
|
|
176
|
+
// Response includes:
|
|
177
|
+
// - bids: Array of [price, size] sorted descending by price
|
|
178
|
+
// - asks: Array of [price, size] sorted ascending by price
|
|
179
|
+
⋮----
|
|
180
|
+
async function getTradeHistory()
|
|
181
|
+
⋮----
|
|
182
|
+
// Get recent trade history (last 100 trades)
|
|
183
|
+
⋮----
|
|
184
|
+
// Response includes:
|
|
185
|
+
// - price: Fill price
|
|
186
|
+
// - side: Taker side (buy/sell)
|
|
187
|
+
// - size: Fill size
|
|
188
|
+
// - time: Trade timestamp
|
|
189
|
+
// - type: Trade type (fill, liquidation, assignment, etc.)
|
|
190
|
+
⋮----
|
|
191
|
+
async function getTradeHistoryWithTime()
|
|
192
|
+
⋮----
|
|
193
|
+
// Get trades before specific time (last 100 trades before specified time)
|
|
194
|
+
⋮----
|
|
195
|
+
before: Date.now() - 3600000, // 1 hour ago
|
|
196
|
+
⋮----
|
|
197
|
+
// Returns up to 100 trades prior to before time (max 7 days back)
|
|
198
|
+
⋮----
|
|
199
|
+
async function getInstruments()
|
|
200
|
+
⋮----
|
|
201
|
+
// Get all available Futures instruments
|
|
202
|
+
⋮----
|
|
203
|
+
// Response includes for each instrument:
|
|
204
|
+
// - symbol: Market symbol
|
|
205
|
+
// - type: Instrument type (flexible_futures, futures_inverse, etc.)
|
|
206
|
+
// - underlying: Underlying asset
|
|
207
|
+
// - tickSize: Minimum price increment
|
|
208
|
+
// - contractSize: Contract size
|
|
209
|
+
// - tradeable: Whether instrument is tradeable
|
|
210
|
+
⋮----
|
|
211
|
+
async function getFeeSchedules()
|
|
212
|
+
⋮----
|
|
213
|
+
// Get fee schedules for Futures trading
|
|
214
|
+
⋮----
|
|
215
|
+
// Response includes maker and taker fees by tier
|
|
216
|
+
⋮----
|
|
217
|
+
async function getPublicExecutionEvents()
|
|
218
|
+
⋮----
|
|
219
|
+
async function getPublicOrderEvents()
|
|
220
|
+
⋮----
|
|
221
|
+
async function getPublicMarkPriceEvents()
|
|
222
|
+
⋮----
|
|
223
|
+
async function getCandles()
|
|
224
|
+
⋮----
|
|
225
|
+
// Get OHLC candles for Futures
|
|
226
|
+
⋮----
|
|
227
|
+
tickType: 'trade', // spot, mark, or trade
|
|
228
|
+
⋮----
|
|
229
|
+
resolution: '1h', // 1m, 5m, 15m, 30m, 1h, 4h, 12h, 1d, 1w
|
|
230
|
+
⋮----
|
|
231
|
+
// Response includes:
|
|
232
|
+
// - candles: Array of OHLC candles
|
|
233
|
+
// - time: Timestamp in ms
|
|
234
|
+
// - open, high, low, close: Prices
|
|
235
|
+
// - volume: Volume
|
|
236
|
+
// - more_candles: True if more candles available
|
|
237
|
+
⋮----
|
|
238
|
+
async function getCandlesWithTimeRange()
|
|
239
|
+
⋮----
|
|
240
|
+
// Get candles for specific time range
|
|
241
|
+
⋮----
|
|
242
|
+
from: Math.floor((Date.now() - 86400000 * 7) / 1000), // 7 days ago (epoch seconds)
|
|
243
|
+
to: Math.floor(Date.now() / 1000), // now (epoch seconds)
|
|
244
|
+
⋮----
|
|
245
|
+
async function getCandlesWithCount()
|
|
246
|
+
⋮----
|
|
247
|
+
// Get specific number of most recent candles
|
|
248
|
+
⋮----
|
|
249
|
+
tickType: 'mark', // Use mark price candles
|
|
250
|
+
⋮----
|
|
251
|
+
// Tick types:
|
|
252
|
+
// - trade: Trade price candles
|
|
253
|
+
// - mark: Mark price candles
|
|
254
|
+
// - spot: Spot price candles
|
|
255
|
+
⋮----
|
|
256
|
+
// Uncomment the function you want to test:
|
|
257
|
+
⋮----
|
|
258
|
+
// getAllTickers();
|
|
259
|
+
// getTickerBySymbol();
|
|
260
|
+
// getOrderBook();
|
|
261
|
+
// getTradeHistory();
|
|
262
|
+
// getTradeHistoryWithTime();
|
|
263
|
+
// getInstruments();
|
|
264
|
+
// getFeeSchedules();
|
|
265
|
+
// getPublicExecutionEvents();
|
|
266
|
+
// getPublicOrderEvents();
|
|
267
|
+
// getPublicMarkPriceEvents();
|
|
268
|
+
// getCandles();
|
|
269
|
+
// getCandlesWithTimeRange();
|
|
270
|
+
// getCandlesWithCount();
|
|
271
|
+
|
|
272
|
+
================
|
|
273
|
+
File: examples/Spot/Public/marketData.ts
|
|
274
|
+
================
|
|
275
|
+
import { SpotClient } from '../../../src/index.js';
|
|
276
|
+
⋮----
|
|
277
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
278
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
279
|
+
// for PUBLIC MARKET DATA that requires no authentication
|
|
280
|
+
⋮----
|
|
281
|
+
/**
|
|
282
|
+
* import { SpotClient } from '@siebly/kraken-api';
|
|
283
|
+
*/
|
|
284
|
+
⋮----
|
|
285
|
+
// you can initialise public client without api keys as public calls do not require auth
|
|
286
|
+
⋮----
|
|
287
|
+
async function publicCalls()
|
|
288
|
+
⋮----
|
|
289
|
+
// Get server time
|
|
290
|
+
⋮----
|
|
291
|
+
// Get system status
|
|
292
|
+
⋮----
|
|
293
|
+
// Get asset info
|
|
294
|
+
⋮----
|
|
295
|
+
// Get tradable asset pairs
|
|
296
|
+
⋮----
|
|
297
|
+
// Get ticker information
|
|
298
|
+
⋮----
|
|
299
|
+
// Get order book
|
|
300
|
+
⋮----
|
|
301
|
+
// Get OHLC data (candles)
|
|
302
|
+
⋮----
|
|
303
|
+
interval: 60, // 1 minute
|
|
304
|
+
⋮----
|
|
305
|
+
// Get recent trades
|
|
306
|
+
⋮----
|
|
307
|
+
// Get recent spreads
|
|
308
|
+
|
|
135
309
|
================
|
|
136
310
|
File: src/lib/websocket/logger.ts
|
|
137
311
|
================
|
|
@@ -143,6 +317,50 @@ export type LogParams = null | any;
|
|
|
143
317
|
⋮----
|
|
144
318
|
export type DefaultLogger = typeof DefaultLogger;
|
|
145
319
|
|
|
320
|
+
================
|
|
321
|
+
File: src/lib/websocket/rest-client-cache.ts
|
|
322
|
+
================
|
|
323
|
+
import { AxiosRequestConfig } from 'axios';
|
|
324
|
+
⋮----
|
|
325
|
+
import { DerivativesClient } from '../../DerivativesClient.js';
|
|
326
|
+
import { SpotClient } from '../../SpotClient.js';
|
|
327
|
+
import { RestClientOptions } from '../requestUtils.js';
|
|
328
|
+
import { DefaultLogger } from './logger.js';
|
|
329
|
+
⋮----
|
|
330
|
+
interface RestClientStore {
|
|
331
|
+
spot: SpotClient;
|
|
332
|
+
derivatives: DerivativesClient;
|
|
333
|
+
}
|
|
334
|
+
⋮----
|
|
335
|
+
interface WebSocketTokenCache {
|
|
336
|
+
spot?: { token: string; expiresAtMs: number };
|
|
337
|
+
derivatives?: { token: string; expiresAtMs: number };
|
|
338
|
+
}
|
|
339
|
+
⋮----
|
|
340
|
+
/**
|
|
341
|
+
* Caches REST clients and WebSocket tokens to avoid redundant requests.
|
|
342
|
+
*/
|
|
343
|
+
export class RestClientCache
|
|
344
|
+
⋮----
|
|
345
|
+
public setLogger(logger: DefaultLogger, loggerCategory: object): void
|
|
346
|
+
⋮----
|
|
347
|
+
public getSpotRESTClient(
|
|
348
|
+
restOptions: RestClientOptions,
|
|
349
|
+
requestOptions?: AxiosRequestConfig,
|
|
350
|
+
): SpotClient
|
|
351
|
+
⋮----
|
|
352
|
+
public async fetchSpotWebSocketToken(
|
|
353
|
+
restOptions: RestClientOptions,
|
|
354
|
+
requestOptions?: AxiosRequestConfig,
|
|
355
|
+
): Promise<
|
|
356
|
+
⋮----
|
|
357
|
+
// still valid for at least 10s
|
|
358
|
+
⋮----
|
|
359
|
+
public getDerivativesRESTClient(
|
|
360
|
+
restOptions: RestClientOptions,
|
|
361
|
+
requestOptions?: AxiosRequestConfig,
|
|
362
|
+
): DerivativesClient
|
|
363
|
+
|
|
146
364
|
================
|
|
147
365
|
File: src/lib/websocket/WsStore.ts
|
|
148
366
|
================
|
|
@@ -5059,48 +5277,238 @@ async function start()
|
|
|
5059
5277
|
// Notification: https://docs.kraken.com/api/docs/futures-api/websocket/notifications
|
|
5060
5278
|
|
|
5061
5279
|
================
|
|
5062
|
-
File:
|
|
5280
|
+
File: examples/Spot/Private/account.ts
|
|
5063
5281
|
================
|
|
5064
|
-
|
|
5065
|
-
|
|
5066
|
-
import { DerivativesClient } from '../../DerivativesClient.js';
|
|
5067
|
-
import { SpotClient } from '../../SpotClient.js';
|
|
5068
|
-
import { RestClientOptions } from '../requestUtils.js';
|
|
5069
|
-
import { DefaultLogger } from './logger.js';
|
|
5282
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5283
|
+
import { SpotClient } from '../../../src/index.js';
|
|
5070
5284
|
⋮----
|
|
5071
|
-
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
}
|
|
5285
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
5286
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
5287
|
+
// for ACCOUNT INFORMATION
|
|
5075
5288
|
⋮----
|
|
5076
|
-
|
|
5077
|
-
|
|
5078
|
-
|
|
5079
|
-
}
|
|
5289
|
+
/**
|
|
5290
|
+
* import { SpotClient } from '@siebly/kraken-api';
|
|
5291
|
+
*/
|
|
5080
5292
|
⋮----
|
|
5293
|
+
// initialise the client
|
|
5081
5294
|
/**
|
|
5082
|
-
*
|
|
5295
|
+
*
|
|
5296
|
+
* Kraken API uses API Key and Private Key (base64 encoded)
|
|
5297
|
+
*
|
|
5298
|
+
* Example:
|
|
5299
|
+
* {
|
|
5300
|
+
* apiKey: 'your-api-key',
|
|
5301
|
+
* apiSecret: 'your-base64-encoded-private-key',
|
|
5302
|
+
* }
|
|
5303
|
+
*
|
|
5304
|
+
* API Key Permissions Required:
|
|
5305
|
+
* - Funds permissions - Query
|
|
5306
|
+
* - Data - Query ledger entries
|
|
5307
|
+
*
|
|
5083
5308
|
*/
|
|
5084
|
-
export class RestClientCache
|
|
5085
5309
|
⋮----
|
|
5086
|
-
|
|
5310
|
+
async function getAccountBalance()
|
|
5087
5311
|
⋮----
|
|
5088
|
-
|
|
5089
|
-
restOptions: RestClientOptions,
|
|
5090
|
-
requestOptions?: AxiosRequestConfig,
|
|
5091
|
-
): SpotClient
|
|
5312
|
+
// Get all cash balances (net of pending withdrawals)
|
|
5092
5313
|
⋮----
|
|
5093
|
-
|
|
5094
|
-
|
|
5095
|
-
|
|
5096
|
-
|
|
5314
|
+
// Note: Staking/Earn assets may have these extensions:
|
|
5315
|
+
// .B - balances in new yield-bearing products
|
|
5316
|
+
// .F - balances earning automatically in Kraken Rewards
|
|
5317
|
+
// .T - tokenized assets
|
|
5097
5318
|
⋮----
|
|
5098
|
-
|
|
5319
|
+
async function getExtendedBalance()
|
|
5099
5320
|
⋮----
|
|
5100
|
-
|
|
5101
|
-
|
|
5102
|
-
|
|
5103
|
-
)
|
|
5321
|
+
// Get extended balances including credits and held amounts
|
|
5322
|
+
// Available balance = balance + credit - credit_used - hold_trade
|
|
5323
|
+
⋮----
|
|
5324
|
+
async function getTradeBalance()
|
|
5325
|
+
⋮----
|
|
5326
|
+
// Get trade balance summary (margin info)
|
|
5327
|
+
⋮----
|
|
5328
|
+
// Response includes:
|
|
5329
|
+
// - eb: equivalent balance
|
|
5330
|
+
// - tb: trade balance
|
|
5331
|
+
// - m: margin amount
|
|
5332
|
+
// - n: unrealized P&L
|
|
5333
|
+
// - e: equity
|
|
5334
|
+
// - mf: free margin
|
|
5335
|
+
⋮----
|
|
5336
|
+
async function getLedgers()
|
|
5337
|
+
⋮----
|
|
5338
|
+
// Query specific ledger entries by ID
|
|
5339
|
+
⋮----
|
|
5340
|
+
// Ledger entry types include:
|
|
5341
|
+
// - trade, deposit, withdrawal, transfer, margin
|
|
5342
|
+
// - adjustment, rollover, spend, receive, settled
|
|
5343
|
+
// - credit, staking, reward, dividend, sale, conversion
|
|
5344
|
+
⋮----
|
|
5345
|
+
async function getLedgersInfo()
|
|
5346
|
+
⋮----
|
|
5347
|
+
// Get ledger info with filters (returns 50 most recent by default)
|
|
5348
|
+
⋮----
|
|
5349
|
+
asset: 'XBT', // Filter by asset
|
|
5350
|
+
type: 'deposit', // Filter by type
|
|
5351
|
+
⋮----
|
|
5352
|
+
async function getTradingVolume()
|
|
5353
|
+
⋮----
|
|
5354
|
+
// Get 30-day USD trading volume and fee schedule
|
|
5355
|
+
⋮----
|
|
5356
|
+
// Response includes:
|
|
5357
|
+
// - currency: volume currency
|
|
5358
|
+
// - volume: current trading volume
|
|
5359
|
+
// - fees: fee schedule by pair
|
|
5360
|
+
// - fees_maker: maker fee schedule
|
|
5361
|
+
⋮----
|
|
5362
|
+
// Uncomment the function you want to test:
|
|
5363
|
+
⋮----
|
|
5364
|
+
// getExtendedBalance();
|
|
5365
|
+
// getTradeBalance();
|
|
5366
|
+
// getLedgers();
|
|
5367
|
+
// getLedgersInfo();
|
|
5368
|
+
// getTradingVolume();
|
|
5369
|
+
|
|
5370
|
+
================
|
|
5371
|
+
File: examples/Spot/Private/orderManagement.ts
|
|
5372
|
+
================
|
|
5373
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5374
|
+
import { SpotClient } from '../../../src/index.js';
|
|
5375
|
+
⋮----
|
|
5376
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
5377
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
5378
|
+
// for ORDER MANAGEMENT
|
|
5379
|
+
⋮----
|
|
5380
|
+
/**
|
|
5381
|
+
* import { SpotClient } from '@siebly/kraken-api';
|
|
5382
|
+
*/
|
|
5383
|
+
⋮----
|
|
5384
|
+
// initialise the client
|
|
5385
|
+
/**
|
|
5386
|
+
*
|
|
5387
|
+
* Kraken API uses API Key and Private Key (base64 encoded)
|
|
5388
|
+
*
|
|
5389
|
+
* Example:
|
|
5390
|
+
* {
|
|
5391
|
+
* apiKey: 'your-api-key',
|
|
5392
|
+
* apiSecret: 'your-base64-encoded-private-key',
|
|
5393
|
+
* }
|
|
5394
|
+
*
|
|
5395
|
+
* API Key Permissions Required:
|
|
5396
|
+
* - Funds permissions - Query (for balance)
|
|
5397
|
+
* - Orders and trades - Query open orders & trades
|
|
5398
|
+
* - Orders and trades - Query closed orders & trades
|
|
5399
|
+
*
|
|
5400
|
+
*/
|
|
5401
|
+
⋮----
|
|
5402
|
+
async function getTradeBalance()
|
|
5403
|
+
⋮----
|
|
5404
|
+
// Get trade balance summary
|
|
5405
|
+
⋮----
|
|
5406
|
+
async function getOpenOrders()
|
|
5407
|
+
⋮----
|
|
5408
|
+
// Get all open orders
|
|
5409
|
+
⋮----
|
|
5410
|
+
async function getOpenOrdersWithTrades()
|
|
5411
|
+
⋮----
|
|
5412
|
+
// Get open orders with related trades
|
|
5413
|
+
⋮----
|
|
5414
|
+
trades: true, // Include trades related to orders
|
|
5415
|
+
⋮----
|
|
5416
|
+
async function getOpenOrdersByClientId()
|
|
5417
|
+
⋮----
|
|
5418
|
+
// Get open orders filtered by client order ID
|
|
5419
|
+
⋮----
|
|
5420
|
+
async function getClosedOrders()
|
|
5421
|
+
⋮----
|
|
5422
|
+
// Get closed orders (last 50)
|
|
5423
|
+
⋮----
|
|
5424
|
+
async function getClosedOrdersWithFilters()
|
|
5425
|
+
⋮----
|
|
5426
|
+
// Get closed orders with filters
|
|
5427
|
+
⋮----
|
|
5428
|
+
trades: true, // Include related trades
|
|
5429
|
+
start: Math.floor(Date.now() / 1000) - 86400 * 7, // Last 7 days
|
|
5430
|
+
closetime: 'close', // Filter by close time
|
|
5431
|
+
⋮----
|
|
5432
|
+
async function getClosedOrdersByClientId()
|
|
5433
|
+
⋮----
|
|
5434
|
+
// Get closed orders by client order ID
|
|
5435
|
+
⋮----
|
|
5436
|
+
async function getOrdersByTxId()
|
|
5437
|
+
⋮----
|
|
5438
|
+
// Query specific orders by transaction ID
|
|
5439
|
+
⋮----
|
|
5440
|
+
// Uncomment the function you want to test:
|
|
5441
|
+
⋮----
|
|
5442
|
+
// getTradeBalance();
|
|
5443
|
+
// getOpenOrders();
|
|
5444
|
+
// getOpenOrdersWithTrades();
|
|
5445
|
+
// getOpenOrdersByClientId();
|
|
5446
|
+
// getClosedOrders();
|
|
5447
|
+
// getClosedOrdersWithFilters();
|
|
5448
|
+
// getClosedOrdersByClientId();
|
|
5449
|
+
// getOrdersByTxId();
|
|
5450
|
+
|
|
5451
|
+
================
|
|
5452
|
+
File: examples/Spot/Private/submitOrder.ts
|
|
5453
|
+
================
|
|
5454
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
5455
|
+
import { SpotClient } from '../../../src/index.js';
|
|
5456
|
+
⋮----
|
|
5457
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
5458
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
5459
|
+
// for SUBMITTING ORDERS
|
|
5460
|
+
⋮----
|
|
5461
|
+
/**
|
|
5462
|
+
* import { SpotClient } from '@siebly/kraken-api';
|
|
5463
|
+
*/
|
|
5464
|
+
⋮----
|
|
5465
|
+
// initialise the client
|
|
5466
|
+
/**
|
|
5467
|
+
*
|
|
5468
|
+
* Kraken API uses API Key and Private Key (base64 encoded)
|
|
5469
|
+
*
|
|
5470
|
+
* Example:
|
|
5471
|
+
* {
|
|
5472
|
+
* apiKey: 'your-api-key',
|
|
5473
|
+
* apiSecret: 'your-base64-encoded-private-key',
|
|
5474
|
+
* }
|
|
5475
|
+
*
|
|
5476
|
+
* API Key Permissions Required: Orders and trades - Create & modify orders
|
|
5477
|
+
*
|
|
5478
|
+
*/
|
|
5479
|
+
⋮----
|
|
5480
|
+
async function submitMarketOrder()
|
|
5481
|
+
⋮----
|
|
5482
|
+
// submit market spot order
|
|
5483
|
+
⋮----
|
|
5484
|
+
async function submitLimitOrder()
|
|
5485
|
+
⋮----
|
|
5486
|
+
// Submit limit spot order
|
|
5487
|
+
⋮----
|
|
5488
|
+
async function submitLimitOrderWithFlags()
|
|
5489
|
+
⋮----
|
|
5490
|
+
// Submit post-only limit order (maker-only)
|
|
5491
|
+
⋮----
|
|
5492
|
+
oflags: 'post', // post-only flag
|
|
5493
|
+
timeinforce: 'GTC', // Good-til-cancelled
|
|
5494
|
+
⋮----
|
|
5495
|
+
async function submitBatchOrders()
|
|
5496
|
+
⋮----
|
|
5497
|
+
// Submit batch of orders (minimum 2, maximum 15)
|
|
5498
|
+
// All orders must be for the same pair
|
|
5499
|
+
⋮----
|
|
5500
|
+
async function submitBatchOrdersValidateOnly()
|
|
5501
|
+
⋮----
|
|
5502
|
+
// Validate batch orders without submitting them
|
|
5503
|
+
⋮----
|
|
5504
|
+
validate: true, // Only validate, don't submit
|
|
5505
|
+
⋮----
|
|
5506
|
+
// Uncomment the function you want to test:
|
|
5507
|
+
⋮----
|
|
5508
|
+
// submitLimitOrder();
|
|
5509
|
+
// submitLimitOrderWithFlags();
|
|
5510
|
+
// submitBatchOrders();
|
|
5511
|
+
// submitBatchOrdersValidateOnly();
|
|
5104
5512
|
|
|
5105
5513
|
================
|
|
5106
5514
|
File: src/lib/webCryptoAPI.ts
|
|
@@ -5535,6 +5943,51 @@ export interface WSAPIEditSpotOrderParams {
|
|
|
5535
5943
|
validate?: boolean;
|
|
5536
5944
|
}
|
|
5537
5945
|
|
|
5946
|
+
================
|
|
5947
|
+
File: src/types/response/shared.types.ts
|
|
5948
|
+
================
|
|
5949
|
+
import { RestClientOptions } from '../../lib/requestUtils.js';
|
|
5950
|
+
⋮----
|
|
5951
|
+
export type DerivativesAPISuccessResponse<TData> = {
|
|
5952
|
+
result: 'success';
|
|
5953
|
+
serverTime: string;
|
|
5954
|
+
} & TData;
|
|
5955
|
+
⋮----
|
|
5956
|
+
export interface DerivativesAPIErrorResponse {
|
|
5957
|
+
result: 'error';
|
|
5958
|
+
error: string;
|
|
5959
|
+
serverTime: string;
|
|
5960
|
+
}
|
|
5961
|
+
⋮----
|
|
5962
|
+
export type DerivativesAPIResponse<TData> =
|
|
5963
|
+
| DerivativesAPISuccessResponse<TData>
|
|
5964
|
+
| DerivativesAPIErrorResponse;
|
|
5965
|
+
⋮----
|
|
5966
|
+
export type SpotAPISuccessResponse<TData> = {
|
|
5967
|
+
error: string[];
|
|
5968
|
+
result: TData;
|
|
5969
|
+
};
|
|
5970
|
+
⋮----
|
|
5971
|
+
export interface SpotAPIErrorResponse {
|
|
5972
|
+
// e.g.{ error: [ 'EGeneral:Invalid arguments:ordertype' ] },
|
|
5973
|
+
error: string[];
|
|
5974
|
+
}
|
|
5975
|
+
⋮----
|
|
5976
|
+
// e.g.{ error: [ 'EGeneral:Invalid arguments:ordertype' ] },
|
|
5977
|
+
⋮----
|
|
5978
|
+
export type SpotAPIResponse<TData> =
|
|
5979
|
+
| SpotAPISuccessResponse<TData>
|
|
5980
|
+
| SpotAPIErrorResponse;
|
|
5981
|
+
⋮----
|
|
5982
|
+
export interface GenericAPIError<TBody = any> {
|
|
5983
|
+
code: number;
|
|
5984
|
+
message: string;
|
|
5985
|
+
body: TBody;
|
|
5986
|
+
headers: Record<string, string>;
|
|
5987
|
+
requestOptions: RestClientOptions;
|
|
5988
|
+
requestParams: Record<string, any>;
|
|
5989
|
+
}
|
|
5990
|
+
|
|
5538
5991
|
================
|
|
5539
5992
|
File: src/InstitutionalClient.ts
|
|
5540
5993
|
================
|
|
@@ -5801,10 +6254,22 @@ getOtcExposures(params?: {
|
|
|
5801
6254
|
checkOtcClient(params?:
|
|
5802
6255
|
|
|
5803
6256
|
================
|
|
5804
|
-
File:
|
|
6257
|
+
File: webpack/webpack.config.cjs
|
|
5805
6258
|
================
|
|
5806
|
-
|
|
5807
|
-
|
|
6259
|
+
function generateConfig(name)
|
|
6260
|
+
⋮----
|
|
6261
|
+
// Add '.ts' and '.tsx' as resolvable extensions.
|
|
6262
|
+
⋮----
|
|
6263
|
+
// Node.js core modules not available in browsers
|
|
6264
|
+
// The REST client's https.Agent (for keepAlive) is Node.js-only and won't work in browsers
|
|
6265
|
+
⋮----
|
|
6266
|
+
// Code is already transpiled from TypeScript, no additional loaders needed
|
|
6267
|
+
|
|
6268
|
+
================
|
|
6269
|
+
File: jest.config.ts
|
|
6270
|
+
================
|
|
6271
|
+
/**
|
|
6272
|
+
* For a detailed explanation regarding each configuration property, visit:
|
|
5808
6273
|
* https://jestjs.io/docs/configuration
|
|
5809
6274
|
*/
|
|
5810
6275
|
⋮----
|
|
@@ -5999,9 +6464,91 @@ The above copyright notice and this permission notice shall be included in all c
|
|
|
5999
6464
|
|
|
6000
6465
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
6001
6466
|
|
|
6467
|
+
================
|
|
6468
|
+
File: examples/Derivatives/Private/orderManagement.ts
|
|
6469
|
+
================
|
|
6470
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6471
|
+
// This example shows how to call Kraken API endpoint with either node.js,
|
|
6472
|
+
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
6473
|
+
// for ORDER MANAGEMENT
|
|
6474
|
+
⋮----
|
|
6475
|
+
import { DerivativesClient } from '../../../src/index.js';
|
|
6476
|
+
⋮----
|
|
6477
|
+
/**
|
|
6478
|
+
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
6479
|
+
*/
|
|
6480
|
+
⋮----
|
|
6481
|
+
// initialise the client
|
|
6482
|
+
/**
|
|
6483
|
+
*
|
|
6484
|
+
* Kraken Futures API uses API Key and API Secret
|
|
6485
|
+
*
|
|
6486
|
+
* Example:
|
|
6487
|
+
* {
|
|
6488
|
+
* apiKey: 'your-api-key',
|
|
6489
|
+
* apiSecret: 'your-api-secret',
|
|
6490
|
+
* }
|
|
6491
|
+
*/
|
|
6492
|
+
⋮----
|
|
6493
|
+
async function editOrder()
|
|
6494
|
+
⋮----
|
|
6495
|
+
// Edit an existing order
|
|
6496
|
+
⋮----
|
|
6497
|
+
orderId: 'a04d0f84-36d4-4499-8382-96fcfc3ce7aa', // Or use cliOrdId instead
|
|
6498
|
+
limitPrice: 1100, // New limit price
|
|
6499
|
+
// or add some other parameters you want to edit
|
|
6500
|
+
⋮----
|
|
6501
|
+
// Response includes:
|
|
6502
|
+
// - status: edited, invalidSize, invalidPrice, etc.
|
|
6503
|
+
// - orderEvents: Array of order events
|
|
6504
|
+
⋮----
|
|
6505
|
+
async function cancelOrder()
|
|
6506
|
+
⋮----
|
|
6507
|
+
// Cancel a single order
|
|
6508
|
+
⋮----
|
|
6509
|
+
order_id: 'a04d0f84-36d4-4499-8382-96fcfc3ce7aa', // Or use cliOrdId
|
|
6510
|
+
⋮----
|
|
6511
|
+
// Response status:
|
|
6512
|
+
// - cancelled: Successfully cancelled
|
|
6513
|
+
// - filled: Order was already filled
|
|
6514
|
+
// - notFound: Order not found
|
|
6515
|
+
⋮----
|
|
6516
|
+
async function cancelAllOrders()
|
|
6517
|
+
⋮----
|
|
6518
|
+
// Cancel all open orders
|
|
6519
|
+
⋮----
|
|
6520
|
+
// Response includes:
|
|
6521
|
+
// - status: cancelled or noOrdersToCancel
|
|
6522
|
+
// - cancelledOrders: Array of cancelled order IDs
|
|
6523
|
+
⋮----
|
|
6524
|
+
async function cancelAllOrdersBySymbol()
|
|
6525
|
+
⋮----
|
|
6526
|
+
// Cancel all orders for specific symbol
|
|
6527
|
+
⋮----
|
|
6528
|
+
async function batchOrderManagement()
|
|
6529
|
+
⋮----
|
|
6530
|
+
// Send, edit, and cancel orders in a single batch request
|
|
6531
|
+
⋮----
|
|
6532
|
+
// Edit existing order
|
|
6533
|
+
⋮----
|
|
6534
|
+
// Cancel existing order
|
|
6535
|
+
⋮----
|
|
6536
|
+
// Response includes batchStatus array with results for each order
|
|
6537
|
+
// - status: placed, edited, cancelled, or rejection reason
|
|
6538
|
+
// - order_tag: Maps back to your request
|
|
6539
|
+
⋮----
|
|
6540
|
+
// Uncomment the function you want to test:
|
|
6541
|
+
⋮----
|
|
6542
|
+
// editOrder();
|
|
6543
|
+
// cancelOrder();
|
|
6544
|
+
// cancelAllOrders();
|
|
6545
|
+
// cancelAllOrdersBySymbol();
|
|
6546
|
+
// batchOrderManagement();
|
|
6547
|
+
|
|
6002
6548
|
================
|
|
6003
6549
|
File: examples/Derivatives/Private/testnet.ts
|
|
6004
6550
|
================
|
|
6551
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6005
6552
|
import { DerivativesClient } from '../../../src/index.js';
|
|
6006
6553
|
⋮----
|
|
6007
6554
|
// This example shows how to call Kraken API testnet (demo) endpoints with either node.js,
|
|
@@ -6231,7 +6778,7 @@ async function start()
|
|
|
6231
6778
|
================
|
|
6232
6779
|
File: examples/Derivatives/WebSockets/publicWs.ts
|
|
6233
6780
|
================
|
|
6234
|
-
|
|
6781
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6235
6782
|
import {
|
|
6236
6783
|
DefaultLogger,
|
|
6237
6784
|
LogParams,
|
|
@@ -6302,13 +6849,14 @@ async function start()
|
|
|
6302
6849
|
// Trade: https://docs.kraken.com/api/docs/futures-api/websocket/ticker
|
|
6303
6850
|
|
|
6304
6851
|
================
|
|
6305
|
-
File: examples/Spot/Private/
|
|
6852
|
+
File: examples/Spot/Private/depositWithdraw.ts
|
|
6306
6853
|
================
|
|
6854
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6307
6855
|
import { SpotClient } from '../../../src/index.js';
|
|
6308
6856
|
⋮----
|
|
6309
6857
|
// This example shows how to call Kraken API endpoint with either node.js,
|
|
6310
6858
|
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
6311
|
-
// for
|
|
6859
|
+
// for DEPOSIT AND WITHDRAWAL
|
|
6312
6860
|
⋮----
|
|
6313
6861
|
/**
|
|
6314
6862
|
* import { SpotClient } from '@siebly/kraken-api';
|
|
@@ -6327,252 +6875,160 @@ import { SpotClient } from '../../../src/index.js';
|
|
|
6327
6875
|
*
|
|
6328
6876
|
* API Key Permissions Required:
|
|
6329
6877
|
* - Funds permissions - Query
|
|
6878
|
+
* - Funds permissions - Deposit
|
|
6879
|
+
* - Funds permissions - Withdraw
|
|
6330
6880
|
* - Data - Query ledger entries
|
|
6331
6881
|
*
|
|
6332
6882
|
*/
|
|
6333
6883
|
⋮----
|
|
6334
|
-
async function
|
|
6884
|
+
async function withdrawFunds()
|
|
6335
6885
|
⋮----
|
|
6336
|
-
//
|
|
6886
|
+
// Make a withdrawal request
|
|
6337
6887
|
⋮----
|
|
6338
|
-
//
|
|
6339
|
-
// .B - balances in new yield-bearing products
|
|
6340
|
-
// .F - balances earning automatically in Kraken Rewards
|
|
6341
|
-
// .T - tokenized assets
|
|
6888
|
+
key: 'btc_2709', // Withdrawal key name from your account
|
|
6342
6889
|
⋮----
|
|
6343
|
-
|
|
6890
|
+
address: 'bc1kar0ssrr7xf3vy5l6d3lydnwkre5og2zz3f5ldq', // Optional confirmation
|
|
6344
6891
|
⋮----
|
|
6345
|
-
//
|
|
6346
|
-
//
|
|
6892
|
+
// Response includes:
|
|
6893
|
+
// - refid: Reference ID for the withdrawal
|
|
6347
6894
|
⋮----
|
|
6348
|
-
async function
|
|
6895
|
+
async function getDepositMethods()
|
|
6349
6896
|
⋮----
|
|
6350
|
-
// Get
|
|
6897
|
+
// Get available deposit methods for an asset
|
|
6351
6898
|
⋮----
|
|
6352
6899
|
// Response includes:
|
|
6353
|
-
// -
|
|
6354
|
-
// -
|
|
6355
|
-
// -
|
|
6356
|
-
// -
|
|
6357
|
-
// -
|
|
6358
|
-
// -
|
|
6900
|
+
// - method: Name of deposit method
|
|
6901
|
+
// - limit: Maximum net amount that can be deposited
|
|
6902
|
+
// - fee: Fees that will be paid
|
|
6903
|
+
// - address-setup-fee: Whether method has setup fee
|
|
6904
|
+
// - gen-address: Whether new addresses can be generated
|
|
6905
|
+
// - minimum: Minimum net amount
|
|
6359
6906
|
⋮----
|
|
6360
|
-
async function
|
|
6907
|
+
async function getDepositAddresses()
|
|
6361
6908
|
⋮----
|
|
6362
|
-
//
|
|
6909
|
+
// Get or generate deposit address
|
|
6363
6910
|
⋮----
|
|
6364
|
-
//
|
|
6365
|
-
// - trade, deposit, withdrawal, transfer, margin
|
|
6366
|
-
// - adjustment, rollover, spend, receive, settled
|
|
6367
|
-
// - credit, staking, reward, dividend, sale, conversion
|
|
6911
|
+
new: false, // Set to true to generate new address
|
|
6368
6912
|
⋮----
|
|
6369
|
-
|
|
6913
|
+
// Response includes:
|
|
6914
|
+
// - address: Deposit address
|
|
6915
|
+
// - expiretm: Expiration time or 0 if not expiring
|
|
6916
|
+
// - new: Whether address has ever been used
|
|
6917
|
+
// - tag: Contains tags/memos for XRP, STX, XLM, EOS
|
|
6370
6918
|
⋮----
|
|
6371
|
-
|
|
6919
|
+
async function generateNewDepositAddress()
|
|
6372
6920
|
⋮----
|
|
6373
|
-
|
|
6374
|
-
type: 'deposit', // Filter by type
|
|
6921
|
+
// Generate a new deposit address
|
|
6375
6922
|
⋮----
|
|
6376
|
-
|
|
6923
|
+
new: true, // Generate new address
|
|
6377
6924
|
⋮----
|
|
6378
|
-
|
|
6925
|
+
async function getWithdrawalMethods()
|
|
6926
|
+
⋮----
|
|
6927
|
+
// Get available withdrawal methods
|
|
6379
6928
|
⋮----
|
|
6380
6929
|
// Response includes:
|
|
6381
|
-
// -
|
|
6382
|
-
// -
|
|
6383
|
-
// -
|
|
6384
|
-
// -
|
|
6930
|
+
// - asset: Name of asset being withdrawn
|
|
6931
|
+
// - method: Name of withdrawal method
|
|
6932
|
+
// - network: Blockchain/network name
|
|
6933
|
+
// - minimum: Minimum net amount that can be withdrawn
|
|
6385
6934
|
⋮----
|
|
6386
|
-
|
|
6935
|
+
async function getWithdrawalAddresses()
|
|
6387
6936
|
⋮----
|
|
6388
|
-
//
|
|
6389
|
-
// getTradeBalance();
|
|
6390
|
-
// getLedgers();
|
|
6391
|
-
// getLedgersInfo();
|
|
6392
|
-
// getTradingVolume();
|
|
6393
|
-
|
|
6394
|
-
================
|
|
6395
|
-
File: examples/Spot/Private/orderManagement.ts
|
|
6396
|
-
================
|
|
6397
|
-
import { SpotClient } from '../../../src/index.js';
|
|
6937
|
+
// Get withdrawal addresses
|
|
6398
6938
|
⋮----
|
|
6399
|
-
|
|
6400
|
-
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
6401
|
-
// for ORDER MANAGEMENT
|
|
6939
|
+
verified: true, // Filter by verification status
|
|
6402
6940
|
⋮----
|
|
6403
|
-
|
|
6404
|
-
|
|
6405
|
-
|
|
6941
|
+
// Response includes:
|
|
6942
|
+
// - address: Withdrawal address
|
|
6943
|
+
// - asset: Asset name
|
|
6944
|
+
// - method: Withdrawal method
|
|
6945
|
+
// - key: Withdrawal key name
|
|
6946
|
+
// - tag: Tags/memos for XRP, STX, XLM, EOS
|
|
6947
|
+
// - verified: Verification status
|
|
6406
6948
|
⋮----
|
|
6407
|
-
|
|
6408
|
-
/**
|
|
6409
|
-
*
|
|
6410
|
-
* Kraken API uses API Key and Private Key (base64 encoded)
|
|
6411
|
-
*
|
|
6412
|
-
* Example:
|
|
6413
|
-
* {
|
|
6414
|
-
* apiKey: 'your-api-key',
|
|
6415
|
-
* apiSecret: 'your-base64-encoded-private-key',
|
|
6416
|
-
* }
|
|
6417
|
-
*
|
|
6418
|
-
* API Key Permissions Required:
|
|
6419
|
-
* - Funds permissions - Query (for balance)
|
|
6420
|
-
* - Orders and trades - Query open orders & trades
|
|
6421
|
-
* - Orders and trades - Query closed orders & trades
|
|
6422
|
-
*
|
|
6423
|
-
*/
|
|
6949
|
+
async function getWithdrawalAddressByKey()
|
|
6424
6950
|
⋮----
|
|
6425
|
-
|
|
6951
|
+
// Find withdrawal address by key name
|
|
6426
6952
|
⋮----
|
|
6427
|
-
//
|
|
6953
|
+
key: 'btc_2709', // Withdrawal key name
|
|
6428
6954
|
⋮----
|
|
6429
|
-
async function
|
|
6955
|
+
async function getWithdrawalsStatus()
|
|
6430
6956
|
⋮----
|
|
6431
|
-
// Get
|
|
6957
|
+
// Get status of recent withdrawals
|
|
6432
6958
|
⋮----
|
|
6433
|
-
|
|
6959
|
+
// Status values:
|
|
6960
|
+
// - Initial: withdrawal just created
|
|
6961
|
+
// - Pending: withdrawal pending
|
|
6962
|
+
// - Settled: withdrawal settled
|
|
6963
|
+
// - Success: withdrawal successful
|
|
6964
|
+
// - Failure: withdrawal failed
|
|
6434
6965
|
⋮----
|
|
6435
|
-
//
|
|
6966
|
+
// Status properties (if available):
|
|
6967
|
+
// - cancel-pending: cancelation requested
|
|
6968
|
+
// - canceled: canceled
|
|
6969
|
+
// - cancel-denied: cancelation denied
|
|
6970
|
+
// - return: return transaction by Kraken
|
|
6971
|
+
// - onhold: on hold pending review
|
|
6436
6972
|
⋮----
|
|
6437
|
-
|
|
6973
|
+
async function getWithdrawalsStatusWithPagination()
|
|
6438
6974
|
⋮----
|
|
6439
|
-
|
|
6975
|
+
// Get withdrawals with pagination
|
|
6440
6976
|
⋮----
|
|
6441
|
-
|
|
6977
|
+
cursor: true, // Enable pagination
|
|
6978
|
+
limit: 10, // Results per page
|
|
6442
6979
|
⋮----
|
|
6443
|
-
async function
|
|
6980
|
+
async function getDepositsStatus()
|
|
6444
6981
|
⋮----
|
|
6445
|
-
// Get
|
|
6982
|
+
// Get status of recent deposits
|
|
6446
6983
|
⋮----
|
|
6447
|
-
|
|
6984
|
+
// Similar status values as withdrawals
|
|
6448
6985
|
⋮----
|
|
6449
|
-
|
|
6986
|
+
async function cancelWithdrawal()
|
|
6450
6987
|
⋮----
|
|
6451
|
-
|
|
6452
|
-
start: Math.floor(Date.now() / 1000) - 86400 * 7, // Last 7 days
|
|
6453
|
-
closetime: 'close', // Filter by close time
|
|
6988
|
+
// Cancel a recent withdrawal (if not yet processed)
|
|
6454
6989
|
⋮----
|
|
6455
|
-
|
|
6990
|
+
refid: 'AGBSO6T-UFMTTQ-I7KGS6', // Reference ID from withdrawal
|
|
6456
6991
|
⋮----
|
|
6457
|
-
//
|
|
6992
|
+
// Returns true if cancellation successful
|
|
6458
6993
|
⋮----
|
|
6459
|
-
async function
|
|
6994
|
+
async function getWithdrawalInfo()
|
|
6460
6995
|
⋮----
|
|
6461
|
-
//
|
|
6996
|
+
// Get withdrawal fee information before withdrawing
|
|
6462
6997
|
⋮----
|
|
6463
|
-
//
|
|
6464
|
-
|
|
6465
|
-
//
|
|
6466
|
-
//
|
|
6467
|
-
//
|
|
6468
|
-
// getOpenOrdersByClientId();
|
|
6469
|
-
// getClosedOrders();
|
|
6470
|
-
// getClosedOrdersWithFilters();
|
|
6471
|
-
// getClosedOrdersByClientId();
|
|
6472
|
-
// getOrdersByTxId();
|
|
6473
|
-
|
|
6474
|
-
================
|
|
6475
|
-
File: examples/Spot/Private/submitOrder.ts
|
|
6476
|
-
================
|
|
6477
|
-
import { SpotClient } from '../../../src/index.js';
|
|
6478
|
-
⋮----
|
|
6479
|
-
// This example shows how to call Kraken API endpoint with either node.js,
|
|
6480
|
-
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
6481
|
-
// for SUBMITTING ORDERS
|
|
6482
|
-
⋮----
|
|
6483
|
-
/**
|
|
6484
|
-
* import { SpotClient } from '@siebly/kraken-api';
|
|
6485
|
-
*/
|
|
6486
|
-
⋮----
|
|
6487
|
-
// initialise the client
|
|
6488
|
-
/**
|
|
6489
|
-
*
|
|
6490
|
-
* Kraken API uses API Key and Private Key (base64 encoded)
|
|
6491
|
-
*
|
|
6492
|
-
* Example:
|
|
6493
|
-
* {
|
|
6494
|
-
* apiKey: 'your-api-key',
|
|
6495
|
-
* apiSecret: 'your-base64-encoded-private-key',
|
|
6496
|
-
* }
|
|
6497
|
-
*
|
|
6498
|
-
* API Key Permissions Required: Orders and trades - Create & modify orders
|
|
6499
|
-
*
|
|
6500
|
-
*/
|
|
6501
|
-
⋮----
|
|
6502
|
-
async function submitMarketOrder()
|
|
6503
|
-
⋮----
|
|
6504
|
-
// submit market spot order
|
|
6505
|
-
⋮----
|
|
6506
|
-
async function submitLimitOrder()
|
|
6507
|
-
⋮----
|
|
6508
|
-
// Submit limit spot order
|
|
6509
|
-
⋮----
|
|
6510
|
-
async function submitLimitOrderWithFlags()
|
|
6511
|
-
⋮----
|
|
6512
|
-
// Submit post-only limit order (maker-only)
|
|
6513
|
-
⋮----
|
|
6514
|
-
oflags: 'post', // post-only flag
|
|
6515
|
-
timeinforce: 'GTC', // Good-til-cancelled
|
|
6516
|
-
⋮----
|
|
6517
|
-
async function submitBatchOrders()
|
|
6518
|
-
⋮----
|
|
6519
|
-
// Submit batch of orders (minimum 2, maximum 15)
|
|
6520
|
-
// All orders must be for the same pair
|
|
6998
|
+
// Response includes:
|
|
6999
|
+
// - method: Withdrawal method
|
|
7000
|
+
// - limit: Maximum amount that can be withdrawn
|
|
7001
|
+
// - amount: Net amount to be withdrawn
|
|
7002
|
+
// - fee: Withdrawal fee
|
|
6521
7003
|
⋮----
|
|
6522
|
-
async function
|
|
7004
|
+
async function transferToFutures()
|
|
6523
7005
|
⋮----
|
|
6524
|
-
|
|
7006
|
+
async function transferToSubaccount()
|
|
6525
7007
|
⋮----
|
|
6526
|
-
|
|
7008
|
+
from: 'UID', // get From API, getSubaccounts()
|
|
7009
|
+
to: 'UID', // get From API, getSubaccounts()
|
|
6527
7010
|
⋮----
|
|
6528
7011
|
// Uncomment the function you want to test:
|
|
6529
7012
|
⋮----
|
|
6530
|
-
//
|
|
6531
|
-
//
|
|
6532
|
-
//
|
|
6533
|
-
//
|
|
6534
|
-
|
|
6535
|
-
|
|
6536
|
-
|
|
6537
|
-
|
|
6538
|
-
|
|
6539
|
-
|
|
6540
|
-
//
|
|
6541
|
-
//
|
|
6542
|
-
//
|
|
6543
|
-
|
|
6544
|
-
/**
|
|
6545
|
-
* import { SpotClient } from '@siebly/kraken-api';
|
|
6546
|
-
*/
|
|
6547
|
-
⋮----
|
|
6548
|
-
// you can initialise public client without api keys as public calls do not require auth
|
|
6549
|
-
⋮----
|
|
6550
|
-
async function publicCalls()
|
|
6551
|
-
⋮----
|
|
6552
|
-
// Get server time
|
|
6553
|
-
⋮----
|
|
6554
|
-
// Get system status
|
|
6555
|
-
⋮----
|
|
6556
|
-
// Get asset info
|
|
6557
|
-
⋮----
|
|
6558
|
-
// Get tradable asset pairs
|
|
6559
|
-
⋮----
|
|
6560
|
-
// Get ticker information
|
|
6561
|
-
⋮----
|
|
6562
|
-
// Get order book
|
|
6563
|
-
⋮----
|
|
6564
|
-
// Get OHLC data (candles)
|
|
6565
|
-
⋮----
|
|
6566
|
-
interval: 60, // 1 minute
|
|
6567
|
-
⋮----
|
|
6568
|
-
// Get recent trades
|
|
6569
|
-
⋮----
|
|
6570
|
-
// Get recent spreads
|
|
7013
|
+
// withdrawFunds();
|
|
7014
|
+
// getDepositMethods();
|
|
7015
|
+
// getDepositAddresses();
|
|
7016
|
+
// generateNewDepositAddress();
|
|
7017
|
+
// getWithdrawalMethods();
|
|
7018
|
+
// getWithdrawalAddresses();
|
|
7019
|
+
// getWithdrawalAddressByKey();
|
|
7020
|
+
// getWithdrawalsStatus();
|
|
7021
|
+
// getWithdrawalsStatusWithPagination();
|
|
7022
|
+
// getDepositsStatus();
|
|
7023
|
+
// cancelWithdrawal();
|
|
7024
|
+
// getWithdrawalInfo();
|
|
7025
|
+
// transferToFutures();
|
|
7026
|
+
// transferToSubaccount();
|
|
6571
7027
|
|
|
6572
7028
|
================
|
|
6573
7029
|
File: examples/Spot/WebSockets/publicWs.ts
|
|
6574
7030
|
================
|
|
6575
|
-
|
|
7031
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
6576
7032
|
import {
|
|
6577
7033
|
DefaultLogger,
|
|
6578
7034
|
LogParams,
|
|
@@ -6771,51 +7227,6 @@ async function start()
|
|
|
6771
7227
|
* Do note that all of these include the "spotPrivateV2" WsKey reference. This tells the WebsocketClient to use the private "wss://ws-auth.kraken.com/v2" endpoint for these private subscription requests.
|
|
6772
7228
|
*/
|
|
6773
7229
|
|
|
6774
|
-
================
|
|
6775
|
-
File: src/types/response/shared.types.ts
|
|
6776
|
-
================
|
|
6777
|
-
import { RestClientOptions } from '../../lib/requestUtils.js';
|
|
6778
|
-
⋮----
|
|
6779
|
-
export type DerivativesAPISuccessResponse<TData> = {
|
|
6780
|
-
result: 'success';
|
|
6781
|
-
serverTime: string;
|
|
6782
|
-
} & TData;
|
|
6783
|
-
⋮----
|
|
6784
|
-
export interface DerivativesAPIErrorResponse {
|
|
6785
|
-
result: 'error';
|
|
6786
|
-
error: string;
|
|
6787
|
-
serverTime: string;
|
|
6788
|
-
}
|
|
6789
|
-
⋮----
|
|
6790
|
-
export type DerivativesAPIResponse<TData> =
|
|
6791
|
-
| DerivativesAPISuccessResponse<TData>
|
|
6792
|
-
| DerivativesAPIErrorResponse;
|
|
6793
|
-
⋮----
|
|
6794
|
-
export type SpotAPISuccessResponse<TData> = {
|
|
6795
|
-
error: string[];
|
|
6796
|
-
result: TData;
|
|
6797
|
-
};
|
|
6798
|
-
⋮----
|
|
6799
|
-
export interface SpotAPIErrorResponse {
|
|
6800
|
-
// e.g.{ error: [ 'EGeneral:Invalid arguments:ordertype' ] },
|
|
6801
|
-
error: string[];
|
|
6802
|
-
}
|
|
6803
|
-
⋮----
|
|
6804
|
-
// e.g.{ error: [ 'EGeneral:Invalid arguments:ordertype' ] },
|
|
6805
|
-
⋮----
|
|
6806
|
-
export type SpotAPIResponse<TData> =
|
|
6807
|
-
| SpotAPISuccessResponse<TData>
|
|
6808
|
-
| SpotAPIErrorResponse;
|
|
6809
|
-
⋮----
|
|
6810
|
-
export interface GenericAPIError<TBody = any> {
|
|
6811
|
-
code: number;
|
|
6812
|
-
message: string;
|
|
6813
|
-
body: TBody;
|
|
6814
|
-
headers: Record<string, string>;
|
|
6815
|
-
requestOptions: RestClientOptions;
|
|
6816
|
-
requestParams: Record<string, any>;
|
|
6817
|
-
}
|
|
6818
|
-
|
|
6819
7230
|
================
|
|
6820
7231
|
File: src/types/response/wsapi.types.ts
|
|
6821
7232
|
================
|
|
@@ -6873,136 +7284,7 @@ export interface WSAPIEditSpotOrderResult {
|
|
|
6873
7284
|
}
|
|
6874
7285
|
|
|
6875
7286
|
================
|
|
6876
|
-
File: src/
|
|
6877
|
-
================
|
|
6878
|
-
import { AxiosRequestConfig } from 'axios';
|
|
6879
|
-
⋮----
|
|
6880
|
-
import { RestClientOptions } from '../../lib/requestUtils.js';
|
|
6881
|
-
⋮----
|
|
6882
|
-
/** General configuration for the WebsocketClient */
|
|
6883
|
-
export interface WSClientConfigurableOptions {
|
|
6884
|
-
/** Your API key */
|
|
6885
|
-
apiKey?: string;
|
|
6886
|
-
|
|
6887
|
-
/** Your API secret */
|
|
6888
|
-
apiSecret?: string;
|
|
6889
|
-
|
|
6890
|
-
/**
|
|
6891
|
-
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
6892
|
-
*
|
|
6893
|
-
* Note: as of November 2025, only the derivatives environment supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
6894
|
-
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
6895
|
-
*
|
|
6896
|
-
* Refer to the following for more information:
|
|
6897
|
-
* https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
6898
|
-
*/
|
|
6899
|
-
testnet?: boolean;
|
|
6900
|
-
|
|
6901
|
-
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
6902
|
-
recvWindow?: number;
|
|
6903
|
-
|
|
6904
|
-
/** How often to check if the connection is alive */
|
|
6905
|
-
pingInterval?: number;
|
|
6906
|
-
|
|
6907
|
-
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
6908
|
-
pongTimeout?: number;
|
|
6909
|
-
|
|
6910
|
-
/** Delay in milliseconds before respawning the connection */
|
|
6911
|
-
reconnectTimeout?: number;
|
|
6912
|
-
|
|
6913
|
-
restOptions?: RestClientOptions;
|
|
6914
|
-
requestOptions?: AxiosRequestConfig;
|
|
6915
|
-
|
|
6916
|
-
wsOptions?: {
|
|
6917
|
-
protocols?: string[];
|
|
6918
|
-
agent?: any;
|
|
6919
|
-
};
|
|
6920
|
-
|
|
6921
|
-
wsUrl?: string;
|
|
6922
|
-
|
|
6923
|
-
/**
|
|
6924
|
-
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
6925
|
-
*
|
|
6926
|
-
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
6927
|
-
*/
|
|
6928
|
-
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
|
|
6929
|
-
|
|
6930
|
-
/**
|
|
6931
|
-
* If you authenticated the WS API before, automatically try to re-authenticate the WS API if you're disconnected/reconnected for any reason.
|
|
6932
|
-
*/
|
|
6933
|
-
reauthWSAPIOnReconnect?: boolean;
|
|
6934
|
-
}
|
|
6935
|
-
⋮----
|
|
6936
|
-
/** Your API key */
|
|
6937
|
-
⋮----
|
|
6938
|
-
/** Your API secret */
|
|
6939
|
-
⋮----
|
|
6940
|
-
/**
|
|
6941
|
-
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
6942
|
-
*
|
|
6943
|
-
* Note: as of November 2025, only the derivatives environment supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
6944
|
-
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
6945
|
-
*
|
|
6946
|
-
* Refer to the following for more information:
|
|
6947
|
-
* https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
6948
|
-
*/
|
|
6949
|
-
⋮----
|
|
6950
|
-
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
6951
|
-
⋮----
|
|
6952
|
-
/** How often to check if the connection is alive */
|
|
6953
|
-
⋮----
|
|
6954
|
-
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
6955
|
-
⋮----
|
|
6956
|
-
/** Delay in milliseconds before respawning the connection */
|
|
6957
|
-
⋮----
|
|
6958
|
-
/**
|
|
6959
|
-
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
6960
|
-
*
|
|
6961
|
-
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
6962
|
-
*/
|
|
6963
|
-
⋮----
|
|
6964
|
-
/**
|
|
6965
|
-
* If you authenticated the WS API before, automatically try to re-authenticate the WS API if you're disconnected/reconnected for any reason.
|
|
6966
|
-
*/
|
|
6967
|
-
⋮----
|
|
6968
|
-
/**
|
|
6969
|
-
* WS configuration that's always defined, regardless of user configuration
|
|
6970
|
-
* (usually comes from defaults if there's no user-provided values)
|
|
6971
|
-
*/
|
|
6972
|
-
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
|
|
6973
|
-
pingInterval: number;
|
|
6974
|
-
pongTimeout: number;
|
|
6975
|
-
reconnectTimeout: number;
|
|
6976
|
-
recvWindow: number;
|
|
6977
|
-
|
|
6978
|
-
/**
|
|
6979
|
-
* If true, require a "receipt" that the connection is ready for use (e.g. a specific event type)
|
|
6980
|
-
*/
|
|
6981
|
-
requireConnectionReadyConfirmation: boolean;
|
|
6982
|
-
authPrivateConnectionsOnConnect: boolean;
|
|
6983
|
-
authPrivateRequests: boolean;
|
|
6984
|
-
reauthWSAPIOnReconnect: boolean;
|
|
6985
|
-
|
|
6986
|
-
/**
|
|
6987
|
-
* Whether to use native WebSocket ping/pong frames for heartbeats
|
|
6988
|
-
*/
|
|
6989
|
-
useNativeHeartbeats: boolean;
|
|
6990
|
-
}
|
|
6991
|
-
⋮----
|
|
6992
|
-
/**
|
|
6993
|
-
* If true, require a "receipt" that the connection is ready for use (e.g. a specific event type)
|
|
6994
|
-
*/
|
|
6995
|
-
⋮----
|
|
6996
|
-
/**
|
|
6997
|
-
* Whether to use native WebSocket ping/pong frames for heartbeats
|
|
6998
|
-
*/
|
|
6999
|
-
⋮----
|
|
7000
|
-
export type WsMarket = 'spot' | 'futures';
|
|
7001
|
-
⋮----
|
|
7002
|
-
export type WsEventInternalSrc = 'event' | 'function' | 'frame';
|
|
7003
|
-
|
|
7004
|
-
================
|
|
7005
|
-
File: src/index.ts
|
|
7287
|
+
File: src/index.ts
|
|
7006
7288
|
================
|
|
7007
7289
|
|
|
7008
7290
|
|
|
@@ -8202,44 +8484,14 @@ updateOAuthFastApiKey(params: OauthUpdateFastApiKeyParams): Promise<
|
|
|
8202
8484
|
listOAuthFastApiKeys(): Promise<
|
|
8203
8485
|
|
|
8204
8486
|
================
|
|
8205
|
-
File: .
|
|
8206
|
-
================
|
|
8207
|
-
!.gitkeep
|
|
8208
|
-
.DS_STORE
|
|
8209
|
-
*.log
|
|
8210
|
-
npm-debug.log*
|
|
8211
|
-
yarn-debug.log*
|
|
8212
|
-
yarn-error.log*
|
|
8213
|
-
lerna-debug.log*
|
|
8214
|
-
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
8215
|
-
pids
|
|
8216
|
-
*.pid
|
|
8217
|
-
*.seed
|
|
8218
|
-
*.pid.lock
|
|
8219
|
-
node_modules/
|
|
8220
|
-
.npm
|
|
8221
|
-
.eslintcache
|
|
8222
|
-
.node_repl_history
|
|
8223
|
-
*.tgz
|
|
8224
|
-
.yarn-integrity
|
|
8225
|
-
.env
|
|
8226
|
-
.env.test
|
|
8227
|
-
.cache
|
|
8228
|
-
dist
|
|
8229
|
-
bundleReport.html
|
|
8230
|
-
.history/
|
|
8231
|
-
examples/ignoreme
|
|
8232
|
-
localtest.sh
|
|
8233
|
-
repomix.sh
|
|
8234
|
-
|
|
8235
|
-
================
|
|
8236
|
-
File: examples/Derivatives/Private/orderManagement.ts
|
|
8487
|
+
File: examples/Derivatives/Private/account.ts
|
|
8237
8488
|
================
|
|
8489
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
8490
|
+
import { DerivativesClient } from '../../../src/index.js';
|
|
8491
|
+
⋮----
|
|
8238
8492
|
// This example shows how to call Kraken API endpoint with either node.js,
|
|
8239
8493
|
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
8240
|
-
// for
|
|
8241
|
-
⋮----
|
|
8242
|
-
import { DerivativesClient } from '../../../src/index.js';
|
|
8494
|
+
// for FUTURES ACCOUNT INFORMATION
|
|
8243
8495
|
⋮----
|
|
8244
8496
|
/**
|
|
8245
8497
|
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
@@ -8255,372 +8507,393 @@ import { DerivativesClient } from '../../../src/index.js';
|
|
|
8255
8507
|
* apiKey: 'your-api-key',
|
|
8256
8508
|
* apiSecret: 'your-api-secret',
|
|
8257
8509
|
* }
|
|
8510
|
+
*
|
|
8511
|
+
* API Key Permissions Required:
|
|
8512
|
+
* - Read access for account information
|
|
8513
|
+
* - Withdrawal permissions for transfers
|
|
8514
|
+
*
|
|
8258
8515
|
*/
|
|
8259
8516
|
⋮----
|
|
8260
|
-
async function
|
|
8261
|
-
⋮----
|
|
8262
|
-
// Edit an existing order
|
|
8263
|
-
⋮----
|
|
8264
|
-
orderId: 'a04d0f84-36d4-4499-8382-96fcfc3ce7aa', // Or use cliOrdId instead
|
|
8265
|
-
limitPrice: 1100, // New limit price
|
|
8266
|
-
// or add some other parameters you want to edit
|
|
8267
|
-
⋮----
|
|
8268
|
-
// Response includes:
|
|
8269
|
-
// - status: edited, invalidSize, invalidPrice, etc.
|
|
8270
|
-
// - orderEvents: Array of order events
|
|
8271
|
-
⋮----
|
|
8272
|
-
async function cancelOrder()
|
|
8273
|
-
⋮----
|
|
8274
|
-
// Cancel a single order
|
|
8275
|
-
⋮----
|
|
8276
|
-
order_id: 'a04d0f84-36d4-4499-8382-96fcfc3ce7aa', // Or use cliOrdId
|
|
8277
|
-
⋮----
|
|
8278
|
-
// Response status:
|
|
8279
|
-
// - cancelled: Successfully cancelled
|
|
8280
|
-
// - filled: Order was already filled
|
|
8281
|
-
// - notFound: Order not found
|
|
8282
|
-
⋮----
|
|
8283
|
-
async function cancelAllOrders()
|
|
8517
|
+
async function getWallets()
|
|
8284
8518
|
⋮----
|
|
8285
|
-
//
|
|
8519
|
+
// Get all wallets (cash and margin accounts)
|
|
8286
8520
|
⋮----
|
|
8287
8521
|
// Response includes:
|
|
8288
|
-
// -
|
|
8289
|
-
// -
|
|
8522
|
+
// - cash: Cash account with balances
|
|
8523
|
+
// - flex: Multi-collateral wallet with margin info
|
|
8524
|
+
// - Available margin, portfolio value, PnL
|
|
8525
|
+
// - Initial/maintenance margin requirements
|
|
8526
|
+
// For each margin account:
|
|
8527
|
+
// - balances, auxiliary (pv, pnl, af, funding)
|
|
8528
|
+
// - marginRequirements (im, mm, lt, tt)
|
|
8529
|
+
// - triggerEstimates
|
|
8290
8530
|
⋮----
|
|
8291
|
-
async function
|
|
8531
|
+
async function getOpenPositions()
|
|
8292
8532
|
⋮----
|
|
8293
|
-
//
|
|
8533
|
+
// Get all open Futures positions
|
|
8294
8534
|
⋮----
|
|
8295
|
-
|
|
8535
|
+
// Response includes for each position:
|
|
8536
|
+
// - symbol: Futures symbol
|
|
8537
|
+
// - side: long or short
|
|
8538
|
+
// - size: Position size
|
|
8539
|
+
// - price: Average entry price
|
|
8540
|
+
// - fillTime: When position was opened
|
|
8541
|
+
// - unrealizedFunding: Unrealized funding
|
|
8296
8542
|
⋮----
|
|
8297
|
-
|
|
8543
|
+
async function getFills()
|
|
8298
8544
|
⋮----
|
|
8299
|
-
//
|
|
8545
|
+
// Get filled orders history (last 100)
|
|
8300
8546
|
⋮----
|
|
8301
|
-
//
|
|
8547
|
+
// Response includes for each fill:
|
|
8548
|
+
// - fill_id: Unique fill identifier
|
|
8549
|
+
// - order_id: Associated order ID
|
|
8550
|
+
// - symbol: Futures symbol
|
|
8551
|
+
// - side: buy or sell
|
|
8552
|
+
// - size: Fill size
|
|
8553
|
+
// - price: Fill price
|
|
8554
|
+
// - fillTime: Execution time
|
|
8555
|
+
// - fillType: maker, taker, liquidation, etc.
|
|
8302
8556
|
⋮----
|
|
8303
|
-
|
|
8304
|
-
// - status: placed, edited, cancelled, or rejection reason
|
|
8305
|
-
// - order_tag: Maps back to your request
|
|
8557
|
+
async function getFillsBeforeTime()
|
|
8306
8558
|
⋮----
|
|
8307
|
-
//
|
|
8559
|
+
// Get fills before specific time
|
|
8308
8560
|
⋮----
|
|
8309
|
-
|
|
8310
|
-
// cancelOrder();
|
|
8311
|
-
// cancelAllOrders();
|
|
8312
|
-
// cancelAllOrdersBySymbol();
|
|
8313
|
-
// batchOrderManagement();
|
|
8314
|
-
|
|
8315
|
-
================
|
|
8316
|
-
File: examples/Derivatives/Public/marketData.ts
|
|
8317
|
-
================
|
|
8318
|
-
import { DerivativesClient } from '../../../src/index.js';
|
|
8561
|
+
lastFillTime: new Date(Date.now() - 86400000).toISOString(), // 24h ago
|
|
8319
8562
|
⋮----
|
|
8320
|
-
//
|
|
8321
|
-
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
8322
|
-
// for FUTURES PUBLIC MARKET DATA that requires no authentication
|
|
8563
|
+
// Returns 100 fills before specified time
|
|
8323
8564
|
⋮----
|
|
8324
|
-
|
|
8325
|
-
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
8326
|
-
*/
|
|
8565
|
+
async function initiateWalletTransfer()
|
|
8327
8566
|
⋮----
|
|
8328
|
-
//
|
|
8567
|
+
// Transfer between margin accounts or to/from cash account
|
|
8329
8568
|
⋮----
|
|
8330
|
-
|
|
8569
|
+
// Transfers funds between accounts instantly
|
|
8331
8570
|
⋮----
|
|
8332
|
-
|
|
8571
|
+
async function initiateWithdrawalToSpot()
|
|
8333
8572
|
⋮----
|
|
8334
|
-
//
|
|
8335
|
-
// - symbol: Market symbol (e.g., PF_BTCUSD)
|
|
8336
|
-
// - last: Last fill price
|
|
8337
|
-
// - markPrice: Current mark price for margining
|
|
8338
|
-
// - bid/ask: Best bid/ask prices
|
|
8339
|
-
// - vol24h: 24h volume
|
|
8340
|
-
// - openInterest: Current open interest
|
|
8341
|
-
// - fundingRate: Current funding rate (perpetuals only)
|
|
8573
|
+
// Withdraw from Futures to Spot wallet
|
|
8342
8574
|
⋮----
|
|
8343
|
-
|
|
8575
|
+
sourceWallet: 'cash', // Default is cash wallet
|
|
8344
8576
|
⋮----
|
|
8345
|
-
//
|
|
8577
|
+
// Response includes:
|
|
8578
|
+
// - uid: Withdrawal reference ID
|
|
8346
8579
|
⋮----
|
|
8347
|
-
|
|
8580
|
+
async function getOrderEvents()
|
|
8348
8581
|
⋮----
|
|
8349
|
-
|
|
8582
|
+
// Get order history events
|
|
8350
8583
|
⋮----
|
|
8351
|
-
|
|
8584
|
+
sort: 'desc', // desc = newest first
|
|
8585
|
+
opened: true, // Include opened orders
|
|
8586
|
+
closed: true, // Include closed orders
|
|
8352
8587
|
⋮----
|
|
8353
8588
|
// Response includes:
|
|
8354
|
-
// -
|
|
8355
|
-
// -
|
|
8589
|
+
// - elements: Array of order events
|
|
8590
|
+
// - Order placed, cancelled, rejected, executed events
|
|
8591
|
+
// - continuationToken: For pagination
|
|
8356
8592
|
⋮----
|
|
8357
|
-
async function
|
|
8593
|
+
async function getOrderEventsBySymbol()
|
|
8358
8594
|
⋮----
|
|
8359
|
-
//
|
|
8595
|
+
// Filter order events by symbol
|
|
8360
8596
|
⋮----
|
|
8361
|
-
|
|
8362
|
-
// - price: Fill price
|
|
8363
|
-
// - side: Taker side (buy/sell)
|
|
8364
|
-
// - size: Fill size
|
|
8365
|
-
// - time: Trade timestamp
|
|
8366
|
-
// - type: Trade type (fill, liquidation, assignment, etc.)
|
|
8597
|
+
async function getExecutionEvents()
|
|
8367
8598
|
⋮----
|
|
8368
|
-
|
|
8599
|
+
// Get execution/trade history
|
|
8369
8600
|
⋮----
|
|
8370
|
-
//
|
|
8601
|
+
// Response includes for each execution:
|
|
8602
|
+
// - execution: Fill details (price, quantity, timestamp)
|
|
8603
|
+
// - order: Associated order details
|
|
8604
|
+
// - fee: Fee paid
|
|
8605
|
+
// - positionSize: Position size after execution
|
|
8371
8606
|
⋮----
|
|
8372
|
-
|
|
8607
|
+
async function getExecutionEventsBySymbol()
|
|
8373
8608
|
⋮----
|
|
8374
|
-
//
|
|
8609
|
+
// Filter executions by symbol
|
|
8375
8610
|
⋮----
|
|
8376
|
-
async function
|
|
8611
|
+
async function getAccountLog()
|
|
8377
8612
|
⋮----
|
|
8378
|
-
// Get all
|
|
8613
|
+
// Get account log (all account activities)
|
|
8379
8614
|
⋮----
|
|
8380
|
-
//
|
|
8381
|
-
// -
|
|
8382
|
-
// -
|
|
8383
|
-
// -
|
|
8384
|
-
// - tickSize: Minimum price increment
|
|
8385
|
-
// - contractSize: Contract size
|
|
8386
|
-
// - tradeable: Whether instrument is tradeable
|
|
8615
|
+
// Log includes:
|
|
8616
|
+
// - futures trade, liquidation, funding rate change
|
|
8617
|
+
// - conversions, transfers, settlements
|
|
8618
|
+
// - interest payments, fees
|
|
8387
8619
|
⋮----
|
|
8388
|
-
async function
|
|
8620
|
+
async function getAccountLogFiltered()
|
|
8389
8621
|
⋮----
|
|
8390
|
-
//
|
|
8622
|
+
// Filter account log by info types
|
|
8391
8623
|
⋮----
|
|
8392
|
-
|
|
8624
|
+
async function enableFuturesSubTrading()
|
|
8393
8625
|
⋮----
|
|
8394
|
-
async function
|
|
8626
|
+
async function getSubaccountTradingStatus()
|
|
8395
8627
|
⋮----
|
|
8396
|
-
async function
|
|
8628
|
+
async function getSubaccounts()
|
|
8629
|
+
// Uncomment the function you want to test:
|
|
8397
8630
|
⋮----
|
|
8398
|
-
|
|
8631
|
+
// getWallets();
|
|
8632
|
+
// getOpenPositions();
|
|
8633
|
+
// getFills();
|
|
8634
|
+
// getFillsBeforeTime();
|
|
8635
|
+
// initiateWalletTransfer();
|
|
8636
|
+
// initiateWithdrawalToSpot();
|
|
8637
|
+
// getOrderEvents();
|
|
8638
|
+
// getOrderEventsBySymbol();
|
|
8639
|
+
// getExecutionEvents();
|
|
8640
|
+
// getExecutionEventsBySymbol();
|
|
8641
|
+
// getAccountLog();
|
|
8642
|
+
// getAccountLogFiltered();
|
|
8643
|
+
// enableFuturesSubTrading();
|
|
8644
|
+
// getSubaccountTradingStatus();
|
|
8645
|
+
// getSubaccounts();
|
|
8646
|
+
|
|
8647
|
+
================
|
|
8648
|
+
File: src/lib/requestUtils.ts
|
|
8649
|
+
================
|
|
8650
|
+
/**
|
|
8651
|
+
* Used to switch how authentication/requests work under the hood
|
|
8652
|
+
*/
|
|
8399
8653
|
⋮----
|
|
8400
|
-
|
|
8654
|
+
/** Spot */
|
|
8401
8655
|
⋮----
|
|
8402
|
-
|
|
8656
|
+
/** Futures */
|
|
8403
8657
|
⋮----
|
|
8404
|
-
|
|
8658
|
+
/** Futures Demo */
|
|
8405
8659
|
⋮----
|
|
8406
|
-
|
|
8660
|
+
/** Institutional */
|
|
8407
8661
|
⋮----
|
|
8408
|
-
|
|
8409
|
-
// - candles: Array of OHLC candles
|
|
8410
|
-
// - time: Timestamp in ms
|
|
8411
|
-
// - open, high, low, close: Prices
|
|
8412
|
-
// - volume: Volume
|
|
8413
|
-
// - more_candles: True if more candles available
|
|
8662
|
+
/** Partner */
|
|
8414
8663
|
⋮----
|
|
8415
|
-
|
|
8664
|
+
export type RestClientType =
|
|
8665
|
+
(typeof REST_CLIENT_TYPE_ENUM)[keyof typeof REST_CLIENT_TYPE_ENUM];
|
|
8416
8666
|
⋮----
|
|
8417
|
-
|
|
8667
|
+
export interface RestClientOptions {
|
|
8668
|
+
/** Your API key */
|
|
8669
|
+
apiKey?: string;
|
|
8670
|
+
|
|
8671
|
+
/** Your API secret */
|
|
8672
|
+
apiSecret?: string;
|
|
8673
|
+
|
|
8674
|
+
/**
|
|
8675
|
+
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
8676
|
+
*
|
|
8677
|
+
* Note: as of November 2025, only the DerivativesClient supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
8678
|
+
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
8679
|
+
*
|
|
8680
|
+
* Refer to the following for more information:
|
|
8681
|
+
* https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
8682
|
+
*/
|
|
8683
|
+
testnet?: boolean;
|
|
8684
|
+
|
|
8685
|
+
/**
|
|
8686
|
+
* Use access token instead of sign, if this is provided.
|
|
8687
|
+
* For guidance refer to: https://github.com/tiagosiebler/kucoin-api/issues/2
|
|
8688
|
+
*/
|
|
8689
|
+
apiAccessToken?: string;
|
|
8690
|
+
|
|
8691
|
+
/** Default: false. If true, we'll throw errors if any params are undefined */
|
|
8692
|
+
strictParamValidation?: boolean;
|
|
8693
|
+
|
|
8694
|
+
/**
|
|
8695
|
+
* Optionally override API protocol + domain
|
|
8696
|
+
* e.g baseUrl: 'https://api.kraken.com'
|
|
8697
|
+
**/
|
|
8698
|
+
baseUrl?: string;
|
|
8699
|
+
|
|
8700
|
+
/** Default: true. whether to try and post-process request exceptions (and throw them). */
|
|
8701
|
+
parseExceptions?: boolean;
|
|
8702
|
+
|
|
8703
|
+
customTimestampFn?: () => number;
|
|
8704
|
+
|
|
8705
|
+
/**
|
|
8706
|
+
* Enable keep alive for REST API requests (via axios).
|
|
8707
|
+
*/
|
|
8708
|
+
keepAlive?: boolean;
|
|
8709
|
+
|
|
8710
|
+
/**
|
|
8711
|
+
* When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
|
|
8712
|
+
* Only relevant if keepAlive is set to true.
|
|
8713
|
+
* Default: 1000 (defaults comes from https agent)
|
|
8714
|
+
*/
|
|
8715
|
+
keepAliveMsecs?: number;
|
|
8716
|
+
|
|
8717
|
+
/**
|
|
8718
|
+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
8719
|
+
*
|
|
8720
|
+
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
8721
|
+
*/
|
|
8722
|
+
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
|
|
8723
|
+
}
|
|
8418
8724
|
⋮----
|
|
8419
|
-
|
|
8420
|
-
to: Math.floor(Date.now() / 1000), // now (epoch seconds)
|
|
8725
|
+
/** Your API key */
|
|
8421
8726
|
⋮----
|
|
8422
|
-
|
|
8727
|
+
/** Your API secret */
|
|
8423
8728
|
⋮----
|
|
8424
|
-
|
|
8729
|
+
/**
|
|
8730
|
+
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
8731
|
+
*
|
|
8732
|
+
* Note: as of November 2025, only the DerivativesClient supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
8733
|
+
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
8734
|
+
*
|
|
8735
|
+
* Refer to the following for more information:
|
|
8736
|
+
* https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
8737
|
+
*/
|
|
8425
8738
|
⋮----
|
|
8426
|
-
|
|
8739
|
+
/**
|
|
8740
|
+
* Use access token instead of sign, if this is provided.
|
|
8741
|
+
* For guidance refer to: https://github.com/tiagosiebler/kucoin-api/issues/2
|
|
8742
|
+
*/
|
|
8427
8743
|
⋮----
|
|
8428
|
-
|
|
8429
|
-
// - trade: Trade price candles
|
|
8430
|
-
// - mark: Mark price candles
|
|
8431
|
-
// - spot: Spot price candles
|
|
8744
|
+
/** Default: false. If true, we'll throw errors if any params are undefined */
|
|
8432
8745
|
⋮----
|
|
8433
|
-
|
|
8746
|
+
/**
|
|
8747
|
+
* Optionally override API protocol + domain
|
|
8748
|
+
* e.g baseUrl: 'https://api.kraken.com'
|
|
8749
|
+
**/
|
|
8434
8750
|
⋮----
|
|
8435
|
-
|
|
8436
|
-
|
|
8437
|
-
|
|
8438
|
-
|
|
8439
|
-
|
|
8440
|
-
|
|
8441
|
-
|
|
8442
|
-
|
|
8443
|
-
|
|
8444
|
-
|
|
8445
|
-
|
|
8446
|
-
|
|
8447
|
-
|
|
8751
|
+
/** Default: true. whether to try and post-process request exceptions (and throw them). */
|
|
8752
|
+
⋮----
|
|
8753
|
+
/**
|
|
8754
|
+
* Enable keep alive for REST API requests (via axios).
|
|
8755
|
+
*/
|
|
8756
|
+
⋮----
|
|
8757
|
+
/**
|
|
8758
|
+
* When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
|
|
8759
|
+
* Only relevant if keepAlive is set to true.
|
|
8760
|
+
* Default: 1000 (defaults comes from https agent)
|
|
8761
|
+
*/
|
|
8762
|
+
⋮----
|
|
8763
|
+
/**
|
|
8764
|
+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
8765
|
+
*
|
|
8766
|
+
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
8767
|
+
*/
|
|
8768
|
+
⋮----
|
|
8769
|
+
export function serializeParams<T extends Record<string, any> | undefined = {}>(
|
|
8770
|
+
params: T,
|
|
8771
|
+
strict_validation: boolean | undefined,
|
|
8772
|
+
encodeValues: boolean,
|
|
8773
|
+
prefixWith: string,
|
|
8774
|
+
repeatArrayValuesAsKVPairs: boolean,
|
|
8775
|
+
): string
|
|
8776
|
+
⋮----
|
|
8777
|
+
// Only prefix if there's a value
|
|
8778
|
+
⋮----
|
|
8779
|
+
export function isEmptyObject(obj: any, acceptStringIfNotEmpty: boolean)
|
|
8780
|
+
⋮----
|
|
8781
|
+
export function getRestBaseUrl(
|
|
8782
|
+
restClientOptions: RestClientOptions,
|
|
8783
|
+
restClientType: RestClientType,
|
|
8784
|
+
): string
|
|
8448
8785
|
|
|
8449
8786
|
================
|
|
8450
|
-
File:
|
|
8787
|
+
File: .gitignore
|
|
8451
8788
|
================
|
|
8452
|
-
|
|
8789
|
+
!.gitkeep
|
|
8790
|
+
.DS_STORE
|
|
8791
|
+
*.log
|
|
8792
|
+
npm-debug.log*
|
|
8793
|
+
yarn-debug.log*
|
|
8794
|
+
yarn-error.log*
|
|
8795
|
+
lerna-debug.log*
|
|
8796
|
+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
|
|
8797
|
+
pids
|
|
8798
|
+
*.pid
|
|
8799
|
+
*.seed
|
|
8800
|
+
*.pid.lock
|
|
8801
|
+
node_modules/
|
|
8802
|
+
.npm
|
|
8803
|
+
.eslintcache
|
|
8804
|
+
.node_repl_history
|
|
8805
|
+
*.tgz
|
|
8806
|
+
.yarn-integrity
|
|
8807
|
+
.env
|
|
8808
|
+
.env.test
|
|
8809
|
+
.cache
|
|
8810
|
+
dist
|
|
8811
|
+
bundleReport.html
|
|
8812
|
+
.history/
|
|
8813
|
+
examples/ignoreme
|
|
8814
|
+
localtest.sh
|
|
8815
|
+
repomix.sh
|
|
8816
|
+
doc
|
|
8817
|
+
|
|
8818
|
+
================
|
|
8819
|
+
File: examples/Derivatives/Private/submitOrder.ts
|
|
8820
|
+
================
|
|
8821
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
8822
|
+
import { DerivativesClient } from '../../../src/index.js';
|
|
8453
8823
|
⋮----
|
|
8454
8824
|
// This example shows how to call Kraken API endpoint with either node.js,
|
|
8455
8825
|
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
8456
|
-
// for
|
|
8826
|
+
// for FUTURES ORDER MANAGEMENT
|
|
8457
8827
|
⋮----
|
|
8458
8828
|
/**
|
|
8459
|
-
* import {
|
|
8829
|
+
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
8460
8830
|
*/
|
|
8461
8831
|
⋮----
|
|
8462
8832
|
// initialise the client
|
|
8463
8833
|
/**
|
|
8464
8834
|
*
|
|
8465
|
-
* Kraken API uses API Key and
|
|
8835
|
+
* Kraken Futures API uses API Key and API Secret
|
|
8466
8836
|
*
|
|
8467
8837
|
* Example:
|
|
8468
8838
|
* {
|
|
8469
8839
|
* apiKey: 'your-api-key',
|
|
8470
|
-
* apiSecret: 'your-
|
|
8840
|
+
* apiSecret: 'your-api-secret',
|
|
8471
8841
|
* }
|
|
8472
8842
|
*
|
|
8473
|
-
* API Key Permissions Required:
|
|
8474
|
-
* - Funds permissions - Query
|
|
8475
|
-
* - Funds permissions - Deposit
|
|
8476
|
-
* - Funds permissions - Withdraw
|
|
8477
|
-
* - Data - Query ledger entries
|
|
8843
|
+
* API Key Permissions Required: Orders and trades - Create & modify orders
|
|
8478
8844
|
*
|
|
8479
8845
|
*/
|
|
8480
8846
|
⋮----
|
|
8481
|
-
async function
|
|
8847
|
+
async function submitLimitOrder()
|
|
8482
8848
|
⋮----
|
|
8483
|
-
//
|
|
8849
|
+
// Submit limit order for Futures
|
|
8484
8850
|
⋮----
|
|
8485
|
-
|
|
8851
|
+
symbol: 'PF_ETHUSD', // Perpetual ETH/USD
|
|
8486
8852
|
⋮----
|
|
8487
|
-
|
|
8853
|
+
size: 0.01, // Contract size
|
|
8488
8854
|
⋮----
|
|
8489
8855
|
// Response includes:
|
|
8490
|
-
// -
|
|
8856
|
+
// - status: placed, partiallyFilled, filled, or rejection reason
|
|
8857
|
+
// - order_id: Unique order identifier
|
|
8858
|
+
// - orderEvents: Array of order events (PLACE, EXECUTE, etc.)
|
|
8491
8859
|
⋮----
|
|
8492
|
-
async function
|
|
8860
|
+
async function submitMarketOrder()
|
|
8493
8861
|
⋮----
|
|
8494
|
-
//
|
|
8862
|
+
// Submit market order (IOC with 1% price protection)
|
|
8495
8863
|
⋮----
|
|
8496
|
-
//
|
|
8497
|
-
// - method: Name of deposit method
|
|
8498
|
-
// - limit: Maximum net amount that can be deposited
|
|
8499
|
-
// - fee: Fees that will be paid
|
|
8500
|
-
// - address-setup-fee: Whether method has setup fee
|
|
8501
|
-
// - gen-address: Whether new addresses can be generated
|
|
8502
|
-
// - minimum: Minimum net amount
|
|
8864
|
+
side: 'sell', // or "buy"
|
|
8503
8865
|
⋮----
|
|
8504
|
-
async function
|
|
8866
|
+
async function submitPostOnlyOrder()
|
|
8505
8867
|
⋮----
|
|
8506
|
-
//
|
|
8868
|
+
// Submit post-only order (maker-only)
|
|
8507
8869
|
⋮----
|
|
8508
|
-
|
|
8509
|
-
⋮----
|
|
8510
|
-
// Response includes:
|
|
8511
|
-
// - address: Deposit address
|
|
8512
|
-
// - expiretm: Expiration time or 0 if not expiring
|
|
8513
|
-
// - new: Whether address has ever been used
|
|
8514
|
-
// - tag: Contains tags/memos for XRP, STX, XLM, EOS
|
|
8515
|
-
⋮----
|
|
8516
|
-
async function generateNewDepositAddress()
|
|
8517
|
-
⋮----
|
|
8518
|
-
// Generate a new deposit address
|
|
8519
|
-
⋮----
|
|
8520
|
-
new: true, // Generate new address
|
|
8521
|
-
⋮----
|
|
8522
|
-
async function getWithdrawalMethods()
|
|
8523
|
-
⋮----
|
|
8524
|
-
// Get available withdrawal methods
|
|
8525
|
-
⋮----
|
|
8526
|
-
// Response includes:
|
|
8527
|
-
// - asset: Name of asset being withdrawn
|
|
8528
|
-
// - method: Name of withdrawal method
|
|
8529
|
-
// - network: Blockchain/network name
|
|
8530
|
-
// - minimum: Minimum net amount that can be withdrawn
|
|
8531
|
-
⋮----
|
|
8532
|
-
async function getWithdrawalAddresses()
|
|
8533
|
-
⋮----
|
|
8534
|
-
// Get withdrawal addresses
|
|
8535
|
-
⋮----
|
|
8536
|
-
verified: true, // Filter by verification status
|
|
8537
|
-
⋮----
|
|
8538
|
-
// Response includes:
|
|
8539
|
-
// - address: Withdrawal address
|
|
8540
|
-
// - asset: Asset name
|
|
8541
|
-
// - method: Withdrawal method
|
|
8542
|
-
// - key: Withdrawal key name
|
|
8543
|
-
// - tag: Tags/memos for XRP, STX, XLM, EOS
|
|
8544
|
-
// - verified: Verification status
|
|
8545
|
-
⋮----
|
|
8546
|
-
async function getWithdrawalAddressByKey()
|
|
8547
|
-
⋮----
|
|
8548
|
-
// Find withdrawal address by key name
|
|
8549
|
-
⋮----
|
|
8550
|
-
key: 'btc_2709', // Withdrawal key name
|
|
8551
|
-
⋮----
|
|
8552
|
-
async function getWithdrawalsStatus()
|
|
8553
|
-
⋮----
|
|
8554
|
-
// Get status of recent withdrawals
|
|
8555
|
-
⋮----
|
|
8556
|
-
// Status values:
|
|
8557
|
-
// - Initial: withdrawal just created
|
|
8558
|
-
// - Pending: withdrawal pending
|
|
8559
|
-
// - Settled: withdrawal settled
|
|
8560
|
-
// - Success: withdrawal successful
|
|
8561
|
-
// - Failure: withdrawal failed
|
|
8562
|
-
⋮----
|
|
8563
|
-
// Status properties (if available):
|
|
8564
|
-
// - cancel-pending: cancelation requested
|
|
8565
|
-
// - canceled: canceled
|
|
8566
|
-
// - cancel-denied: cancelation denied
|
|
8567
|
-
// - return: return transaction by Kraken
|
|
8568
|
-
// - onhold: on hold pending review
|
|
8569
|
-
⋮----
|
|
8570
|
-
async function getWithdrawalsStatusWithPagination()
|
|
8571
|
-
⋮----
|
|
8572
|
-
// Get withdrawals with pagination
|
|
8573
|
-
⋮----
|
|
8574
|
-
cursor: true, // Enable pagination
|
|
8575
|
-
limit: 10, // Results per page
|
|
8576
|
-
⋮----
|
|
8577
|
-
async function getDepositsStatus()
|
|
8578
|
-
⋮----
|
|
8579
|
-
// Get status of recent deposits
|
|
8580
|
-
⋮----
|
|
8581
|
-
// Similar status values as withdrawals
|
|
8582
|
-
⋮----
|
|
8583
|
-
async function cancelWithdrawal()
|
|
8584
|
-
⋮----
|
|
8585
|
-
// Cancel a recent withdrawal (if not yet processed)
|
|
8870
|
+
async function submitReduceOnlyOrder()
|
|
8586
8871
|
⋮----
|
|
8587
|
-
|
|
8872
|
+
// Submit reduce-only order (only closes position, won't open new)
|
|
8588
8873
|
⋮----
|
|
8589
|
-
//
|
|
8874
|
+
reduceOnly: true, // Only reduce existing position
|
|
8590
8875
|
⋮----
|
|
8591
|
-
async function
|
|
8876
|
+
async function batchOrderSubmit()
|
|
8592
8877
|
⋮----
|
|
8593
|
-
//
|
|
8878
|
+
// Send, edit, and cancel orders in a single batch request
|
|
8594
8879
|
⋮----
|
|
8595
|
-
//
|
|
8596
|
-
// - method: Withdrawal method
|
|
8597
|
-
// - limit: Maximum amount that can be withdrawn
|
|
8598
|
-
// - amount: Net amount to be withdrawn
|
|
8599
|
-
// - fee: Withdrawal fee
|
|
8880
|
+
// Send new order
|
|
8600
8881
|
⋮----
|
|
8601
|
-
|
|
8882
|
+
order_tag: 'order-1', // Tag to map responses
|
|
8602
8883
|
⋮----
|
|
8603
|
-
|
|
8884
|
+
// Send another order
|
|
8604
8885
|
⋮----
|
|
8605
|
-
|
|
8606
|
-
|
|
8886
|
+
// Response includes batchStatus array with results for each order
|
|
8887
|
+
// - status: placed, edited, cancelled, or rejection reason
|
|
8888
|
+
// - order_tag: Maps back to your request
|
|
8607
8889
|
⋮----
|
|
8608
8890
|
// Uncomment the function you want to test:
|
|
8609
8891
|
⋮----
|
|
8610
|
-
//
|
|
8611
|
-
//
|
|
8612
|
-
//
|
|
8613
|
-
//
|
|
8614
|
-
//
|
|
8615
|
-
// getWithdrawalAddresses();
|
|
8616
|
-
// getWithdrawalAddressByKey();
|
|
8617
|
-
// getWithdrawalsStatus();
|
|
8618
|
-
// getWithdrawalsStatusWithPagination();
|
|
8619
|
-
// getDepositsStatus();
|
|
8620
|
-
// cancelWithdrawal();
|
|
8621
|
-
// getWithdrawalInfo();
|
|
8622
|
-
// transferToFutures();
|
|
8623
|
-
// transferToSubaccount();
|
|
8892
|
+
// submitLimitOrder();
|
|
8893
|
+
// submitMarketOrder();
|
|
8894
|
+
// submitPostOnlyOrder();
|
|
8895
|
+
// submitReduceOnlyOrder();
|
|
8896
|
+
//batchOrderSubmit();
|
|
8624
8897
|
|
|
8625
8898
|
================
|
|
8626
8899
|
File: examples/Spot/WebSockets/privateWs.ts
|
|
@@ -8679,7 +8952,9 @@ async function start()
|
|
|
8679
8952
|
*
|
|
8680
8953
|
* So you do NOT need to manually fetch or provide the token when subscribing to private topics.
|
|
8681
8954
|
*
|
|
8682
|
-
* Do note that
|
|
8955
|
+
* Do note that:
|
|
8956
|
+
* - Most private topics use "spotPrivateV2" WsKey, which connects to "wss://ws-auth.kraken.com/v2"
|
|
8957
|
+
* - The level3 topic uses "spotL3V2" WsKey, which connects to "wss://ws-l3.kraken.com/v2" (dedicated L3 endpoint)
|
|
8683
8958
|
*/
|
|
8684
8959
|
⋮----
|
|
8685
8960
|
// Balances, requires auth: https://docs.kraken.com/api/docs/websocket-v2/executions
|
|
@@ -8701,6 +8976,7 @@ ratecounter: true, // default: false
|
|
|
8701
8976
|
// users: 'all',
|
|
8702
8977
|
⋮----
|
|
8703
8978
|
// Orders Level 3, requires auth: https://docs.kraken.com/api/docs/websocket-v2/level3
|
|
8979
|
+
// Note: level3 uses a dedicated endpoint (wss://ws-l3.kraken.com/v2), so use WS_KEY_MAP.spotL3V2
|
|
8704
8980
|
⋮----
|
|
8705
8981
|
// topic: 'level3',
|
|
8706
8982
|
⋮----
|
|
@@ -8709,279 +8985,16 @@ ratecounter: true, // default: false
|
|
|
8709
8985
|
// snapshot: true, // default: true
|
|
8710
8986
|
|
|
8711
8987
|
================
|
|
8712
|
-
File:
|
|
8713
|
-
================
|
|
8714
|
-
{
|
|
8715
|
-
"name": "@siebly/kraken-api",
|
|
8716
|
-
"version": "1.0.1",
|
|
8717
|
-
"description": "Complete & robust Node.js SDK for Kraken's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
|
|
8718
|
-
"scripts": {
|
|
8719
|
-
"clean": "rm -rf dist",
|
|
8720
|
-
"build": "npm run clean && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && bash ./postBuild.sh",
|
|
8721
|
-
"test": "jest --passWithNoTests",
|
|
8722
|
-
"lint": "eslint src"
|
|
8723
|
-
},
|
|
8724
|
-
"main": "dist/cjs/index.js",
|
|
8725
|
-
"module": "dist/mjs/index.js",
|
|
8726
|
-
"types": "dist/mjs/index.d.ts",
|
|
8727
|
-
"exports": {
|
|
8728
|
-
".": {
|
|
8729
|
-
"import": "./dist/mjs/index.js",
|
|
8730
|
-
"require": "./dist/cjs/index.js",
|
|
8731
|
-
"types": "./dist/mjs/index.d.ts"
|
|
8732
|
-
}
|
|
8733
|
-
},
|
|
8734
|
-
"type": "module",
|
|
8735
|
-
"files": [
|
|
8736
|
-
"dist/*",
|
|
8737
|
-
"llms.txt"
|
|
8738
|
-
],
|
|
8739
|
-
"author": "Siebly.io (https://github.com/sieblyio)",
|
|
8740
|
-
"contributors": [
|
|
8741
|
-
"Tiago Siebler (https://github.com/tiagosiebler)",
|
|
8742
|
-
"Jerko J (https://github.com/JJ-Cro)"
|
|
8743
|
-
],
|
|
8744
|
-
"dependencies": {
|
|
8745
|
-
"axios": "^1.10.0",
|
|
8746
|
-
"isomorphic-ws": "^5.0.0",
|
|
8747
|
-
"nanoid": "^3.3.11",
|
|
8748
|
-
"ws": "^8.18.3"
|
|
8749
|
-
},
|
|
8750
|
-
"devDependencies": {
|
|
8751
|
-
"@types/jest": "^29.5.12",
|
|
8752
|
-
"@types/node": "^22.11.6",
|
|
8753
|
-
"@types/ws": "^8.18.1",
|
|
8754
|
-
"@typescript-eslint/eslint-plugin": "^8.18.0",
|
|
8755
|
-
"@typescript-eslint/parser": "^8.18.0",
|
|
8756
|
-
"eslint": "^8.29.0",
|
|
8757
|
-
"eslint-config-prettier": "^9.1.0",
|
|
8758
|
-
"eslint-plugin-prettier": "^5.1.3",
|
|
8759
|
-
"eslint-plugin-require-extensions": "^0.1.3",
|
|
8760
|
-
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
8761
|
-
"jest": "^29.7.0",
|
|
8762
|
-
"prettier": "^3.3.3",
|
|
8763
|
-
"ts-jest": "^29.2.4",
|
|
8764
|
-
"ts-node": "^10.9.2",
|
|
8765
|
-
"typescript": "^5.7.3"
|
|
8766
|
-
},
|
|
8767
|
-
"keywords": [
|
|
8768
|
-
"kraken",
|
|
8769
|
-
"kraken api",
|
|
8770
|
-
"kraken nodejs",
|
|
8771
|
-
"kraken javascript",
|
|
8772
|
-
"kraken typescript",
|
|
8773
|
-
"kraken websocket api",
|
|
8774
|
-
"kraken websocket api javascript",
|
|
8775
|
-
"algo trading",
|
|
8776
|
-
"api",
|
|
8777
|
-
"websocket",
|
|
8778
|
-
"rest",
|
|
8779
|
-
"rest api",
|
|
8780
|
-
"usdt",
|
|
8781
|
-
"trading bots",
|
|
8782
|
-
"nodejs",
|
|
8783
|
-
"node",
|
|
8784
|
-
"trading",
|
|
8785
|
-
"cryptocurrency",
|
|
8786
|
-
"bitcoin",
|
|
8787
|
-
"best"
|
|
8788
|
-
],
|
|
8789
|
-
"funding": {
|
|
8790
|
-
"type": "individual",
|
|
8791
|
-
"url": "https://github.com/sponsors/tiagosiebler"
|
|
8792
|
-
},
|
|
8793
|
-
"license": "MIT",
|
|
8794
|
-
"repository": {
|
|
8795
|
-
"type": "git",
|
|
8796
|
-
"url": "https://github.com/sieblyio/kraken-api"
|
|
8797
|
-
},
|
|
8798
|
-
"bugs": {
|
|
8799
|
-
"url": "https://github.com/sieblyio/kraken-api/issues"
|
|
8800
|
-
},
|
|
8801
|
-
"homepage": "https://github.com/sieblyio/kraken-api#readme"
|
|
8802
|
-
}
|
|
8803
|
-
|
|
8804
|
-
================
|
|
8805
|
-
File: examples/Derivatives/Private/account.ts
|
|
8988
|
+
File: src/types/websockets/ws-general.ts
|
|
8806
8989
|
================
|
|
8807
|
-
import {
|
|
8808
|
-
|
|
8809
|
-
|
|
8810
|
-
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
8811
|
-
// for FUTURES ACCOUNT INFORMATION
|
|
8812
|
-
⋮----
|
|
8813
|
-
/**
|
|
8814
|
-
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
8815
|
-
*/
|
|
8816
|
-
⋮----
|
|
8817
|
-
// initialise the client
|
|
8818
|
-
/**
|
|
8819
|
-
*
|
|
8820
|
-
* Kraken Futures API uses API Key and API Secret
|
|
8821
|
-
*
|
|
8822
|
-
* Example:
|
|
8823
|
-
* {
|
|
8824
|
-
* apiKey: 'your-api-key',
|
|
8825
|
-
* apiSecret: 'your-api-secret',
|
|
8826
|
-
* }
|
|
8827
|
-
*
|
|
8828
|
-
* API Key Permissions Required:
|
|
8829
|
-
* - Read access for account information
|
|
8830
|
-
* - Withdrawal permissions for transfers
|
|
8831
|
-
*
|
|
8832
|
-
*/
|
|
8833
|
-
⋮----
|
|
8834
|
-
async function getWallets()
|
|
8990
|
+
import { AxiosRequestConfig } from 'axios';
|
|
8991
|
+
import type { ClientRequestArgs } from 'http';
|
|
8992
|
+
import WebSocket from 'isomorphic-ws';
|
|
8835
8993
|
⋮----
|
|
8836
|
-
|
|
8994
|
+
import { RestClientOptions } from '../../lib/requestUtils.js';
|
|
8837
8995
|
⋮----
|
|
8838
|
-
|
|
8839
|
-
|
|
8840
|
-
// - flex: Multi-collateral wallet with margin info
|
|
8841
|
-
// - Available margin, portfolio value, PnL
|
|
8842
|
-
// - Initial/maintenance margin requirements
|
|
8843
|
-
// For each margin account:
|
|
8844
|
-
// - balances, auxiliary (pv, pnl, af, funding)
|
|
8845
|
-
// - marginRequirements (im, mm, lt, tt)
|
|
8846
|
-
// - triggerEstimates
|
|
8847
|
-
⋮----
|
|
8848
|
-
async function getOpenPositions()
|
|
8849
|
-
⋮----
|
|
8850
|
-
// Get all open Futures positions
|
|
8851
|
-
⋮----
|
|
8852
|
-
// Response includes for each position:
|
|
8853
|
-
// - symbol: Futures symbol
|
|
8854
|
-
// - side: long or short
|
|
8855
|
-
// - size: Position size
|
|
8856
|
-
// - price: Average entry price
|
|
8857
|
-
// - fillTime: When position was opened
|
|
8858
|
-
// - unrealizedFunding: Unrealized funding
|
|
8859
|
-
⋮----
|
|
8860
|
-
async function getFills()
|
|
8861
|
-
⋮----
|
|
8862
|
-
// Get filled orders history (last 100)
|
|
8863
|
-
⋮----
|
|
8864
|
-
// Response includes for each fill:
|
|
8865
|
-
// - fill_id: Unique fill identifier
|
|
8866
|
-
// - order_id: Associated order ID
|
|
8867
|
-
// - symbol: Futures symbol
|
|
8868
|
-
// - side: buy or sell
|
|
8869
|
-
// - size: Fill size
|
|
8870
|
-
// - price: Fill price
|
|
8871
|
-
// - fillTime: Execution time
|
|
8872
|
-
// - fillType: maker, taker, liquidation, etc.
|
|
8873
|
-
⋮----
|
|
8874
|
-
async function getFillsBeforeTime()
|
|
8875
|
-
⋮----
|
|
8876
|
-
// Get fills before specific time
|
|
8877
|
-
⋮----
|
|
8878
|
-
lastFillTime: new Date(Date.now() - 86400000).toISOString(), // 24h ago
|
|
8879
|
-
⋮----
|
|
8880
|
-
// Returns 100 fills before specified time
|
|
8881
|
-
⋮----
|
|
8882
|
-
async function initiateWalletTransfer()
|
|
8883
|
-
⋮----
|
|
8884
|
-
// Transfer between margin accounts or to/from cash account
|
|
8885
|
-
⋮----
|
|
8886
|
-
// Transfers funds between accounts instantly
|
|
8887
|
-
⋮----
|
|
8888
|
-
async function initiateWithdrawalToSpot()
|
|
8889
|
-
⋮----
|
|
8890
|
-
// Withdraw from Futures to Spot wallet
|
|
8891
|
-
⋮----
|
|
8892
|
-
sourceWallet: 'cash', // Default is cash wallet
|
|
8893
|
-
⋮----
|
|
8894
|
-
// Response includes:
|
|
8895
|
-
// - uid: Withdrawal reference ID
|
|
8896
|
-
⋮----
|
|
8897
|
-
async function getOrderEvents()
|
|
8898
|
-
⋮----
|
|
8899
|
-
// Get order history events
|
|
8900
|
-
⋮----
|
|
8901
|
-
sort: 'desc', // desc = newest first
|
|
8902
|
-
opened: true, // Include opened orders
|
|
8903
|
-
closed: true, // Include closed orders
|
|
8904
|
-
⋮----
|
|
8905
|
-
// Response includes:
|
|
8906
|
-
// - elements: Array of order events
|
|
8907
|
-
// - Order placed, cancelled, rejected, executed events
|
|
8908
|
-
// - continuationToken: For pagination
|
|
8909
|
-
⋮----
|
|
8910
|
-
async function getOrderEventsBySymbol()
|
|
8911
|
-
⋮----
|
|
8912
|
-
// Filter order events by symbol
|
|
8913
|
-
⋮----
|
|
8914
|
-
async function getExecutionEvents()
|
|
8915
|
-
⋮----
|
|
8916
|
-
// Get execution/trade history
|
|
8917
|
-
⋮----
|
|
8918
|
-
// Response includes for each execution:
|
|
8919
|
-
// - execution: Fill details (price, quantity, timestamp)
|
|
8920
|
-
// - order: Associated order details
|
|
8921
|
-
// - fee: Fee paid
|
|
8922
|
-
// - positionSize: Position size after execution
|
|
8923
|
-
⋮----
|
|
8924
|
-
async function getExecutionEventsBySymbol()
|
|
8925
|
-
⋮----
|
|
8926
|
-
// Filter executions by symbol
|
|
8927
|
-
⋮----
|
|
8928
|
-
async function getAccountLog()
|
|
8929
|
-
⋮----
|
|
8930
|
-
// Get account log (all account activities)
|
|
8931
|
-
⋮----
|
|
8932
|
-
// Log includes:
|
|
8933
|
-
// - futures trade, liquidation, funding rate change
|
|
8934
|
-
// - conversions, transfers, settlements
|
|
8935
|
-
// - interest payments, fees
|
|
8936
|
-
⋮----
|
|
8937
|
-
async function getAccountLogFiltered()
|
|
8938
|
-
⋮----
|
|
8939
|
-
// Filter account log by info types
|
|
8940
|
-
⋮----
|
|
8941
|
-
async function enableFuturesSubTrading()
|
|
8942
|
-
⋮----
|
|
8943
|
-
async function getSubaccountTradingStatus()
|
|
8944
|
-
⋮----
|
|
8945
|
-
async function getSubaccounts()
|
|
8946
|
-
// Uncomment the function you want to test:
|
|
8947
|
-
⋮----
|
|
8948
|
-
// getWallets();
|
|
8949
|
-
// getOpenPositions();
|
|
8950
|
-
// getFills();
|
|
8951
|
-
// getFillsBeforeTime();
|
|
8952
|
-
// initiateWalletTransfer();
|
|
8953
|
-
// initiateWithdrawalToSpot();
|
|
8954
|
-
// getOrderEvents();
|
|
8955
|
-
// getOrderEventsBySymbol();
|
|
8956
|
-
// getExecutionEvents();
|
|
8957
|
-
// getExecutionEventsBySymbol();
|
|
8958
|
-
// getAccountLog();
|
|
8959
|
-
// getAccountLogFiltered();
|
|
8960
|
-
// enableFuturesSubTrading();
|
|
8961
|
-
// getSubaccountTradingStatus();
|
|
8962
|
-
// getSubaccounts();
|
|
8963
|
-
|
|
8964
|
-
================
|
|
8965
|
-
File: src/lib/requestUtils.ts
|
|
8966
|
-
================
|
|
8967
|
-
/**
|
|
8968
|
-
* Used to switch how authentication/requests work under the hood
|
|
8969
|
-
*/
|
|
8970
|
-
⋮----
|
|
8971
|
-
/** Spot */
|
|
8972
|
-
⋮----
|
|
8973
|
-
/** Futures */
|
|
8974
|
-
⋮----
|
|
8975
|
-
/** Futures Demo */
|
|
8976
|
-
⋮----
|
|
8977
|
-
/** Institutional */
|
|
8978
|
-
⋮----
|
|
8979
|
-
/** Partner */
|
|
8980
|
-
⋮----
|
|
8981
|
-
export type RestClientType =
|
|
8982
|
-
(typeof REST_CLIENT_TYPE_ENUM)[keyof typeof REST_CLIENT_TYPE_ENUM];
|
|
8983
|
-
⋮----
|
|
8984
|
-
export interface RestClientOptions {
|
|
8996
|
+
/** General configuration for the WebsocketClient */
|
|
8997
|
+
export interface WSClientConfigurableOptions {
|
|
8985
8998
|
/** Your API key */
|
|
8986
8999
|
apiKey?: string;
|
|
8987
9000
|
|
|
@@ -8991,7 +9004,7 @@ export interface RestClientOptions {
|
|
|
8991
9004
|
/**
|
|
8992
9005
|
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
8993
9006
|
*
|
|
8994
|
-
* Note: as of November 2025, only the
|
|
9007
|
+
* Note: as of November 2025, only the derivatives environment supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
8995
9008
|
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
8996
9009
|
*
|
|
8997
9010
|
* Refer to the following for more information:
|
|
@@ -8999,37 +9012,27 @@ export interface RestClientOptions {
|
|
|
8999
9012
|
*/
|
|
9000
9013
|
testnet?: boolean;
|
|
9001
9014
|
|
|
9002
|
-
/**
|
|
9003
|
-
|
|
9004
|
-
* For guidance refer to: https://github.com/tiagosiebler/kucoin-api/issues/2
|
|
9005
|
-
*/
|
|
9006
|
-
apiAccessToken?: string;
|
|
9015
|
+
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
9016
|
+
recvWindow?: number;
|
|
9007
9017
|
|
|
9008
|
-
/**
|
|
9009
|
-
|
|
9018
|
+
/** How often to check if the connection is alive */
|
|
9019
|
+
pingInterval?: number;
|
|
9010
9020
|
|
|
9011
|
-
/**
|
|
9012
|
-
|
|
9013
|
-
* e.g baseUrl: 'https://api.kraken.com'
|
|
9014
|
-
**/
|
|
9015
|
-
baseUrl?: string;
|
|
9021
|
+
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
9022
|
+
pongTimeout?: number;
|
|
9016
9023
|
|
|
9017
|
-
/**
|
|
9018
|
-
|
|
9024
|
+
/** Delay in milliseconds before respawning the connection */
|
|
9025
|
+
reconnectTimeout?: number;
|
|
9019
9026
|
|
|
9020
|
-
|
|
9027
|
+
restOptions?: RestClientOptions;
|
|
9028
|
+
requestOptions?: AxiosRequestConfig;
|
|
9021
9029
|
|
|
9022
|
-
|
|
9023
|
-
|
|
9024
|
-
|
|
9025
|
-
|
|
9030
|
+
wsOptions?: {
|
|
9031
|
+
protocols?: string[];
|
|
9032
|
+
agent?: any;
|
|
9033
|
+
} & Partial<WebSocket.ClientOptions | ClientRequestArgs>;
|
|
9026
9034
|
|
|
9027
|
-
|
|
9028
|
-
* When using HTTP KeepAlive, how often to send TCP KeepAlive packets over sockets being kept alive. Default = 1000.
|
|
9029
|
-
* Only relevant if keepAlive is set to true.
|
|
9030
|
-
* Default: 1000 (defaults comes from https agent)
|
|
9031
|
-
*/
|
|
9032
|
-
keepAliveMsecs?: number;
|
|
9035
|
+
wsUrl?: string;
|
|
9033
9036
|
|
|
9034
9037
|
/**
|
|
9035
9038
|
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
@@ -9037,6 +9040,11 @@ export interface RestClientOptions {
|
|
|
9037
9040
|
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
9038
9041
|
*/
|
|
9039
9042
|
customSignMessageFn?: (message: string, secret: string) => Promise<string>;
|
|
9043
|
+
|
|
9044
|
+
/**
|
|
9045
|
+
* If you authenticated the WS API before, automatically try to re-authenticate the WS API if you're disconnected/reconnected for any reason.
|
|
9046
|
+
*/
|
|
9047
|
+
reauthWSAPIOnReconnect?: boolean;
|
|
9040
9048
|
}
|
|
9041
9049
|
⋮----
|
|
9042
9050
|
/** Your API key */
|
|
@@ -9046,59 +9054,89 @@ export interface RestClientOptions {
|
|
|
9046
9054
|
/**
|
|
9047
9055
|
* Set to `true` to connect to testnet (Kraken's demo environment). The live environment is used by default.
|
|
9048
9056
|
*
|
|
9049
|
-
* Note: as of November 2025, only the
|
|
9057
|
+
* Note: as of November 2025, only the derivatives environment supports testnet connections. Kraken refer to this as the "Demo" environment, but it is effectively a testnet.
|
|
9050
9058
|
* This is a place to test your API integration. It is not a good place to test strategy performance, as the liquidity and orderbook dynamics are very different to the live environment.
|
|
9051
9059
|
*
|
|
9052
9060
|
* Refer to the following for more information:
|
|
9053
9061
|
* https://github.com/tiagosiebler/awesome-crypto-examples/wiki/CEX-Testnets
|
|
9054
9062
|
*/
|
|
9055
9063
|
⋮----
|
|
9056
|
-
/**
|
|
9057
|
-
* Use access token instead of sign, if this is provided.
|
|
9058
|
-
* For guidance refer to: https://github.com/tiagosiebler/kucoin-api/issues/2
|
|
9059
|
-
*/
|
|
9064
|
+
/** Define a recv window when preparing a private websocket signature. This is in milliseconds, so 5000 == 5 seconds */
|
|
9060
9065
|
⋮----
|
|
9061
|
-
/**
|
|
9066
|
+
/** How often to check if the connection is alive */
|
|
9062
9067
|
⋮----
|
|
9063
|
-
/**
|
|
9064
|
-
* Optionally override API protocol + domain
|
|
9065
|
-
* e.g baseUrl: 'https://api.kraken.com'
|
|
9066
|
-
**/
|
|
9068
|
+
/** How long to wait for a pong (heartbeat reply) before assuming the connection is dead */
|
|
9067
9069
|
⋮----
|
|
9068
|
-
/**
|
|
9070
|
+
/** Delay in milliseconds before respawning the connection */
|
|
9069
9071
|
⋮----
|
|
9070
9072
|
/**
|
|
9071
|
-
*
|
|
9073
|
+
* Allows you to provide a custom "signMessage" function, e.g. to use node's much faster createHmac method
|
|
9074
|
+
*
|
|
9075
|
+
* Look in the examples folder for a demonstration on using node's createHmac instead.
|
|
9072
9076
|
*/
|
|
9073
9077
|
⋮----
|
|
9074
9078
|
/**
|
|
9075
|
-
*
|
|
9076
|
-
* Only relevant if keepAlive is set to true.
|
|
9077
|
-
* Default: 1000 (defaults comes from https agent)
|
|
9079
|
+
* If you authenticated the WS API before, automatically try to re-authenticate the WS API if you're disconnected/reconnected for any reason.
|
|
9078
9080
|
*/
|
|
9079
9081
|
⋮----
|
|
9080
9082
|
/**
|
|
9081
|
-
|
|
9082
|
-
|
|
9083
|
-
|
|
9083
|
+
* WS configuration that's always defined, regardless of user configuration
|
|
9084
|
+
* (usually comes from defaults if there's no user-provided values)
|
|
9085
|
+
*/
|
|
9086
|
+
export interface WebsocketClientOptions extends WSClientConfigurableOptions {
|
|
9087
|
+
pingInterval: number;
|
|
9088
|
+
pongTimeout: number;
|
|
9089
|
+
reconnectTimeout: number;
|
|
9090
|
+
recvWindow: number;
|
|
9091
|
+
|
|
9092
|
+
/**
|
|
9093
|
+
* If true, require a "receipt" that the connection is ready for use (e.g. a specific event type)
|
|
9094
|
+
*/
|
|
9095
|
+
requireConnectionReadyConfirmation: boolean;
|
|
9096
|
+
authPrivateConnectionsOnConnect: boolean;
|
|
9097
|
+
authPrivateRequests: boolean;
|
|
9098
|
+
reauthWSAPIOnReconnect: boolean;
|
|
9099
|
+
|
|
9100
|
+
/**
|
|
9101
|
+
* Whether to use native WebSocket ping/pong frames for heartbeats
|
|
9084
9102
|
*/
|
|
9103
|
+
useNativeHeartbeats: boolean;
|
|
9104
|
+
}
|
|
9085
9105
|
⋮----
|
|
9086
|
-
|
|
9087
|
-
|
|
9088
|
-
|
|
9089
|
-
encodeValues: boolean,
|
|
9090
|
-
prefixWith: string,
|
|
9091
|
-
repeatArrayValuesAsKVPairs: boolean,
|
|
9092
|
-
): string
|
|
9106
|
+
/**
|
|
9107
|
+
* If true, require a "receipt" that the connection is ready for use (e.g. a specific event type)
|
|
9108
|
+
*/
|
|
9093
9109
|
⋮----
|
|
9094
|
-
|
|
9110
|
+
/**
|
|
9111
|
+
* Whether to use native WebSocket ping/pong frames for heartbeats
|
|
9112
|
+
*/
|
|
9095
9113
|
⋮----
|
|
9096
|
-
export
|
|
9114
|
+
export type WsMarket = 'spot' | 'futures';
|
|
9097
9115
|
⋮----
|
|
9098
|
-
export function
|
|
9099
|
-
|
|
9100
|
-
|
|
9101
|
-
|
|
9116
|
+
export type WsEventInternalSrc = 'event' | 'function' | 'frame';
|
|
9117
|
+
|
|
9118
|
+
================
|
|
9119
|
+
File: src/types/websockets/ws-subscriptions.ts
|
|
9120
|
+
================
|
|
9121
|
+
] as const; // Note: Admin topics (Status, Heartbeat & Ping are automatically used internally and can't be subscribed to manually).
|
|
9122
|
+
⋮----
|
|
9123
|
+
export type WSSpotPublicTopic = (typeof WS_SPOT_PUBLIC_TOPICS)[number];
|
|
9124
|
+
⋮----
|
|
9125
|
+
export type WSSpotPrivateTopic = (typeof WS_SPOT_PRIVATE_TOPICS)[number];
|
|
9126
|
+
⋮----
|
|
9127
|
+
export type WSSpotTopic = WSSpotPublicTopic | WSSpotPrivateTopic;
|
|
9128
|
+
⋮----
|
|
9129
|
+
export type WSDerivativesPublicTopic =
|
|
9130
|
+
(typeof WS_DERIVATIVES_PUBLIC_TOPICS)[number];
|
|
9131
|
+
⋮----
|
|
9132
|
+
export type WSDerivativesPrivateTopic =
|
|
9133
|
+
(typeof WS_DERIVATIVES_PRIVATE_TOPICS)[number];
|
|
9134
|
+
⋮----
|
|
9135
|
+
export type WSDerivativesTopic =
|
|
9136
|
+
| WSDerivativesPublicTopic
|
|
9137
|
+
| WSDerivativesPrivateTopic;
|
|
9138
|
+
⋮----
|
|
9139
|
+
export type WSTopic = WSSpotTopic | WSDerivativesTopic;
|
|
9102
9140
|
|
|
9103
9141
|
================
|
|
9104
9142
|
File: src/WebsocketAPIClient.ts
|
|
@@ -9416,965 +9454,235 @@ export interface WSAPITopicResponseMap {
|
|
|
9416
9454
|
}
|
|
9417
9455
|
|
|
9418
9456
|
================
|
|
9419
|
-
File: src/
|
|
9457
|
+
File: src/lib/websocket/websocket-util.ts
|
|
9420
9458
|
================
|
|
9421
|
-
|
|
9459
|
+
import WebSocket from 'isomorphic-ws';
|
|
9422
9460
|
⋮----
|
|
9423
|
-
|
|
9461
|
+
import { WSAPIRequestOperationKrakenSpot } from '../../types/websockets/ws-api.js';
|
|
9462
|
+
import { WSTopic } from '../../types/websockets/ws-subscriptions.js';
|
|
9424
9463
|
⋮----
|
|
9425
|
-
|
|
9464
|
+
/** Should be one WS key per unique URL */
|
|
9426
9465
|
⋮----
|
|
9427
|
-
|
|
9466
|
+
/**
|
|
9467
|
+
* Public WebSocket subscriptions for Kraken Spot products, via the V2 API
|
|
9468
|
+
*
|
|
9469
|
+
* - Ref: https://docs.kraken.com/api/docs/guides/spot-ws-intro
|
|
9470
|
+
* - Channels: https://docs.kraken.com/api/docs/websocket-v2/add_order
|
|
9471
|
+
*
|
|
9472
|
+
* Note: Use spotPrivateV2 for private channels (requires API keys).
|
|
9473
|
+
*/
|
|
9428
9474
|
⋮----
|
|
9429
|
-
|
|
9430
|
-
|
|
9475
|
+
/**
|
|
9476
|
+
* Public WebSocket subscriptions for Kraken Derivatives products, via the V1 API:
|
|
9477
|
+
*
|
|
9478
|
+
* - Ref: https://docs.kraken.com/api/docs/guides/futures-websockets
|
|
9479
|
+
* - Channels: https://docs.kraken.com/api/docs/futures-api/websocket/open_orders
|
|
9480
|
+
*
|
|
9481
|
+
* Note: While both Public and Private channels use the same WebSocket URL, we will actually maintain separate connections for easier management. Private channels require authentication and the connection is authenticated automatically.
|
|
9482
|
+
*/
|
|
9431
9483
|
⋮----
|
|
9432
|
-
|
|
9433
|
-
|
|
9484
|
+
/** This is used to differentiate between each of the available websocket streams */
|
|
9485
|
+
export type WsKey = (typeof WS_KEY_MAP)[keyof typeof WS_KEY_MAP];
|
|
9434
9486
|
⋮----
|
|
9435
|
-
export type
|
|
9436
|
-
| WSDerivativesPublicTopic
|
|
9437
|
-
| WSDerivativesPrivateTopic;
|
|
9487
|
+
export type WSOperation = 'subscribe' | 'unsubscribe';
|
|
9438
9488
|
⋮----
|
|
9439
|
-
|
|
9489
|
+
/**
|
|
9490
|
+
* Normalised internal format for a request (subscribe/unsubscribe/etc) on a topic, with optional parameters.
|
|
9491
|
+
*
|
|
9492
|
+
* - Topic: the topic this event is for
|
|
9493
|
+
* - Payload: the parameters to include, optional. E.g. auth requires key + sign. Some topics allow configurable parameters.
|
|
9494
|
+
*/
|
|
9495
|
+
export interface WSTopicRequest<
|
|
9496
|
+
TWSTopic extends WSTopic = WSTopic,
|
|
9497
|
+
TWSPayload = any,
|
|
9498
|
+
> {
|
|
9499
|
+
topic: TWSTopic;
|
|
9500
|
+
payload?: TWSPayload;
|
|
9501
|
+
}
|
|
9502
|
+
⋮----
|
|
9503
|
+
/**
|
|
9504
|
+
* Conveniently allow users to request a topic either as string topics or objects (containing string topic + params)
|
|
9505
|
+
*/
|
|
9506
|
+
export type WSTopicRequestOrStringTopic<
|
|
9507
|
+
TWSTopic extends WSTopic,
|
|
9508
|
+
TWSPayload = any,
|
|
9509
|
+
> = WSTopicRequest<TWSTopic, TWSPayload> | string;
|
|
9510
|
+
⋮----
|
|
9511
|
+
export interface WSRequestOperationKraken<
|
|
9512
|
+
TWSTopic extends string,
|
|
9513
|
+
TWSParams extends object = any,
|
|
9514
|
+
> {
|
|
9515
|
+
// spot only
|
|
9516
|
+
method?: WSOperation;
|
|
9517
|
+
// futures only
|
|
9518
|
+
event?: WSOperation;
|
|
9519
|
+
params:
|
|
9520
|
+
| {
|
|
9521
|
+
channel: (TWSTopic | string | number)[];
|
|
9522
|
+
symbol?: string[];
|
|
9523
|
+
event_trigger?: string;
|
|
9524
|
+
snapshot?: boolean;
|
|
9525
|
+
}
|
|
9526
|
+
| TWSParams;
|
|
9527
|
+
req_id: number;
|
|
9528
|
+
/**
|
|
9529
|
+
* The following are needed for futures/derivatives requests
|
|
9530
|
+
*/
|
|
9531
|
+
feed?: TWSTopic;
|
|
9532
|
+
api_key?: string;
|
|
9533
|
+
original_challenge?: string;
|
|
9534
|
+
signed_challenge?: string;
|
|
9535
|
+
}
|
|
9536
|
+
⋮----
|
|
9537
|
+
// spot only
|
|
9538
|
+
⋮----
|
|
9539
|
+
// futures only
|
|
9540
|
+
⋮----
|
|
9541
|
+
/**
|
|
9542
|
+
* The following are needed for futures/derivatives requests
|
|
9543
|
+
*/
|
|
9544
|
+
⋮----
|
|
9545
|
+
/**
|
|
9546
|
+
* #305: ws.terminate() is undefined in browsers.
|
|
9547
|
+
* This only works in node.js, not in browsers.
|
|
9548
|
+
* Does nothing if `ws` is undefined. Does nothing in browsers.
|
|
9549
|
+
*/
|
|
9550
|
+
export function safeTerminateWs(
|
|
9551
|
+
ws?: WebSocket | any,
|
|
9552
|
+
fallbackToClose?: boolean,
|
|
9553
|
+
): boolean
|
|
9554
|
+
⋮----
|
|
9555
|
+
/**
|
|
9556
|
+
* WS API promises are stored using a primary key. This key is constructed using
|
|
9557
|
+
* properties found in every request & reply.
|
|
9558
|
+
*
|
|
9559
|
+
* The counterpart to this is in resolveEmittableEvents
|
|
9560
|
+
*/
|
|
9561
|
+
export function getPromiseRefForWSAPIRequest(
|
|
9562
|
+
wsKey: WsKey,
|
|
9563
|
+
requestEvent: WSAPIRequestOperationKrakenSpot,
|
|
9564
|
+
): string
|
|
9440
9565
|
|
|
9441
9566
|
================
|
|
9442
|
-
File:
|
|
9567
|
+
File: src/lib/BaseWSClient.ts
|
|
9443
9568
|
================
|
|
9444
|
-
|
|
9445
|
-
|
|
9446
|
-
|
|
9447
|
-
|
|
9448
|
-
|
|
9449
|
-
|
|
9450
|
-
|
|
9451
|
-
|
|
9452
|
-
|
|
9453
|
-
|
|
9454
|
-
|
|
9455
|
-
|
|
9456
|
-
|
|
9457
|
-
|
|
9458
|
-
|
|
9459
|
-
|
|
9460
|
-
|
|
9569
|
+
import { EventEmitter } from 'events';
|
|
9570
|
+
import WebSocket from 'isomorphic-ws';
|
|
9571
|
+
⋮----
|
|
9572
|
+
import {
|
|
9573
|
+
isMessageEvent,
|
|
9574
|
+
MessageEventLike,
|
|
9575
|
+
} from '../types/websockets/ws-events.js';
|
|
9576
|
+
import {
|
|
9577
|
+
WebsocketClientOptions,
|
|
9578
|
+
WSClientConfigurableOptions,
|
|
9579
|
+
WsEventInternalSrc,
|
|
9580
|
+
} from '../types/websockets/ws-general.js';
|
|
9581
|
+
import { WSTopic } from '../types/websockets/ws-subscriptions.js';
|
|
9582
|
+
import { checkWebCryptoAPISupported } from './webCryptoAPI.js';
|
|
9583
|
+
import { DefaultLogger } from './websocket/logger.js';
|
|
9584
|
+
import {
|
|
9585
|
+
safeTerminateWs,
|
|
9586
|
+
WSOperation,
|
|
9587
|
+
WSTopicRequest,
|
|
9588
|
+
WSTopicRequestOrStringTopic,
|
|
9589
|
+
} from './websocket/websocket-util.js';
|
|
9590
|
+
import { WsStore } from './websocket/WsStore.js';
|
|
9591
|
+
import {
|
|
9592
|
+
WSConnectedResult,
|
|
9593
|
+
WsConnectionStateEnum,
|
|
9594
|
+
} from './websocket/WsStore.types.js';
|
|
9595
|
+
⋮----
|
|
9596
|
+
type UseTheExceptionEventInstead = never;
|
|
9597
|
+
⋮----
|
|
9598
|
+
interface WSClientEventMap<WsKey extends string> {
|
|
9599
|
+
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
|
9600
|
+
open: (evt: {
|
|
9601
|
+
wsKey: WsKey;
|
|
9602
|
+
event: any;
|
|
9603
|
+
wsUrl: string;
|
|
9604
|
+
ws: WebSocket;
|
|
9605
|
+
}) => void;
|
|
9461
9606
|
|
|
9462
|
-
|
|
9607
|
+
/** Reconnecting a dropped connection */
|
|
9608
|
+
reconnecting: (evt: { wsKey: WsKey; event: any }) => void;
|
|
9463
9609
|
|
|
9464
|
-
|
|
9610
|
+
/** Successfully reconnected a connection that dropped */
|
|
9611
|
+
reconnected: (evt: {
|
|
9612
|
+
wsKey: WsKey;
|
|
9613
|
+
event: any;
|
|
9614
|
+
wsUrl: string;
|
|
9615
|
+
ws: WebSocket;
|
|
9616
|
+
}) => void;
|
|
9465
9617
|
|
|
9466
|
-
|
|
9467
|
-
|
|
9468
|
-
- Dedicated REST clients for Spot, Derivatives (Futures), Institutional, and Partner operations
|
|
9469
|
-
- Unified WebSocket client for all markets
|
|
9470
|
-
- Complete TypeScript support (with type declarations for most API requests & responses).
|
|
9471
|
-
- Strongly typed requests and responses.
|
|
9472
|
-
- Automated end-to-end tests ensuring reliability.
|
|
9473
|
-
- Actively maintained with a modern, promise-driven interface.
|
|
9474
|
-
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.
|
|
9475
|
-
- Event driven messaging.
|
|
9476
|
-
- Smart WebSocket persistence with automatic reconnection handling.
|
|
9477
|
-
- Emit `reconnected` event when dropped connection is restored.
|
|
9478
|
-
- Support for both public and private WebSocket streams.
|
|
9479
|
-
- Browser-friendly HMAC signature mechanism.
|
|
9480
|
-
- Automatically supports both ESM and CJS projects.
|
|
9481
|
-
- Heavy automated end-to-end testing with real API calls.
|
|
9482
|
-
- Proxy support via axios integration.
|
|
9483
|
-
- Active community support & collaboration in telegram: [Node.js Algo Traders](https://t.me/nodetraders).
|
|
9618
|
+
/** Connection closed */
|
|
9619
|
+
close: (evt: { wsKey: WsKey; event: any }) => void;
|
|
9484
9620
|
|
|
9485
|
-
|
|
9621
|
+
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
|
9622
|
+
response: (response: any & { wsKey: WsKey }) => void;
|
|
9486
9623
|
|
|
9487
|
-
|
|
9488
|
-
|
|
9489
|
-
- [Issues & Discussion](#issues--discussion)
|
|
9490
|
-
- [Related Projects](#related-projects)
|
|
9491
|
-
- [Documentation](#documentation)
|
|
9492
|
-
- [Structure](#structure)
|
|
9493
|
-
- [Usage](#usage)
|
|
9494
|
-
- [REST API Clients](#rest-api)
|
|
9495
|
-
- [Spot Trading](#spot-trading)
|
|
9496
|
-
- [Derivatives (Futures) Trading](#derivatives-futures-trading)
|
|
9497
|
-
- [WebSockets](#websockets)
|
|
9498
|
-
- [Public WebSocket Streams](#public-websocket-streams)
|
|
9499
|
-
- [Private WebSocket Streams](#private-websocket-streams)
|
|
9500
|
-
- [WebSocket API (WebsocketAPIClient)](#websocket-api-websocketapiclient)
|
|
9501
|
-
- [Customise Logging](#customise-logging)
|
|
9502
|
-
- [LLMs & AI](#use-with-llms--ai)
|
|
9503
|
-
- [Used By](#used-by)
|
|
9504
|
-
- [Contributions & Thanks](#contributions--thanks)
|
|
9624
|
+
/** Received data for topic */
|
|
9625
|
+
message: (response: any & { wsKey: WsKey }) => void;
|
|
9505
9626
|
|
|
9506
|
-
|
|
9627
|
+
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
|
9628
|
+
exception: (response: any & { wsKey: WsKey }) => void;
|
|
9507
9629
|
|
|
9508
|
-
|
|
9630
|
+
/**
|
|
9631
|
+
* See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
|
|
9632
|
+
* @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
|
|
9633
|
+
*/
|
|
9634
|
+
error: UseTheExceptionEventInstead;
|
|
9509
9635
|
|
|
9510
|
-
|
|
9511
|
-
|
|
9512
|
-
|
|
9513
|
-
|
|
9514
|
-
|
|
9515
|
-
|
|
9516
|
-
|
|
9517
|
-
|
|
9518
|
-
|
|
9519
|
-
|
|
9520
|
-
|
|
9521
|
-
|
|
9522
|
-
|
|
9523
|
-
|
|
9524
|
-
|
|
9525
|
-
|
|
9526
|
-
|
|
9527
|
-
|
|
9528
|
-
|
|
9529
|
-
|
|
9530
|
-
|
|
9531
|
-
|
|
9532
|
-
|
|
9533
|
-
|
|
9534
|
-
|
|
9535
|
-
|
|
9536
|
-
|
|
9537
|
-
|
|
9538
|
-
|
|
9539
|
-
|
|
9540
|
-
|
|
9541
|
-
|
|
9542
|
-
|
|
9543
|
-
|
|
9544
|
-
|
|
9545
|
-
|
|
9546
|
-
|
|
9547
|
-
|
|
9548
|
-
|
|
9549
|
-
|
|
9550
|
-
|
|
9551
|
-
|
|
9552
|
-
|
|
9553
|
-
|
|
9554
|
-
|
|
9555
|
-
|
|
9556
|
-
|
|
9557
|
-
|
|
9558
|
-
|
|
9559
|
-
|
|
9560
|
-
- [src](./src) - the whole connector written in typescript
|
|
9561
|
-
- [examples](./examples) - some implementation examples & demonstrations. Contributions are welcome!
|
|
9562
|
-
|
|
9563
|
-
---
|
|
9564
|
-
|
|
9565
|
-
# Usage
|
|
9566
|
-
|
|
9567
|
-
Create API credentials on Kraken's website:
|
|
9568
|
-
|
|
9569
|
-
- [Kraken API Key Management](https://www.kraken.com/u/security/api)
|
|
9570
|
-
- [Kraken Futures API Key Management](https://futures.kraken.com/settings/api)
|
|
9571
|
-
|
|
9572
|
-
## REST API
|
|
9573
|
-
|
|
9574
|
-
The SDK provides dedicated REST clients for different trading products:
|
|
9575
|
-
|
|
9576
|
-
- **SpotClient** - for spot trading, staking, and account operations
|
|
9577
|
-
- **DerivativesClient** - for futures trading operations
|
|
9578
|
-
- **InstitutionalClient** - for institutional trading and custody
|
|
9579
|
-
- **PartnerClient** - for partner and affiliate operations
|
|
9580
|
-
|
|
9581
|
-
### Spot Trading
|
|
9582
|
-
|
|
9583
|
-
To use Kraken's Spot APIs, import (or require) the `SpotClient`:
|
|
9584
|
-
|
|
9585
|
-
```javascript
|
|
9586
|
-
import { SpotClient } from '@siebly/kraken-api';
|
|
9587
|
-
// or if you prefer require:
|
|
9588
|
-
// const { SpotClient } = require('@siebly/kraken-api');
|
|
9589
|
-
|
|
9590
|
-
// For public endpoints, API credentials are optional
|
|
9591
|
-
const publicClient = new SpotClient();
|
|
9592
|
-
|
|
9593
|
-
// For private endpoints, provide API credentials
|
|
9594
|
-
const client = new SpotClient({
|
|
9595
|
-
apiKey: 'your-api-key',
|
|
9596
|
-
apiSecret: 'your-base64-encoded-private-key',
|
|
9597
|
-
});
|
|
9598
|
-
|
|
9599
|
-
// Public API Examples
|
|
9600
|
-
|
|
9601
|
-
// Get ticker information
|
|
9602
|
-
const ticker = await publicClient.getTicker({
|
|
9603
|
-
pair: 'XBTUSD',
|
|
9604
|
-
});
|
|
9605
|
-
console.log('Ticker: ', ticker);
|
|
9606
|
-
|
|
9607
|
-
// Get order book
|
|
9608
|
-
const orderBook = await publicClient.getOrderBook({
|
|
9609
|
-
pair: 'XBTUSD',
|
|
9610
|
-
count: 10,
|
|
9611
|
-
});
|
|
9612
|
-
console.log('Order Book: ', orderBook);
|
|
9613
|
-
|
|
9614
|
-
// Private API Examples (requires authentication)
|
|
9615
|
-
|
|
9616
|
-
// Submit a market order
|
|
9617
|
-
client
|
|
9618
|
-
.submitOrder({
|
|
9619
|
-
ordertype: 'market',
|
|
9620
|
-
type: 'buy',
|
|
9621
|
-
volume: '0.01',
|
|
9622
|
-
pair: 'XBTUSD',
|
|
9623
|
-
cl_ord_id: client.generateNewOrderID(),
|
|
9624
|
-
})
|
|
9625
|
-
.then((result) => {
|
|
9626
|
-
console.log('Market Order Result: ', result);
|
|
9627
|
-
})
|
|
9628
|
-
.catch((err) => {
|
|
9629
|
-
console.error('Error: ', err);
|
|
9630
|
-
});
|
|
9631
|
-
|
|
9632
|
-
// Submit a limit order
|
|
9633
|
-
client
|
|
9634
|
-
.submitOrder({
|
|
9635
|
-
ordertype: 'limit',
|
|
9636
|
-
type: 'buy',
|
|
9637
|
-
volume: '0.0001',
|
|
9638
|
-
pair: 'XBTUSD',
|
|
9639
|
-
price: '10000',
|
|
9640
|
-
cl_ord_id: client.generateNewOrderID(),
|
|
9641
|
-
})
|
|
9642
|
-
.then((result) => {
|
|
9643
|
-
console.log('Limit Order Result: ', result);
|
|
9644
|
-
})
|
|
9645
|
-
.catch((err) => {
|
|
9646
|
-
console.error('Error: ', err);
|
|
9647
|
-
});
|
|
9648
|
-
|
|
9649
|
-
// Submit batch of orders (minimum 2, maximum 15)
|
|
9650
|
-
client
|
|
9651
|
-
.submitBatchOrders({
|
|
9652
|
-
pair: 'XBTUSD',
|
|
9653
|
-
orders: [
|
|
9654
|
-
{
|
|
9655
|
-
ordertype: 'limit',
|
|
9656
|
-
type: 'buy',
|
|
9657
|
-
volume: '0.0001',
|
|
9658
|
-
price: '10000.00',
|
|
9659
|
-
timeinforce: 'GTC',
|
|
9660
|
-
cl_ord_id: client.generateNewOrderID(),
|
|
9661
|
-
},
|
|
9662
|
-
{
|
|
9663
|
-
ordertype: 'limit',
|
|
9664
|
-
type: 'buy',
|
|
9665
|
-
volume: '0.0001',
|
|
9666
|
-
price: '11111.00',
|
|
9667
|
-
timeinforce: 'GTC',
|
|
9668
|
-
cl_ord_id: client.generateNewOrderID(),
|
|
9669
|
-
},
|
|
9670
|
-
{
|
|
9671
|
-
ordertype: 'limit',
|
|
9672
|
-
type: 'sell',
|
|
9673
|
-
volume: '0.0001',
|
|
9674
|
-
price: '13000.00',
|
|
9675
|
-
timeinforce: 'GTC',
|
|
9676
|
-
cl_ord_id: client.generateNewOrderID(),
|
|
9677
|
-
},
|
|
9678
|
-
],
|
|
9679
|
-
})
|
|
9680
|
-
.then((result) => {
|
|
9681
|
-
console.log('Batch Order Result: ', JSON.stringify(result, null, 2));
|
|
9682
|
-
})
|
|
9683
|
-
.catch((err) => {
|
|
9684
|
-
console.error('Error: ', err);
|
|
9685
|
-
});
|
|
9686
|
-
|
|
9687
|
-
// Get account balances
|
|
9688
|
-
client
|
|
9689
|
-
.getAccountBalance()
|
|
9690
|
-
.then((balance) => {
|
|
9691
|
-
console.log('Account Balance: ', balance);
|
|
9692
|
-
})
|
|
9693
|
-
.catch((err) => {
|
|
9694
|
-
console.error('Error: ', err);
|
|
9695
|
-
});
|
|
9696
|
-
```
|
|
9697
|
-
|
|
9698
|
-
See [SpotClient](./src/SpotClient.ts) for further information, or the [examples](./examples/) for lots of usage examples.
|
|
9699
|
-
|
|
9700
|
-
### Derivatives (Futures) Trading
|
|
9701
|
-
|
|
9702
|
-
Use the `DerivativesClient` for futures trading operations:
|
|
9703
|
-
|
|
9704
|
-
```javascript
|
|
9705
|
-
import { DerivativesClient } from '@siebly/kraken-api';
|
|
9706
|
-
// or if you prefer require:
|
|
9707
|
-
// const { DerivativesClient } = require('@siebly/kraken-api');
|
|
9708
|
-
|
|
9709
|
-
// For public endpoints, API credentials are optional
|
|
9710
|
-
const publicClient = new DerivativesClient();
|
|
9711
|
-
|
|
9712
|
-
// For private endpoints, provide API credentials
|
|
9713
|
-
const client = new DerivativesClient({
|
|
9714
|
-
apiKey: 'your-api-key',
|
|
9715
|
-
apiSecret: 'your-api-secret',
|
|
9716
|
-
});
|
|
9717
|
-
|
|
9718
|
-
// Public API Examples
|
|
9719
|
-
|
|
9720
|
-
// Get order book for a specific instrument
|
|
9721
|
-
const orderBook = await publicClient.getOrderBook({
|
|
9722
|
-
symbol: 'PF_XBTUSD',
|
|
9723
|
-
});
|
|
9724
|
-
console.log('Futures Order Book: ', orderBook);
|
|
9725
|
-
|
|
9726
|
-
// Get ticker information
|
|
9727
|
-
const ticker = await publicClient.getTickers({
|
|
9728
|
-
symbol: 'PF_XBTUSD',
|
|
9729
|
-
});
|
|
9730
|
-
console.log('Futures Ticker: ', ticker);
|
|
9731
|
-
|
|
9732
|
-
// Private API Examples (requires authentication)
|
|
9733
|
-
|
|
9734
|
-
// Get account balances
|
|
9735
|
-
client
|
|
9736
|
-
.getAccountsDetails()
|
|
9737
|
-
.then((accounts) => {
|
|
9738
|
-
console.log('Accounts Details: ', accounts);
|
|
9739
|
-
})
|
|
9740
|
-
.catch((err) => {
|
|
9741
|
-
console.error('Error: ', err);
|
|
9742
|
-
});
|
|
9743
|
-
|
|
9744
|
-
// Submit a limit order
|
|
9745
|
-
client
|
|
9746
|
-
.submitOrder({
|
|
9747
|
-
orderType: 'lmt',
|
|
9748
|
-
symbol: 'PF_ETHUSD', // Perpetual ETH/USD
|
|
9749
|
-
side: 'buy',
|
|
9750
|
-
size: 0.01, // Contract size
|
|
9751
|
-
limitPrice: 1000,
|
|
9752
|
-
cliOrdId: client.generateNewOrderID(),
|
|
9753
|
-
})
|
|
9754
|
-
.then((result) => {
|
|
9755
|
-
console.log('Limit Order Result: ', JSON.stringify(result, null, 2));
|
|
9756
|
-
})
|
|
9757
|
-
.catch((err) => {
|
|
9758
|
-
console.error('Error: ', err);
|
|
9759
|
-
});
|
|
9760
|
-
```
|
|
9761
|
-
|
|
9762
|
-
See [DerivativesClient](./src/DerivativesClient.ts) for further information.
|
|
9763
|
-
|
|
9764
|
-
## WebSockets
|
|
9765
|
-
|
|
9766
|
-
Kraken supports two types of WebSocket connections:
|
|
9767
|
-
|
|
9768
|
-
1. **WebSocket Subscriptions** - Real-time market data and account updates via the `WebsocketClient`
|
|
9769
|
-
2. **WebSocket API** - REST-like request/response trading via the `WebsocketAPIClient`
|
|
9770
|
-
|
|
9771
|
-
### WebSocket Subscriptions (WebsocketClient)
|
|
9772
|
-
|
|
9773
|
-
The unified `WebsocketClient` handles all Kraken WebSocket streams with automatic connection management and reconnection.
|
|
9774
|
-
|
|
9775
|
-
Key WebSocket features:
|
|
9776
|
-
|
|
9777
|
-
- Event driven messaging
|
|
9778
|
-
- Smart WebSocket persistence with automatic reconnection
|
|
9779
|
-
- Heartbeat mechanisms to detect disconnections
|
|
9780
|
-
- Automatic resubscription after reconnection
|
|
9781
|
-
- Support for both Spot and Futures markets
|
|
9782
|
-
- Support for both public and private WebSocket streams
|
|
9783
|
-
|
|
9784
|
-
### Public WebSocket Streams
|
|
9785
|
-
|
|
9786
|
-
For public market data, API credentials are not required:
|
|
9787
|
-
|
|
9788
|
-
```javascript
|
|
9789
|
-
import { WebsocketClient } from '@siebly/kraken-api';
|
|
9790
|
-
// or if you prefer require:
|
|
9791
|
-
// const { WebsocketClient } = require('@siebly/kraken-api');
|
|
9792
|
-
// Create WebSocket client for public streams
|
|
9793
|
-
const wsClient = new WebsocketClient();
|
|
9794
|
-
|
|
9795
|
-
// Set up event handlers
|
|
9796
|
-
wsClient.on('open', (data) => {
|
|
9797
|
-
console.log('WebSocket connected: ', data?.wsKey);
|
|
9798
|
-
});
|
|
9799
|
-
|
|
9800
|
-
wsClient.on('message', (data) => {
|
|
9801
|
-
console.log('Data received: ', JSON.stringify(data, null, 2));
|
|
9802
|
-
});
|
|
9803
|
-
|
|
9804
|
-
wsClient.on('reconnected', (data) => {
|
|
9805
|
-
console.log('WebSocket reconnected: ', data);
|
|
9806
|
-
});
|
|
9807
|
-
|
|
9808
|
-
wsClient.on('exception', (data) => {
|
|
9809
|
-
console.error('WebSocket error: ', data);
|
|
9810
|
-
});
|
|
9811
|
-
|
|
9812
|
-
// Spot - Subscribe to public data streams
|
|
9813
|
-
wsClient.subscribe(
|
|
9814
|
-
{
|
|
9815
|
-
topic: 'ticker',
|
|
9816
|
-
payload: {
|
|
9817
|
-
symbol: ['BTC/USD', 'ETH/USD'],
|
|
9818
|
-
},
|
|
9819
|
-
},
|
|
9820
|
-
'spotPublicV2',
|
|
9821
|
-
);
|
|
9822
|
-
|
|
9823
|
-
wsClient.subscribe(
|
|
9824
|
-
{
|
|
9825
|
-
topic: 'book',
|
|
9826
|
-
payload: {
|
|
9827
|
-
symbol: ['BTC/USD'],
|
|
9828
|
-
depth: 10,
|
|
9829
|
-
},
|
|
9830
|
-
},
|
|
9831
|
-
'spotPublicV2',
|
|
9832
|
-
);
|
|
9833
|
-
|
|
9834
|
-
// Derivatives - Subscribe to public data streams
|
|
9835
|
-
wsClient.subscribe(
|
|
9836
|
-
{
|
|
9837
|
-
topic: 'ticker',
|
|
9838
|
-
payload: {
|
|
9839
|
-
product_ids: ['PI_XBTUSD', 'PI_ETHUSD'],
|
|
9840
|
-
},
|
|
9841
|
-
},
|
|
9842
|
-
'derivativesPublicV1',
|
|
9843
|
-
);
|
|
9844
|
-
|
|
9845
|
-
wsClient.subscribe(
|
|
9846
|
-
{
|
|
9847
|
-
topic: 'book',
|
|
9848
|
-
payload: {
|
|
9849
|
-
product_ids: ['PI_XBTUSD'],
|
|
9850
|
-
},
|
|
9851
|
-
},
|
|
9852
|
-
'derivativesPublicV1',
|
|
9853
|
-
);
|
|
9854
|
-
```
|
|
9855
|
-
|
|
9856
|
-
### Private WebSocket Streams
|
|
9857
|
-
|
|
9858
|
-
For private account data streams, API credentials are required:
|
|
9859
|
-
|
|
9860
|
-
```javascript
|
|
9861
|
-
import { WebsocketClient } from '@siebly/kraken-api';
|
|
9862
|
-
|
|
9863
|
-
// Create WebSocket client with API credentials for private streams
|
|
9864
|
-
const wsClient = new WebsocketClient({
|
|
9865
|
-
apiKey: 'your-api-key',
|
|
9866
|
-
apiSecret: 'your-api-secret',
|
|
9867
|
-
});
|
|
9868
|
-
|
|
9869
|
-
// Set up event handlers
|
|
9870
|
-
wsClient.on('open', (data) => {
|
|
9871
|
-
console.log('Private WebSocket connected: ', data?.wsKey);
|
|
9872
|
-
});
|
|
9873
|
-
|
|
9874
|
-
wsClient.on('message', (data) => {
|
|
9875
|
-
console.log('Private data received: ', JSON.stringify(data, null, 2));
|
|
9876
|
-
});
|
|
9877
|
-
|
|
9878
|
-
wsClient.on('authenticated', (data) => {
|
|
9879
|
-
console.log('WebSocket authenticated: ', data);
|
|
9880
|
-
});
|
|
9881
|
-
|
|
9882
|
-
wsClient.on('response', (data) => {
|
|
9883
|
-
console.log('WebSocket response: ', data);
|
|
9884
|
-
});
|
|
9885
|
-
|
|
9886
|
-
wsClient.on('exception', (data) => {
|
|
9887
|
-
console.error('WebSocket error: ', data);
|
|
9888
|
-
});
|
|
9889
|
-
|
|
9890
|
-
// Spot - Subscribe to private data streams
|
|
9891
|
-
wsClient.subscribe(
|
|
9892
|
-
{
|
|
9893
|
-
topic: 'executions',
|
|
9894
|
-
payload: {
|
|
9895
|
-
snap_trades: true,
|
|
9896
|
-
snap_orders: true,
|
|
9897
|
-
order_status: true,
|
|
9898
|
-
},
|
|
9899
|
-
},
|
|
9900
|
-
'spotPrivateV2',
|
|
9901
|
-
);
|
|
9902
|
-
|
|
9903
|
-
wsClient.subscribe(
|
|
9904
|
-
{
|
|
9905
|
-
topic: 'balances',
|
|
9906
|
-
payload: {
|
|
9907
|
-
snapshot: true,
|
|
9908
|
-
},
|
|
9909
|
-
},
|
|
9910
|
-
'spotPrivateV2',
|
|
9911
|
-
);
|
|
9912
|
-
|
|
9913
|
-
// Derivatives - Subscribe to private data streams
|
|
9914
|
-
// Note: SDK automatically handles authentication and challenge tokens
|
|
9915
|
-
wsClient.subscribe('open_orders', 'derivativesPrivateV1');
|
|
9916
|
-
|
|
9917
|
-
wsClient.subscribe(
|
|
9918
|
-
{
|
|
9919
|
-
topic: 'fills',
|
|
9920
|
-
payload: {
|
|
9921
|
-
product_ids: ['PF_XBTUSD'],
|
|
9922
|
-
},
|
|
9923
|
-
},
|
|
9924
|
-
'derivativesPrivateV1',
|
|
9925
|
-
);
|
|
9926
|
-
|
|
9927
|
-
wsClient.subscribe('balances', 'derivativesPrivateV1');
|
|
9928
|
-
|
|
9929
|
-
wsClient.subscribe('open_positions', 'derivativesPrivateV1');
|
|
9930
|
-
```
|
|
9931
|
-
|
|
9932
|
-
For more comprehensive examples, including custom logging and error handling, check the [examples](./examples/WebSockets) folder.
|
|
9933
|
-
|
|
9934
|
-
### WebSocket API (WebsocketAPIClient)
|
|
9935
|
-
|
|
9936
|
-
The `WebsocketAPIClient` provides a REST-like interface for trading operations over WebSocket, offering lower latency than REST APIs. Currently, only Spot trading is supported.
|
|
9937
|
-
|
|
9938
|
-
```javascript
|
|
9939
|
-
import { WebsocketAPIClient } from '@siebly/kraken-api';
|
|
9940
|
-
|
|
9941
|
-
// Create WebSocket API client with credentials
|
|
9942
|
-
const wsApiClient = new WebsocketAPIClient({
|
|
9943
|
-
apiKey: 'your-api-key',
|
|
9944
|
-
apiSecret: 'your-api-secret',
|
|
9945
|
-
});
|
|
9946
|
-
|
|
9947
|
-
// The client handles event listeners automatically, but you can customize them
|
|
9948
|
-
wsApiClient
|
|
9949
|
-
.getWSClient()
|
|
9950
|
-
.on('open', (data) => {
|
|
9951
|
-
console.log('WebSocket API connected:', data.wsKey);
|
|
9952
|
-
})
|
|
9953
|
-
.on('response', (data) => {
|
|
9954
|
-
console.log('Response:', data);
|
|
9955
|
-
})
|
|
9956
|
-
.on('exception', (data) => {
|
|
9957
|
-
console.error('Error:', data);
|
|
9958
|
-
});
|
|
9959
|
-
|
|
9960
|
-
// Trading operations return promises
|
|
9961
|
-
|
|
9962
|
-
// Submit a spot order
|
|
9963
|
-
const orderResponse = await wsApiClient.submitSpotOrder({
|
|
9964
|
-
order_type: 'limit',
|
|
9965
|
-
side: 'buy',
|
|
9966
|
-
limit_price: 26500.4,
|
|
9967
|
-
order_qty: 1.2,
|
|
9968
|
-
symbol: 'BTC/USD',
|
|
9969
|
-
});
|
|
9970
|
-
console.log('Order placed:', orderResponse);
|
|
9971
|
-
|
|
9972
|
-
// Amend an existing order
|
|
9973
|
-
const amendResponse = await wsApiClient.amendSpotOrder({
|
|
9974
|
-
order_id: 'OAIYAU-LGI3M-PFM5VW',
|
|
9975
|
-
order_qty: 1.5,
|
|
9976
|
-
limit_price: 27000,
|
|
9977
|
-
});
|
|
9978
|
-
|
|
9979
|
-
// Cancel specific orders
|
|
9980
|
-
const cancelResponse = await wsApiClient.cancelSpotOrder({
|
|
9981
|
-
order_id: ['OM5CRX-N2HAL-GFGWE9', 'OLUMT4-UTEGU-ZYM7E9'],
|
|
9982
|
-
});
|
|
9983
|
-
|
|
9984
|
-
// Cancel all open orders
|
|
9985
|
-
const cancelAllResponse = await wsApiClient.cancelAllSpotOrders();
|
|
9986
|
-
```
|
|
9987
|
-
|
|
9988
|
-
The WebSocket API provides several advantages:
|
|
9989
|
-
|
|
9990
|
-
- **Lower Latency** - Faster than REST API for high-frequency trading
|
|
9991
|
-
- **Connection Reuse** - Single persistent connection for multiple requests
|
|
9992
|
-
- **Better Performance** - Batch operations for submitting/canceling multiple orders
|
|
9993
|
-
- **Type Safety** - Full TypeScript support with typed requests and responses
|
|
9994
|
-
|
|
9995
|
-
See the [WebSocket API examples](./examples/WebSockets/Spot/wsAPI.ts) for more detailed usage.
|
|
9996
|
-
|
|
9997
|
-
---
|
|
9998
|
-
|
|
9999
|
-
## Customise Logging
|
|
10000
|
-
|
|
10001
|
-
Pass a custom logger which supports the log methods `trace`, `info` and `error`, or override methods from the default logger as desired.
|
|
10002
|
-
|
|
10003
|
-
```javascript
|
|
10004
|
-
import { WebsocketClient, DefaultLogger } from '@siebly/kraken-api';
|
|
10005
|
-
|
|
10006
|
-
// E.g. customise logging for only the trace level:
|
|
10007
|
-
const customLogger: DefaultLogger = {
|
|
10008
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10009
|
-
trace: (...params: LogParams): void => {
|
|
10010
|
-
// console.log('trace', ...params);
|
|
10011
|
-
},
|
|
10012
|
-
info: (...params: LogParams): void => {
|
|
10013
|
-
console.log('info', ...params);
|
|
10014
|
-
},
|
|
10015
|
-
error: (...params: LogParams): void => {
|
|
10016
|
-
console.error('error', ...params);
|
|
10017
|
-
},
|
|
10018
|
-
};
|
|
10019
|
-
|
|
10020
|
-
const ws = new WebsocketClient(
|
|
10021
|
-
{
|
|
10022
|
-
apiKey: 'apiKeyHere',
|
|
10023
|
-
apiSecret: 'apiSecretHere',
|
|
10024
|
-
},
|
|
10025
|
-
customLogger,
|
|
10026
|
-
);
|
|
10027
|
-
```
|
|
10028
|
-
|
|
10029
|
-
## Use with LLMs & AI
|
|
10030
|
-
|
|
10031
|
-
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.
|
|
10032
|
-
|
|
10033
|
-
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.
|
|
10034
|
-
|
|
10035
|
-
---
|
|
10036
|
-
|
|
10037
|
-
## Used By
|
|
10038
|
-
|
|
10039
|
-
[](https://github.com/sieblyio/kraken-api/network/dependents)
|
|
10040
|
-
|
|
10041
|
-
---
|
|
10042
|
-
|
|
10043
|
-
<!-- template_contributions -->
|
|
10044
|
-
|
|
10045
|
-
### Contributions & Thanks
|
|
10046
|
-
|
|
10047
|
-
Have my projects helped you? Share the love, there are many ways you can show your thanks:
|
|
10048
|
-
|
|
10049
|
-
- Star & share my projects.
|
|
10050
|
-
- Are my projects useful? Sponsor me on Github and support my effort to maintain & improve them: https://github.com/sponsors/tiagosiebler
|
|
10051
|
-
- Have an interesting project? Get in touch & invite me to it.
|
|
10052
|
-
- Or buy me all the coffee:
|
|
10053
|
-
- ETH(ERC20): `0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C` <!-- metamask -->
|
|
10054
|
-
|
|
10055
|
-
<!-- template_contributions_end -->
|
|
10056
|
-
|
|
10057
|
-
### Contributions & Pull Requests
|
|
10058
|
-
|
|
10059
|
-
Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
|
|
10060
|
-
|
|
10061
|
-
<!-- template_star_history -->
|
|
10062
|
-
|
|
10063
|
-
## Star History
|
|
10064
|
-
|
|
10065
|
-
[](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)
|
|
10066
|
-
|
|
10067
|
-
<!-- template_star_history_end -->
|
|
10068
|
-
|
|
10069
|
-
================
|
|
10070
|
-
File: examples/Derivatives/Private/submitOrder.ts
|
|
10071
|
-
================
|
|
10072
|
-
import { DerivativesClient } from '../../../src/index.js';
|
|
10073
|
-
⋮----
|
|
10074
|
-
// This example shows how to call Kraken API endpoint with either node.js,
|
|
10075
|
-
// javascript (js) or typescript (ts) with the npm module "@siebly/kraken-api" for Kraken exchange
|
|
10076
|
-
// for FUTURES ORDER MANAGEMENT
|
|
10077
|
-
⋮----
|
|
10078
|
-
/**
|
|
10079
|
-
* import { DerivativesClient } from '@siebly/kraken-api';
|
|
10080
|
-
*/
|
|
10081
|
-
⋮----
|
|
10082
|
-
// initialise the client
|
|
10083
|
-
/**
|
|
10084
|
-
*
|
|
10085
|
-
* Kraken Futures API uses API Key and API Secret
|
|
10086
|
-
*
|
|
10087
|
-
* Example:
|
|
10088
|
-
* {
|
|
10089
|
-
* apiKey: 'your-api-key',
|
|
10090
|
-
* apiSecret: 'your-api-secret',
|
|
10091
|
-
* }
|
|
10092
|
-
*
|
|
10093
|
-
* API Key Permissions Required: Orders and trades - Create & modify orders
|
|
10094
|
-
*
|
|
10095
|
-
*/
|
|
10096
|
-
⋮----
|
|
10097
|
-
async function submitLimitOrder()
|
|
10098
|
-
⋮----
|
|
10099
|
-
// Submit limit order for Futures
|
|
10100
|
-
⋮----
|
|
10101
|
-
symbol: 'PF_ETHUSD', // Perpetual ETH/USD
|
|
10102
|
-
⋮----
|
|
10103
|
-
size: 0.01, // Contract size
|
|
10104
|
-
⋮----
|
|
10105
|
-
// Response includes:
|
|
10106
|
-
// - status: placed, partiallyFilled, filled, or rejection reason
|
|
10107
|
-
// - order_id: Unique order identifier
|
|
10108
|
-
// - orderEvents: Array of order events (PLACE, EXECUTE, etc.)
|
|
10109
|
-
⋮----
|
|
10110
|
-
async function submitMarketOrder()
|
|
10111
|
-
⋮----
|
|
10112
|
-
// Submit market order (IOC with 1% price protection)
|
|
10113
|
-
⋮----
|
|
10114
|
-
side: 'sell', // or "buy"
|
|
10115
|
-
⋮----
|
|
10116
|
-
async function submitPostOnlyOrder()
|
|
10117
|
-
⋮----
|
|
10118
|
-
// Submit post-only order (maker-only)
|
|
10119
|
-
⋮----
|
|
10120
|
-
async function submitReduceOnlyOrder()
|
|
10121
|
-
⋮----
|
|
10122
|
-
// Submit reduce-only order (only closes position, won't open new)
|
|
10123
|
-
⋮----
|
|
10124
|
-
reduceOnly: true, // Only reduce existing position
|
|
10125
|
-
⋮----
|
|
10126
|
-
async function batchOrderSubmit()
|
|
10127
|
-
⋮----
|
|
10128
|
-
// Send, edit, and cancel orders in a single batch request
|
|
10129
|
-
⋮----
|
|
10130
|
-
// Send new order
|
|
10131
|
-
⋮----
|
|
10132
|
-
order_tag: 'order-1', // Tag to map responses
|
|
10133
|
-
⋮----
|
|
10134
|
-
// Send another order
|
|
10135
|
-
⋮----
|
|
10136
|
-
// Response includes batchStatus array with results for each order
|
|
10137
|
-
// - status: placed, edited, cancelled, or rejection reason
|
|
10138
|
-
// - order_tag: Maps back to your request
|
|
10139
|
-
⋮----
|
|
10140
|
-
// Uncomment the function you want to test:
|
|
10141
|
-
⋮----
|
|
10142
|
-
// submitLimitOrder();
|
|
10143
|
-
// submitMarketOrder();
|
|
10144
|
-
// submitPostOnlyOrder();
|
|
10145
|
-
// submitReduceOnlyOrder();
|
|
10146
|
-
//batchOrderSubmit();
|
|
10147
|
-
|
|
10148
|
-
================
|
|
10149
|
-
File: src/lib/websocket/websocket-util.ts
|
|
10150
|
-
================
|
|
10151
|
-
import WebSocket from 'isomorphic-ws';
|
|
10152
|
-
⋮----
|
|
10153
|
-
import { WSAPIRequestOperationKrakenSpot } from '../../types/websockets/ws-api.js';
|
|
10154
|
-
import { WSTopic } from '../../types/websockets/ws-subscriptions.js';
|
|
10155
|
-
⋮----
|
|
10156
|
-
/** Should be one WS key per unique URL */
|
|
10157
|
-
⋮----
|
|
10158
|
-
/**
|
|
10159
|
-
* Public WebSocket subscriptions for Kraken Spot products, via the V2 API
|
|
10160
|
-
*
|
|
10161
|
-
* - Ref: https://docs.kraken.com/api/docs/guides/spot-ws-intro
|
|
10162
|
-
* - Channels: https://docs.kraken.com/api/docs/websocket-v2/add_order
|
|
10163
|
-
*
|
|
10164
|
-
* Note: Use spotPrivateV2 for private channels (requires API keys).
|
|
10165
|
-
*/
|
|
10166
|
-
⋮----
|
|
10167
|
-
/**
|
|
10168
|
-
* Public WebSocket subscriptions for Kraken Derivatives products, via the V1 API:
|
|
10169
|
-
*
|
|
10170
|
-
* - Ref: https://docs.kraken.com/api/docs/guides/futures-websockets
|
|
10171
|
-
* - Channels: https://docs.kraken.com/api/docs/futures-api/websocket/open_orders
|
|
10172
|
-
*
|
|
10173
|
-
* Note: While both Public and Private channels use the same WebSocket URL, we will actually maintain separate connections for easier management. Private channels require authentication and the connection is authenticated automatically.
|
|
10174
|
-
*/
|
|
10175
|
-
⋮----
|
|
10176
|
-
/** This is used to differentiate between each of the available websocket streams */
|
|
10177
|
-
export type WsKey = (typeof WS_KEY_MAP)[keyof typeof WS_KEY_MAP];
|
|
10178
|
-
⋮----
|
|
10179
|
-
export type WSOperation = 'subscribe' | 'unsubscribe';
|
|
10180
|
-
⋮----
|
|
10181
|
-
/**
|
|
10182
|
-
* Normalised internal format for a request (subscribe/unsubscribe/etc) on a topic, with optional parameters.
|
|
10183
|
-
*
|
|
10184
|
-
* - Topic: the topic this event is for
|
|
10185
|
-
* - Payload: the parameters to include, optional. E.g. auth requires key + sign. Some topics allow configurable parameters.
|
|
10186
|
-
*/
|
|
10187
|
-
export interface WSTopicRequest<
|
|
10188
|
-
TWSTopic extends WSTopic = WSTopic,
|
|
10189
|
-
TWSPayload = any,
|
|
10190
|
-
> {
|
|
10191
|
-
topic: TWSTopic;
|
|
10192
|
-
payload?: TWSPayload;
|
|
10193
|
-
}
|
|
10194
|
-
⋮----
|
|
10195
|
-
/**
|
|
10196
|
-
* Conveniently allow users to request a topic either as string topics or objects (containing string topic + params)
|
|
10197
|
-
*/
|
|
10198
|
-
export type WSTopicRequestOrStringTopic<
|
|
10199
|
-
TWSTopic extends WSTopic,
|
|
10200
|
-
TWSPayload = any,
|
|
10201
|
-
> = WSTopicRequest<TWSTopic, TWSPayload> | string;
|
|
10202
|
-
⋮----
|
|
10203
|
-
export interface WSRequestOperationKraken<
|
|
10204
|
-
TWSTopic extends string,
|
|
10205
|
-
TWSParams extends object = any,
|
|
10206
|
-
> {
|
|
10207
|
-
// spot only
|
|
10208
|
-
method?: WSOperation;
|
|
10209
|
-
// futures only
|
|
10210
|
-
event?: WSOperation;
|
|
10211
|
-
params:
|
|
10212
|
-
| {
|
|
10213
|
-
channel: (TWSTopic | string | number)[];
|
|
10214
|
-
symbol?: string[];
|
|
10215
|
-
event_trigger?: string;
|
|
10216
|
-
snapshot?: boolean;
|
|
10217
|
-
}
|
|
10218
|
-
| TWSParams;
|
|
10219
|
-
req_id: number;
|
|
10220
|
-
/**
|
|
10221
|
-
* The following are needed for futures/derivatives requests
|
|
10222
|
-
*/
|
|
10223
|
-
feed?: TWSTopic;
|
|
10224
|
-
api_key?: string;
|
|
10225
|
-
original_challenge?: string;
|
|
10226
|
-
signed_challenge?: string;
|
|
10227
|
-
}
|
|
10228
|
-
⋮----
|
|
10229
|
-
// spot only
|
|
10230
|
-
⋮----
|
|
10231
|
-
// futures only
|
|
10232
|
-
⋮----
|
|
10233
|
-
/**
|
|
10234
|
-
* The following are needed for futures/derivatives requests
|
|
10235
|
-
*/
|
|
10236
|
-
⋮----
|
|
10237
|
-
/**
|
|
10238
|
-
* #305: ws.terminate() is undefined in browsers.
|
|
10239
|
-
* This only works in node.js, not in browsers.
|
|
10240
|
-
* Does nothing if `ws` is undefined. Does nothing in browsers.
|
|
10241
|
-
*/
|
|
10242
|
-
export function safeTerminateWs(
|
|
10243
|
-
ws?: WebSocket | any,
|
|
10244
|
-
fallbackToClose?: boolean,
|
|
10245
|
-
): boolean
|
|
10246
|
-
⋮----
|
|
10247
|
-
/**
|
|
10248
|
-
* WS API promises are stored using a primary key. This key is constructed using
|
|
10249
|
-
* properties found in every request & reply.
|
|
10250
|
-
*
|
|
10251
|
-
* The counterpart to this is in resolveEmittableEvents
|
|
10252
|
-
*/
|
|
10253
|
-
export function getPromiseRefForWSAPIRequest(
|
|
10254
|
-
wsKey: WsKey,
|
|
10255
|
-
requestEvent: WSAPIRequestOperationKrakenSpot,
|
|
10256
|
-
): string
|
|
10257
|
-
|
|
10258
|
-
================
|
|
10259
|
-
File: src/lib/BaseWSClient.ts
|
|
10260
|
-
================
|
|
10261
|
-
import { EventEmitter } from 'events';
|
|
10262
|
-
import WebSocket from 'isomorphic-ws';
|
|
10263
|
-
⋮----
|
|
10264
|
-
import {
|
|
10265
|
-
isMessageEvent,
|
|
10266
|
-
MessageEventLike,
|
|
10267
|
-
} from '../types/websockets/ws-events.js';
|
|
10268
|
-
import {
|
|
10269
|
-
WebsocketClientOptions,
|
|
10270
|
-
WSClientConfigurableOptions,
|
|
10271
|
-
WsEventInternalSrc,
|
|
10272
|
-
} from '../types/websockets/ws-general.js';
|
|
10273
|
-
import { WSTopic } from '../types/websockets/ws-subscriptions.js';
|
|
10274
|
-
import { checkWebCryptoAPISupported } from './webCryptoAPI.js';
|
|
10275
|
-
import { DefaultLogger } from './websocket/logger.js';
|
|
10276
|
-
import {
|
|
10277
|
-
safeTerminateWs,
|
|
10278
|
-
WSOperation,
|
|
10279
|
-
WSTopicRequest,
|
|
10280
|
-
WSTopicRequestOrStringTopic,
|
|
10281
|
-
} from './websocket/websocket-util.js';
|
|
10282
|
-
import { WsStore } from './websocket/WsStore.js';
|
|
10283
|
-
import {
|
|
10284
|
-
WSConnectedResult,
|
|
10285
|
-
WsConnectionStateEnum,
|
|
10286
|
-
} from './websocket/WsStore.types.js';
|
|
10287
|
-
⋮----
|
|
10288
|
-
type UseTheExceptionEventInstead = never;
|
|
10289
|
-
⋮----
|
|
10290
|
-
interface WSClientEventMap<WsKey extends string> {
|
|
10291
|
-
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
|
10292
|
-
open: (evt: {
|
|
10293
|
-
wsKey: WsKey;
|
|
10294
|
-
event: any;
|
|
10295
|
-
wsUrl: string;
|
|
10296
|
-
ws: WebSocket;
|
|
10297
|
-
}) => void;
|
|
10298
|
-
|
|
10299
|
-
/** Reconnecting a dropped connection */
|
|
10300
|
-
reconnecting: (evt: { wsKey: WsKey; event: any }) => void;
|
|
10301
|
-
|
|
10302
|
-
/** Successfully reconnected a connection that dropped */
|
|
10303
|
-
reconnected: (evt: {
|
|
10304
|
-
wsKey: WsKey;
|
|
10305
|
-
event: any;
|
|
10306
|
-
wsUrl: string;
|
|
10307
|
-
ws: WebSocket;
|
|
10308
|
-
}) => void;
|
|
10309
|
-
|
|
10310
|
-
/** Connection closed */
|
|
10311
|
-
close: (evt: { wsKey: WsKey; event: any }) => void;
|
|
10312
|
-
|
|
10313
|
-
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
|
10314
|
-
response: (response: any & { wsKey: WsKey }) => void;
|
|
10315
|
-
|
|
10316
|
-
/** Received data for topic */
|
|
10317
|
-
message: (response: any & { wsKey: WsKey }) => void;
|
|
10318
|
-
|
|
10319
|
-
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
|
10320
|
-
exception: (response: any & { wsKey: WsKey }) => void;
|
|
10321
|
-
|
|
10322
|
-
/**
|
|
10323
|
-
* See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
|
|
10324
|
-
* @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
|
|
10325
|
-
*/
|
|
10326
|
-
error: UseTheExceptionEventInstead;
|
|
10327
|
-
|
|
10328
|
-
/** Confirmation that a connection successfully authenticated */
|
|
10329
|
-
authenticated: (event: { wsKey: WsKey; event: any }) => void;
|
|
10330
|
-
}
|
|
10331
|
-
⋮----
|
|
10332
|
-
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
|
10333
|
-
⋮----
|
|
10334
|
-
/** Reconnecting a dropped connection */
|
|
10335
|
-
⋮----
|
|
10336
|
-
/** Successfully reconnected a connection that dropped */
|
|
10337
|
-
⋮----
|
|
10338
|
-
/** Connection closed */
|
|
10339
|
-
⋮----
|
|
10340
|
-
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
|
10341
|
-
⋮----
|
|
10342
|
-
/** Received data for topic */
|
|
10343
|
-
⋮----
|
|
10344
|
-
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
|
10345
|
-
⋮----
|
|
10346
|
-
/**
|
|
10347
|
-
* See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
|
|
10348
|
-
* @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
|
|
10349
|
-
*/
|
|
10350
|
-
⋮----
|
|
10351
|
-
/** Confirmation that a connection successfully authenticated */
|
|
10352
|
-
⋮----
|
|
10353
|
-
export interface EmittableEvent<
|
|
10354
|
-
TEventType extends
|
|
10355
|
-
keyof WSClientEventMap<string> = keyof WSClientEventMap<string>,
|
|
10356
|
-
> {
|
|
10357
|
-
eventType:
|
|
10358
|
-
| TEventType
|
|
10359
|
-
| 'pong'
|
|
10360
|
-
| 'connectionReady' // tied to "requireConnectionReadyConfirmation";
|
|
10361
|
-
| 'connectionReadyForAuth'; // tied to specific events we need to wait for, before we can begin post-connect auth
|
|
10362
|
-
event: Parameters<WSClientEventMap<string>[TEventType]>[0];
|
|
10363
|
-
isWSAPIResponse?: boolean;
|
|
10364
|
-
}
|
|
10365
|
-
⋮----
|
|
10366
|
-
| 'connectionReady' // tied to "requireConnectionReadyConfirmation";
|
|
10367
|
-
| 'connectionReadyForAuth'; // tied to specific events we need to wait for, before we can begin post-connect auth
|
|
10368
|
-
⋮----
|
|
10369
|
-
// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
|
|
10370
|
-
export interface BaseWebsocketClient<
|
|
10371
|
-
TWSKey extends string,
|
|
10372
|
-
TWSRequestEvent extends object,
|
|
10373
|
-
> {
|
|
10374
|
-
on<U extends keyof WSClientEventMap<TWSKey>>(
|
|
10375
|
-
event: U,
|
|
10376
|
-
listener: WSClientEventMap<TWSKey>[U],
|
|
10377
|
-
): this;
|
|
9636
|
+
/** Confirmation that a connection successfully authenticated */
|
|
9637
|
+
authenticated: (event: { wsKey: WsKey; event: any }) => void;
|
|
9638
|
+
}
|
|
9639
|
+
⋮----
|
|
9640
|
+
/** Connection opened. If this connection was previously opened and reconnected, expect the reconnected event instead */
|
|
9641
|
+
⋮----
|
|
9642
|
+
/** Reconnecting a dropped connection */
|
|
9643
|
+
⋮----
|
|
9644
|
+
/** Successfully reconnected a connection that dropped */
|
|
9645
|
+
⋮----
|
|
9646
|
+
/** Connection closed */
|
|
9647
|
+
⋮----
|
|
9648
|
+
/** Received reply to websocket command (e.g. after subscribing to topics) */
|
|
9649
|
+
⋮----
|
|
9650
|
+
/** Received data for topic */
|
|
9651
|
+
⋮----
|
|
9652
|
+
/** Exception from ws client OR custom listeners (e.g. if you throw inside your event handler) */
|
|
9653
|
+
⋮----
|
|
9654
|
+
/**
|
|
9655
|
+
* See for more information: https://github.com/tiagosiebler/bybit-api/issues/413
|
|
9656
|
+
* @deprecated Use the 'exception' event instead. The 'error' event had the unintended consequence of throwing an unhandled promise rejection.
|
|
9657
|
+
*/
|
|
9658
|
+
⋮----
|
|
9659
|
+
/** Confirmation that a connection successfully authenticated */
|
|
9660
|
+
⋮----
|
|
9661
|
+
export interface EmittableEvent<
|
|
9662
|
+
TEventType extends
|
|
9663
|
+
keyof WSClientEventMap<string> = keyof WSClientEventMap<string>,
|
|
9664
|
+
> {
|
|
9665
|
+
eventType:
|
|
9666
|
+
| TEventType
|
|
9667
|
+
| 'pong'
|
|
9668
|
+
| 'connectionReady' // tied to "requireConnectionReadyConfirmation";
|
|
9669
|
+
| 'connectionReadyForAuth'; // tied to specific events we need to wait for, before we can begin post-connect auth
|
|
9670
|
+
event: Parameters<WSClientEventMap<string>[TEventType]>[0];
|
|
9671
|
+
isWSAPIResponse?: boolean;
|
|
9672
|
+
}
|
|
9673
|
+
⋮----
|
|
9674
|
+
| 'connectionReady' // tied to "requireConnectionReadyConfirmation";
|
|
9675
|
+
| 'connectionReadyForAuth'; // tied to specific events we need to wait for, before we can begin post-connect auth
|
|
9676
|
+
⋮----
|
|
9677
|
+
// Type safety for on and emit handlers: https://stackoverflow.com/a/61609010/880837
|
|
9678
|
+
export interface BaseWebsocketClient<
|
|
9679
|
+
TWSKey extends string,
|
|
9680
|
+
TWSRequestEvent extends object,
|
|
9681
|
+
> {
|
|
9682
|
+
on<U extends keyof WSClientEventMap<TWSKey>>(
|
|
9683
|
+
event: U,
|
|
9684
|
+
listener: WSClientEventMap<TWSKey>[U],
|
|
9685
|
+
): this;
|
|
10378
9686
|
|
|
10379
9687
|
emit<U extends keyof WSClientEventMap<TWSKey>>(
|
|
10380
9688
|
event: U,
|
|
@@ -10844,6 +10152,763 @@ public async assertIsAuthenticated(wsKey: TWSKey): Promise<unknown>
|
|
|
10844
10152
|
⋮----
|
|
10845
10153
|
// Start authentication, it should automatically store/return a promise.
|
|
10846
10154
|
|
|
10155
|
+
================
|
|
10156
|
+
File: package.json
|
|
10157
|
+
================
|
|
10158
|
+
{
|
|
10159
|
+
"name": "@siebly/kraken-api",
|
|
10160
|
+
"version": "1.0.3",
|
|
10161
|
+
"description": "Complete & robust Node.js SDK for Kraken's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
|
|
10162
|
+
"scripts": {
|
|
10163
|
+
"clean": "rm -rf dist",
|
|
10164
|
+
"build": "npm run clean && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && bash ./postBuild.sh",
|
|
10165
|
+
"pack": "webpack --config webpack/webpack.config.cjs",
|
|
10166
|
+
"test": "jest --passWithNoTests",
|
|
10167
|
+
"lint": "eslint src"
|
|
10168
|
+
},
|
|
10169
|
+
"main": "dist/cjs/index.js",
|
|
10170
|
+
"module": "dist/mjs/index.js",
|
|
10171
|
+
"types": "dist/mjs/index.d.ts",
|
|
10172
|
+
"exports": {
|
|
10173
|
+
".": {
|
|
10174
|
+
"import": "./dist/mjs/index.js",
|
|
10175
|
+
"require": "./dist/cjs/index.js",
|
|
10176
|
+
"types": "./dist/mjs/index.d.ts"
|
|
10177
|
+
}
|
|
10178
|
+
},
|
|
10179
|
+
"type": "module",
|
|
10180
|
+
"files": [
|
|
10181
|
+
"dist/*",
|
|
10182
|
+
"llms.txt"
|
|
10183
|
+
],
|
|
10184
|
+
"author": "Siebly.io (https://github.com/sieblyio)",
|
|
10185
|
+
"contributors": [
|
|
10186
|
+
"Tiago Siebler (https://github.com/tiagosiebler)",
|
|
10187
|
+
"Jerko J (https://github.com/JJ-Cro)"
|
|
10188
|
+
],
|
|
10189
|
+
"dependencies": {
|
|
10190
|
+
"axios": "^1.10.0",
|
|
10191
|
+
"isomorphic-ws": "^5.0.0",
|
|
10192
|
+
"nanoid": "^3.3.11",
|
|
10193
|
+
"ws": "^8.18.3"
|
|
10194
|
+
},
|
|
10195
|
+
"devDependencies": {
|
|
10196
|
+
"@types/jest": "^29.5.12",
|
|
10197
|
+
"@types/node": "^22.11.6",
|
|
10198
|
+
"@types/ws": "^8.18.1",
|
|
10199
|
+
"@typescript-eslint/eslint-plugin": "^8.18.0",
|
|
10200
|
+
"@typescript-eslint/parser": "^8.18.0",
|
|
10201
|
+
"eslint": "^8.29.0",
|
|
10202
|
+
"eslint-config-prettier": "^9.1.0",
|
|
10203
|
+
"eslint-plugin-prettier": "^5.1.3",
|
|
10204
|
+
"eslint-plugin-require-extensions": "^0.1.3",
|
|
10205
|
+
"eslint-plugin-simple-import-sort": "^12.1.1",
|
|
10206
|
+
"jest": "^29.7.0",
|
|
10207
|
+
"prettier": "^3.3.3",
|
|
10208
|
+
"ts-jest": "^29.2.4",
|
|
10209
|
+
"ts-node": "^10.9.2",
|
|
10210
|
+
"typescript": "^5.7.3"
|
|
10211
|
+
},
|
|
10212
|
+
"optionalDependencies": {
|
|
10213
|
+
"webpack": "^5.0.0",
|
|
10214
|
+
"webpack-cli": "^4.0.0",
|
|
10215
|
+
"webpack-bundle-analyzer": "^4.10.2"
|
|
10216
|
+
},
|
|
10217
|
+
"keywords": [
|
|
10218
|
+
"kraken",
|
|
10219
|
+
"kraken api",
|
|
10220
|
+
"kraken nodejs",
|
|
10221
|
+
"kraken javascript",
|
|
10222
|
+
"kraken typescript",
|
|
10223
|
+
"kraken websocket api",
|
|
10224
|
+
"kraken websocket api javascript",
|
|
10225
|
+
"algo trading",
|
|
10226
|
+
"api",
|
|
10227
|
+
"websocket",
|
|
10228
|
+
"rest",
|
|
10229
|
+
"rest api",
|
|
10230
|
+
"usdt",
|
|
10231
|
+
"trading bots",
|
|
10232
|
+
"nodejs",
|
|
10233
|
+
"node",
|
|
10234
|
+
"trading",
|
|
10235
|
+
"cryptocurrency",
|
|
10236
|
+
"bitcoin",
|
|
10237
|
+
"best"
|
|
10238
|
+
],
|
|
10239
|
+
"funding": {
|
|
10240
|
+
"type": "individual",
|
|
10241
|
+
"url": "https://github.com/sponsors/tiagosiebler"
|
|
10242
|
+
},
|
|
10243
|
+
"license": "MIT",
|
|
10244
|
+
"repository": {
|
|
10245
|
+
"type": "git",
|
|
10246
|
+
"url": "https://github.com/sieblyio/kraken-api"
|
|
10247
|
+
},
|
|
10248
|
+
"bugs": {
|
|
10249
|
+
"url": "https://github.com/sieblyio/kraken-api/issues"
|
|
10250
|
+
},
|
|
10251
|
+
"homepage": "https://github.com/sieblyio/kraken-api#readme"
|
|
10252
|
+
}
|
|
10253
|
+
|
|
10254
|
+
================
|
|
10255
|
+
File: README.md
|
|
10256
|
+
================
|
|
10257
|
+
# Node.js & JavaScript SDK for Kraken REST APIs & WebSockets
|
|
10258
|
+
|
|
10259
|
+
[](https://github.com/sieblyio/kraken-api/actions/workflows/e2etest.yml)
|
|
10260
|
+
[][1]
|
|
10261
|
+
[][1]
|
|
10262
|
+
[][1]
|
|
10263
|
+
[][1]
|
|
10264
|
+
[](https://t.me/nodetraders)
|
|
10265
|
+
|
|
10266
|
+
<p align="center">
|
|
10267
|
+
<a href="https://www.npmjs.com/package/@siebly/kraken-api">
|
|
10268
|
+
<picture>
|
|
10269
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/sieblyio/kraken-api/blob/main/docs/images/logoDarkMode2.svg?raw=true#gh-dark-mode-only">
|
|
10270
|
+
<img alt="SDK Logo" src="https://github.com/sieblyio/kraken-api/blob/main/docs/images/logoBrightMode2.svg?raw=true#gh-light-mode-only">
|
|
10271
|
+
</picture>
|
|
10272
|
+
</a>
|
|
10273
|
+
</p>
|
|
10274
|
+
|
|
10275
|
+
[1]: https://www.npmjs.com/package/@siebly/kraken-api
|
|
10276
|
+
|
|
10277
|
+
Complete & robust JavaScript & Node.js SDK for the Kraken REST APIs and WebSockets:
|
|
10278
|
+
|
|
10279
|
+
- Professional, robust & performant Kraken SDK with extensive production use in live trading environments.
|
|
10280
|
+
- Complete integration with all Kraken REST APIs and WebSockets.
|
|
10281
|
+
- Dedicated REST clients for Spot, Derivatives (Futures), Institutional, and Partner operations
|
|
10282
|
+
- Unified WebSocket client for all markets
|
|
10283
|
+
- Complete TypeScript support (with type declarations for most API requests & responses).
|
|
10284
|
+
- Strongly typed requests and responses.
|
|
10285
|
+
- Automated end-to-end tests ensuring reliability.
|
|
10286
|
+
- Actively maintained with a modern, promise-driven interface.
|
|
10287
|
+
- Robust WebSocket integration with configurable connection heartbeats & automatic reconnect then resubscribe workflows.
|
|
10288
|
+
- Event driven messaging.
|
|
10289
|
+
- Smart WebSocket persistence with automatic reconnection handling.
|
|
10290
|
+
- Emit `reconnected` event when dropped connection is restored.
|
|
10291
|
+
- Support for both public and private WebSocket streams.
|
|
10292
|
+
- Browser-friendly HMAC signature mechanism.
|
|
10293
|
+
- Automatically supports both ESM and CJS projects.
|
|
10294
|
+
- Heavy automated end-to-end testing with real API calls.
|
|
10295
|
+
- Proxy support via axios integration.
|
|
10296
|
+
- Active community support & collaboration in telegram: [Node.js Algo Traders](https://t.me/nodetraders).
|
|
10297
|
+
|
|
10298
|
+
## Table of Contents
|
|
10299
|
+
|
|
10300
|
+
- [Installation](#installation)
|
|
10301
|
+
- [Examples](#examples)
|
|
10302
|
+
- [Issues & Discussion](#issues--discussion)
|
|
10303
|
+
- [Related Projects](#related-projects)
|
|
10304
|
+
- [Documentation](#documentation)
|
|
10305
|
+
- [Structure](#structure)
|
|
10306
|
+
- [Usage](#usage)
|
|
10307
|
+
- [REST API Clients](#rest-api)
|
|
10308
|
+
- [Spot Trading](#spot-trading)
|
|
10309
|
+
- [Derivatives (Futures) Trading](#derivatives-futures-trading)
|
|
10310
|
+
- [WebSockets](#websockets)
|
|
10311
|
+
- [Public WebSocket Streams](#public-websocket-streams)
|
|
10312
|
+
- [Private WebSocket Streams](#private-websocket-streams)
|
|
10313
|
+
- [WebSocket API (WebsocketAPIClient)](#websocket-api-websocketapiclient)
|
|
10314
|
+
- [Customise Logging](#customise-logging)
|
|
10315
|
+
- [Browser/Frontend Usage](#browserfrontend-usage)
|
|
10316
|
+
- [Webpack](#webpack)
|
|
10317
|
+
- [LLMs & AI](#use-with-llms--ai)
|
|
10318
|
+
- [Used By](#used-by)
|
|
10319
|
+
- [Contributions & Thanks](#contributions--thanks)
|
|
10320
|
+
|
|
10321
|
+
## Installation
|
|
10322
|
+
|
|
10323
|
+
`npm install --save @siebly/kraken-api`
|
|
10324
|
+
|
|
10325
|
+
## Examples
|
|
10326
|
+
|
|
10327
|
+
Refer to the [examples](./examples) folder for implementation demos, including:
|
|
10328
|
+
|
|
10329
|
+
- **Spot Trading Examples**: market data, account management, order placement
|
|
10330
|
+
- **Derivatives Trading Examples**: futures market data, account management, order placement
|
|
10331
|
+
- **WebSocket Examples**: public market data streams, private account data
|
|
10332
|
+
|
|
10333
|
+
## Issues & Discussion
|
|
10334
|
+
|
|
10335
|
+
- Issues? Check the [issues tab](https://github.com/sieblyio/kraken-api/issues).
|
|
10336
|
+
- Discuss & collaborate with other node devs? Join our [Node.js Algo Traders](https://t.me/nodetraders) engineering community on telegram.
|
|
10337
|
+
- Follow our announcement channel for real-time updates on [X/Twitter](https://x.com/sieblyio)
|
|
10338
|
+
|
|
10339
|
+
<!-- template_related_projects -->
|
|
10340
|
+
|
|
10341
|
+
## Related Projects
|
|
10342
|
+
|
|
10343
|
+
Check out our JavaScript/TypeScript/Node.js SDKs & Projects:
|
|
10344
|
+
|
|
10345
|
+
- Visit our website: [https://Siebly.io](https://siebly.io/?ref=gh)
|
|
10346
|
+
- Try our REST API & WebSocket SDKs published on npmjs:
|
|
10347
|
+
- [Bybit Node.js SDK: bybit-api](https://www.npmjs.com/package/bybit-api)
|
|
10348
|
+
- [Kraken Node.js SDK: @siebly/kraken-api](https://www.npmjs.com/package/@siebly/kraken-api)
|
|
10349
|
+
- [OKX Node.js SDK: okx-api](https://www.npmjs.com/package/okx-api)
|
|
10350
|
+
- [Binance Node.js SDK: binance](https://www.npmjs.com/package/binance)
|
|
10351
|
+
- [Gate (gate.com) Node.js SDK: gateio-api](https://www.npmjs.com/package/gateio-api)
|
|
10352
|
+
- [Bitget Node.js SDK: bitget-api](https://www.npmjs.com/package/bitget-api)
|
|
10353
|
+
- [Kucoin Node.js SDK: kucoin-api](https://www.npmjs.com/package/kucoin-api)
|
|
10354
|
+
- [Coinbase Node.js SDK: coinbase-api](https://www.npmjs.com/package/coinbase-api)
|
|
10355
|
+
- [Bitmart Node.js SDK: bitmart-api](https://www.npmjs.com/package/bitmart-api)
|
|
10356
|
+
- Try my misc utilities:
|
|
10357
|
+
- [OrderBooks Node.js: orderbooks](https://www.npmjs.com/package/orderbooks)
|
|
10358
|
+
- [Crypto Exchange Account State Cache: accountstate](https://www.npmjs.com/package/accountstate)
|
|
10359
|
+
- Check out my examples:
|
|
10360
|
+
- [awesome-crypto-examples Node.js](https://github.com/tiagosiebler/awesome-crypto-examples)
|
|
10361
|
+
<!-- template_related_projects_end -->
|
|
10362
|
+
|
|
10363
|
+
## Documentation
|
|
10364
|
+
|
|
10365
|
+
Most methods accept JS objects. These can be populated using parameters specified by Kraken's API documentation, or check the type definition in each class within this repository.
|
|
10366
|
+
|
|
10367
|
+
### API Documentation Links
|
|
10368
|
+
|
|
10369
|
+
- [Kraken API Documentation](https://docs.kraken.com/api/)
|
|
10370
|
+
- [Spot Trading API](https://docs.kraken.com/api/docs/rest-api/get-server-time)
|
|
10371
|
+
- [Futures Trading API](https://docs.futures.kraken.com/)
|
|
10372
|
+
|
|
10373
|
+
## Structure
|
|
10374
|
+
|
|
10375
|
+
This project uses typescript. Resources are stored in 2 key structures:
|
|
10376
|
+
|
|
10377
|
+
- [src](./src) - the whole connector written in typescript
|
|
10378
|
+
- [examples](./examples) - some implementation examples & demonstrations. Contributions are welcome!
|
|
10379
|
+
|
|
10380
|
+
---
|
|
10381
|
+
|
|
10382
|
+
# Usage
|
|
10383
|
+
|
|
10384
|
+
Create API credentials on Kraken's website:
|
|
10385
|
+
|
|
10386
|
+
- [Kraken API Key Management](https://www.kraken.com/u/security/api)
|
|
10387
|
+
- [Kraken Futures API Key Management](https://futures.kraken.com/settings/api)
|
|
10388
|
+
|
|
10389
|
+
## REST API
|
|
10390
|
+
|
|
10391
|
+
The SDK provides dedicated REST clients for different trading products:
|
|
10392
|
+
|
|
10393
|
+
- **SpotClient** - for spot trading, staking, and account operations
|
|
10394
|
+
- **DerivativesClient** - for futures trading operations
|
|
10395
|
+
- **InstitutionalClient** - for institutional trading and custody
|
|
10396
|
+
- **PartnerClient** - for partner and affiliate operations
|
|
10397
|
+
|
|
10398
|
+
### Spot Trading
|
|
10399
|
+
|
|
10400
|
+
To use Kraken's Spot APIs, import (or require) the `SpotClient`:
|
|
10401
|
+
|
|
10402
|
+
```javascript
|
|
10403
|
+
import { SpotClient } from '@siebly/kraken-api';
|
|
10404
|
+
// or if you prefer require:
|
|
10405
|
+
// const { SpotClient } = require('@siebly/kraken-api');
|
|
10406
|
+
|
|
10407
|
+
// For public endpoints, API credentials are optional
|
|
10408
|
+
const publicClient = new SpotClient();
|
|
10409
|
+
|
|
10410
|
+
// For private endpoints, provide API credentials
|
|
10411
|
+
const client = new SpotClient({
|
|
10412
|
+
apiKey: 'your-api-key',
|
|
10413
|
+
apiSecret: 'your-base64-encoded-private-key',
|
|
10414
|
+
});
|
|
10415
|
+
|
|
10416
|
+
// Public API Examples
|
|
10417
|
+
|
|
10418
|
+
// Get ticker information
|
|
10419
|
+
const ticker = await publicClient.getTicker({
|
|
10420
|
+
pair: 'XBTUSD',
|
|
10421
|
+
});
|
|
10422
|
+
console.log('Ticker: ', ticker);
|
|
10423
|
+
|
|
10424
|
+
// Get order book
|
|
10425
|
+
const orderBook = await publicClient.getOrderBook({
|
|
10426
|
+
pair: 'XBTUSD',
|
|
10427
|
+
count: 10,
|
|
10428
|
+
});
|
|
10429
|
+
console.log('Order Book: ', orderBook);
|
|
10430
|
+
|
|
10431
|
+
// Private API Examples (requires authentication)
|
|
10432
|
+
|
|
10433
|
+
// Submit a market order
|
|
10434
|
+
client
|
|
10435
|
+
.submitOrder({
|
|
10436
|
+
ordertype: 'market',
|
|
10437
|
+
type: 'buy',
|
|
10438
|
+
volume: '0.01',
|
|
10439
|
+
pair: 'XBTUSD',
|
|
10440
|
+
cl_ord_id: client.generateNewOrderID(),
|
|
10441
|
+
})
|
|
10442
|
+
.then((result) => {
|
|
10443
|
+
console.log('Market Order Result: ', result);
|
|
10444
|
+
})
|
|
10445
|
+
.catch((err) => {
|
|
10446
|
+
console.error('Error: ', err);
|
|
10447
|
+
});
|
|
10448
|
+
|
|
10449
|
+
// Submit a limit order
|
|
10450
|
+
client
|
|
10451
|
+
.submitOrder({
|
|
10452
|
+
ordertype: 'limit',
|
|
10453
|
+
type: 'buy',
|
|
10454
|
+
volume: '0.0001',
|
|
10455
|
+
pair: 'XBTUSD',
|
|
10456
|
+
price: '10000',
|
|
10457
|
+
cl_ord_id: client.generateNewOrderID(),
|
|
10458
|
+
})
|
|
10459
|
+
.then((result) => {
|
|
10460
|
+
console.log('Limit Order Result: ', result);
|
|
10461
|
+
})
|
|
10462
|
+
.catch((err) => {
|
|
10463
|
+
console.error('Error: ', err);
|
|
10464
|
+
});
|
|
10465
|
+
|
|
10466
|
+
// Submit batch of orders (minimum 2, maximum 15)
|
|
10467
|
+
client
|
|
10468
|
+
.submitBatchOrders({
|
|
10469
|
+
pair: 'XBTUSD',
|
|
10470
|
+
orders: [
|
|
10471
|
+
{
|
|
10472
|
+
ordertype: 'limit',
|
|
10473
|
+
type: 'buy',
|
|
10474
|
+
volume: '0.0001',
|
|
10475
|
+
price: '10000.00',
|
|
10476
|
+
timeinforce: 'GTC',
|
|
10477
|
+
cl_ord_id: client.generateNewOrderID(),
|
|
10478
|
+
},
|
|
10479
|
+
{
|
|
10480
|
+
ordertype: 'limit',
|
|
10481
|
+
type: 'buy',
|
|
10482
|
+
volume: '0.0001',
|
|
10483
|
+
price: '11111.00',
|
|
10484
|
+
timeinforce: 'GTC',
|
|
10485
|
+
cl_ord_id: client.generateNewOrderID(),
|
|
10486
|
+
},
|
|
10487
|
+
{
|
|
10488
|
+
ordertype: 'limit',
|
|
10489
|
+
type: 'sell',
|
|
10490
|
+
volume: '0.0001',
|
|
10491
|
+
price: '13000.00',
|
|
10492
|
+
timeinforce: 'GTC',
|
|
10493
|
+
cl_ord_id: client.generateNewOrderID(),
|
|
10494
|
+
},
|
|
10495
|
+
],
|
|
10496
|
+
})
|
|
10497
|
+
.then((result) => {
|
|
10498
|
+
console.log('Batch Order Result: ', JSON.stringify(result, null, 2));
|
|
10499
|
+
})
|
|
10500
|
+
.catch((err) => {
|
|
10501
|
+
console.error('Error: ', err);
|
|
10502
|
+
});
|
|
10503
|
+
|
|
10504
|
+
// Get account balances
|
|
10505
|
+
client
|
|
10506
|
+
.getAccountBalance()
|
|
10507
|
+
.then((balance) => {
|
|
10508
|
+
console.log('Account Balance: ', balance);
|
|
10509
|
+
})
|
|
10510
|
+
.catch((err) => {
|
|
10511
|
+
console.error('Error: ', err);
|
|
10512
|
+
});
|
|
10513
|
+
```
|
|
10514
|
+
|
|
10515
|
+
See [SpotClient](./src/SpotClient.ts) for further information, or the [examples](./examples/) for lots of usage examples.
|
|
10516
|
+
|
|
10517
|
+
### Derivatives (Futures) Trading
|
|
10518
|
+
|
|
10519
|
+
Use the `DerivativesClient` for futures trading operations:
|
|
10520
|
+
|
|
10521
|
+
```javascript
|
|
10522
|
+
import { DerivativesClient } from '@siebly/kraken-api';
|
|
10523
|
+
// or if you prefer require:
|
|
10524
|
+
// const { DerivativesClient } = require('@siebly/kraken-api');
|
|
10525
|
+
|
|
10526
|
+
// For public endpoints, API credentials are optional
|
|
10527
|
+
const publicClient = new DerivativesClient();
|
|
10528
|
+
|
|
10529
|
+
// For private endpoints, provide API credentials
|
|
10530
|
+
const client = new DerivativesClient({
|
|
10531
|
+
apiKey: 'your-api-key',
|
|
10532
|
+
apiSecret: 'your-api-secret',
|
|
10533
|
+
});
|
|
10534
|
+
|
|
10535
|
+
// Public API Examples
|
|
10536
|
+
|
|
10537
|
+
// Get order book for a specific instrument
|
|
10538
|
+
const orderBook = await publicClient.getOrderBook({
|
|
10539
|
+
symbol: 'PF_XBTUSD',
|
|
10540
|
+
});
|
|
10541
|
+
console.log('Futures Order Book: ', orderBook);
|
|
10542
|
+
|
|
10543
|
+
// Get ticker information
|
|
10544
|
+
const ticker = await publicClient.getTickers({
|
|
10545
|
+
symbol: 'PF_XBTUSD',
|
|
10546
|
+
});
|
|
10547
|
+
console.log('Futures Ticker: ', ticker);
|
|
10548
|
+
|
|
10549
|
+
// Private API Examples (requires authentication)
|
|
10550
|
+
|
|
10551
|
+
// Get account balances
|
|
10552
|
+
client
|
|
10553
|
+
.getAccountsDetails()
|
|
10554
|
+
.then((accounts) => {
|
|
10555
|
+
console.log('Accounts Details: ', accounts);
|
|
10556
|
+
})
|
|
10557
|
+
.catch((err) => {
|
|
10558
|
+
console.error('Error: ', err);
|
|
10559
|
+
});
|
|
10560
|
+
|
|
10561
|
+
// Submit a limit order
|
|
10562
|
+
client
|
|
10563
|
+
.submitOrder({
|
|
10564
|
+
orderType: 'lmt',
|
|
10565
|
+
symbol: 'PF_ETHUSD', // Perpetual ETH/USD
|
|
10566
|
+
side: 'buy',
|
|
10567
|
+
size: 0.01, // Contract size
|
|
10568
|
+
limitPrice: 1000,
|
|
10569
|
+
cliOrdId: client.generateNewOrderID(),
|
|
10570
|
+
})
|
|
10571
|
+
.then((result) => {
|
|
10572
|
+
console.log('Limit Order Result: ', JSON.stringify(result, null, 2));
|
|
10573
|
+
})
|
|
10574
|
+
.catch((err) => {
|
|
10575
|
+
console.error('Error: ', err);
|
|
10576
|
+
});
|
|
10577
|
+
```
|
|
10578
|
+
|
|
10579
|
+
See [DerivativesClient](./src/DerivativesClient.ts) for further information.
|
|
10580
|
+
|
|
10581
|
+
## WebSockets
|
|
10582
|
+
|
|
10583
|
+
Kraken supports two types of WebSocket connections:
|
|
10584
|
+
|
|
10585
|
+
1. **WebSocket Subscriptions** - Real-time market data and account updates via the `WebsocketClient`
|
|
10586
|
+
2. **WebSocket API** - REST-like request/response trading via the `WebsocketAPIClient`
|
|
10587
|
+
|
|
10588
|
+
### WebSocket Subscriptions (WebsocketClient)
|
|
10589
|
+
|
|
10590
|
+
The unified `WebsocketClient` handles all Kraken WebSocket streams with automatic connection management and reconnection.
|
|
10591
|
+
|
|
10592
|
+
Key WebSocket features:
|
|
10593
|
+
|
|
10594
|
+
- Event driven messaging
|
|
10595
|
+
- Smart WebSocket persistence with automatic reconnection
|
|
10596
|
+
- Heartbeat mechanisms to detect disconnections
|
|
10597
|
+
- Automatic resubscription after reconnection
|
|
10598
|
+
- Support for both Spot and Futures markets
|
|
10599
|
+
- Support for both public and private WebSocket streams
|
|
10600
|
+
|
|
10601
|
+
### Public WebSocket Streams
|
|
10602
|
+
|
|
10603
|
+
For public market data, API credentials are not required:
|
|
10604
|
+
|
|
10605
|
+
```javascript
|
|
10606
|
+
import { WebsocketClient } from '@siebly/kraken-api';
|
|
10607
|
+
// or if you prefer require:
|
|
10608
|
+
// const { WebsocketClient } = require('@siebly/kraken-api');
|
|
10609
|
+
// Create WebSocket client for public streams
|
|
10610
|
+
const wsClient = new WebsocketClient();
|
|
10611
|
+
|
|
10612
|
+
// Set up event handlers
|
|
10613
|
+
wsClient.on('open', (data) => {
|
|
10614
|
+
console.log('WebSocket connected: ', data?.wsKey);
|
|
10615
|
+
});
|
|
10616
|
+
|
|
10617
|
+
wsClient.on('message', (data) => {
|
|
10618
|
+
console.log('Data received: ', JSON.stringify(data, null, 2));
|
|
10619
|
+
});
|
|
10620
|
+
|
|
10621
|
+
wsClient.on('reconnected', (data) => {
|
|
10622
|
+
console.log('WebSocket reconnected: ', data);
|
|
10623
|
+
});
|
|
10624
|
+
|
|
10625
|
+
wsClient.on('exception', (data) => {
|
|
10626
|
+
console.error('WebSocket error: ', data);
|
|
10627
|
+
});
|
|
10628
|
+
|
|
10629
|
+
// Spot - Subscribe to public data streams
|
|
10630
|
+
wsClient.subscribe(
|
|
10631
|
+
{
|
|
10632
|
+
topic: 'ticker',
|
|
10633
|
+
payload: {
|
|
10634
|
+
symbol: ['BTC/USD', 'ETH/USD'],
|
|
10635
|
+
},
|
|
10636
|
+
},
|
|
10637
|
+
'spotPublicV2',
|
|
10638
|
+
);
|
|
10639
|
+
|
|
10640
|
+
wsClient.subscribe(
|
|
10641
|
+
{
|
|
10642
|
+
topic: 'book',
|
|
10643
|
+
payload: {
|
|
10644
|
+
symbol: ['BTC/USD'],
|
|
10645
|
+
depth: 10,
|
|
10646
|
+
},
|
|
10647
|
+
},
|
|
10648
|
+
'spotPublicV2',
|
|
10649
|
+
);
|
|
10650
|
+
|
|
10651
|
+
// Derivatives - Subscribe to public data streams
|
|
10652
|
+
wsClient.subscribe(
|
|
10653
|
+
{
|
|
10654
|
+
topic: 'ticker',
|
|
10655
|
+
payload: {
|
|
10656
|
+
product_ids: ['PI_XBTUSD', 'PI_ETHUSD'],
|
|
10657
|
+
},
|
|
10658
|
+
},
|
|
10659
|
+
'derivativesPublicV1',
|
|
10660
|
+
);
|
|
10661
|
+
|
|
10662
|
+
wsClient.subscribe(
|
|
10663
|
+
{
|
|
10664
|
+
topic: 'book',
|
|
10665
|
+
payload: {
|
|
10666
|
+
product_ids: ['PI_XBTUSD'],
|
|
10667
|
+
},
|
|
10668
|
+
},
|
|
10669
|
+
'derivativesPublicV1',
|
|
10670
|
+
);
|
|
10671
|
+
```
|
|
10672
|
+
|
|
10673
|
+
### Private WebSocket Streams
|
|
10674
|
+
|
|
10675
|
+
For private account data streams, API credentials are required:
|
|
10676
|
+
|
|
10677
|
+
```javascript
|
|
10678
|
+
import { WebsocketClient } from '@siebly/kraken-api';
|
|
10679
|
+
|
|
10680
|
+
// Create WebSocket client with API credentials for private streams
|
|
10681
|
+
const wsClient = new WebsocketClient({
|
|
10682
|
+
apiKey: 'your-api-key',
|
|
10683
|
+
apiSecret: 'your-api-secret',
|
|
10684
|
+
});
|
|
10685
|
+
|
|
10686
|
+
// Set up event handlers
|
|
10687
|
+
wsClient.on('open', (data) => {
|
|
10688
|
+
console.log('Private WebSocket connected: ', data?.wsKey);
|
|
10689
|
+
});
|
|
10690
|
+
|
|
10691
|
+
wsClient.on('message', (data) => {
|
|
10692
|
+
console.log('Private data received: ', JSON.stringify(data, null, 2));
|
|
10693
|
+
});
|
|
10694
|
+
|
|
10695
|
+
wsClient.on('authenticated', (data) => {
|
|
10696
|
+
console.log('WebSocket authenticated: ', data);
|
|
10697
|
+
});
|
|
10698
|
+
|
|
10699
|
+
wsClient.on('response', (data) => {
|
|
10700
|
+
console.log('WebSocket response: ', data);
|
|
10701
|
+
});
|
|
10702
|
+
|
|
10703
|
+
wsClient.on('exception', (data) => {
|
|
10704
|
+
console.error('WebSocket error: ', data);
|
|
10705
|
+
});
|
|
10706
|
+
|
|
10707
|
+
// Spot - Subscribe to private data streams
|
|
10708
|
+
wsClient.subscribe(
|
|
10709
|
+
{
|
|
10710
|
+
topic: 'executions',
|
|
10711
|
+
payload: {
|
|
10712
|
+
snap_trades: true,
|
|
10713
|
+
snap_orders: true,
|
|
10714
|
+
order_status: true,
|
|
10715
|
+
},
|
|
10716
|
+
},
|
|
10717
|
+
'spotPrivateV2',
|
|
10718
|
+
);
|
|
10719
|
+
|
|
10720
|
+
wsClient.subscribe(
|
|
10721
|
+
{
|
|
10722
|
+
topic: 'balances',
|
|
10723
|
+
payload: {
|
|
10724
|
+
snapshot: true,
|
|
10725
|
+
},
|
|
10726
|
+
},
|
|
10727
|
+
'spotPrivateV2',
|
|
10728
|
+
);
|
|
10729
|
+
|
|
10730
|
+
// Derivatives - Subscribe to private data streams
|
|
10731
|
+
// Note: SDK automatically handles authentication and challenge tokens
|
|
10732
|
+
wsClient.subscribe('open_orders', 'derivativesPrivateV1');
|
|
10733
|
+
|
|
10734
|
+
wsClient.subscribe(
|
|
10735
|
+
{
|
|
10736
|
+
topic: 'fills',
|
|
10737
|
+
payload: {
|
|
10738
|
+
product_ids: ['PF_XBTUSD'],
|
|
10739
|
+
},
|
|
10740
|
+
},
|
|
10741
|
+
'derivativesPrivateV1',
|
|
10742
|
+
);
|
|
10743
|
+
|
|
10744
|
+
wsClient.subscribe('balances', 'derivativesPrivateV1');
|
|
10745
|
+
|
|
10746
|
+
wsClient.subscribe('open_positions', 'derivativesPrivateV1');
|
|
10747
|
+
```
|
|
10748
|
+
|
|
10749
|
+
For more comprehensive examples, including custom logging and error handling, check the [examples](./examples/WebSockets) folder.
|
|
10750
|
+
|
|
10751
|
+
### WebSocket API (WebsocketAPIClient)
|
|
10752
|
+
|
|
10753
|
+
The `WebsocketAPIClient` provides a REST-like interface for trading operations over WebSocket, offering lower latency than REST APIs. Currently, only Spot trading is supported.
|
|
10754
|
+
|
|
10755
|
+
```javascript
|
|
10756
|
+
import { WebsocketAPIClient } from '@siebly/kraken-api';
|
|
10757
|
+
|
|
10758
|
+
// Create WebSocket API client with credentials
|
|
10759
|
+
const wsApiClient = new WebsocketAPIClient({
|
|
10760
|
+
apiKey: 'your-api-key',
|
|
10761
|
+
apiSecret: 'your-api-secret',
|
|
10762
|
+
});
|
|
10763
|
+
|
|
10764
|
+
// The client handles event listeners automatically, but you can customize them
|
|
10765
|
+
wsApiClient
|
|
10766
|
+
.getWSClient()
|
|
10767
|
+
.on('open', (data) => {
|
|
10768
|
+
console.log('WebSocket API connected:', data.wsKey);
|
|
10769
|
+
})
|
|
10770
|
+
.on('response', (data) => {
|
|
10771
|
+
console.log('Response:', data);
|
|
10772
|
+
})
|
|
10773
|
+
.on('exception', (data) => {
|
|
10774
|
+
console.error('Error:', data);
|
|
10775
|
+
});
|
|
10776
|
+
|
|
10777
|
+
// Trading operations return promises
|
|
10778
|
+
|
|
10779
|
+
// Submit a spot order
|
|
10780
|
+
const orderResponse = await wsApiClient.submitSpotOrder({
|
|
10781
|
+
order_type: 'limit',
|
|
10782
|
+
side: 'buy',
|
|
10783
|
+
limit_price: 26500.4,
|
|
10784
|
+
order_qty: 1.2,
|
|
10785
|
+
symbol: 'BTC/USD',
|
|
10786
|
+
});
|
|
10787
|
+
console.log('Order placed:', orderResponse);
|
|
10788
|
+
|
|
10789
|
+
// Amend an existing order
|
|
10790
|
+
const amendResponse = await wsApiClient.amendSpotOrder({
|
|
10791
|
+
order_id: 'OAIYAU-LGI3M-PFM5VW',
|
|
10792
|
+
order_qty: 1.5,
|
|
10793
|
+
limit_price: 27000,
|
|
10794
|
+
});
|
|
10795
|
+
|
|
10796
|
+
// Cancel specific orders
|
|
10797
|
+
const cancelResponse = await wsApiClient.cancelSpotOrder({
|
|
10798
|
+
order_id: ['OM5CRX-N2HAL-GFGWE9', 'OLUMT4-UTEGU-ZYM7E9'],
|
|
10799
|
+
});
|
|
10800
|
+
|
|
10801
|
+
// Cancel all open orders
|
|
10802
|
+
const cancelAllResponse = await wsApiClient.cancelAllSpotOrders();
|
|
10803
|
+
```
|
|
10804
|
+
|
|
10805
|
+
The WebSocket API provides several advantages:
|
|
10806
|
+
|
|
10807
|
+
- **Lower Latency** - Faster than REST API for high-frequency trading
|
|
10808
|
+
- **Connection Reuse** - Single persistent connection for multiple requests
|
|
10809
|
+
- **Better Performance** - Batch operations for submitting/canceling multiple orders
|
|
10810
|
+
- **Type Safety** - Full TypeScript support with typed requests and responses
|
|
10811
|
+
|
|
10812
|
+
See the [WebSocket API examples](./examples/WebSockets/Spot/wsAPI.ts) for more detailed usage.
|
|
10813
|
+
|
|
10814
|
+
---
|
|
10815
|
+
|
|
10816
|
+
## Customise Logging
|
|
10817
|
+
|
|
10818
|
+
Pass a custom logger which supports the log methods `trace`, `info` and `error`, or override methods from the default logger as desired.
|
|
10819
|
+
|
|
10820
|
+
```javascript
|
|
10821
|
+
import { WebsocketClient, DefaultLogger } from '@siebly/kraken-api';
|
|
10822
|
+
|
|
10823
|
+
// E.g. customise logging for only the trace level:
|
|
10824
|
+
const customLogger: DefaultLogger = {
|
|
10825
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
10826
|
+
trace: (...params: LogParams): void => {
|
|
10827
|
+
// console.log('trace', ...params);
|
|
10828
|
+
},
|
|
10829
|
+
info: (...params: LogParams): void => {
|
|
10830
|
+
console.log('info', ...params);
|
|
10831
|
+
},
|
|
10832
|
+
error: (...params: LogParams): void => {
|
|
10833
|
+
console.error('error', ...params);
|
|
10834
|
+
},
|
|
10835
|
+
};
|
|
10836
|
+
|
|
10837
|
+
const ws = new WebsocketClient(
|
|
10838
|
+
{
|
|
10839
|
+
apiKey: 'apiKeyHere',
|
|
10840
|
+
apiSecret: 'apiSecretHere',
|
|
10841
|
+
},
|
|
10842
|
+
customLogger,
|
|
10843
|
+
);
|
|
10844
|
+
```
|
|
10845
|
+
|
|
10846
|
+
## Browser/Frontend Usage
|
|
10847
|
+
|
|
10848
|
+
### Webpack
|
|
10849
|
+
|
|
10850
|
+
Build a bundle using webpack:
|
|
10851
|
+
|
|
10852
|
+
- `npm install`
|
|
10853
|
+
- `npm run build`
|
|
10854
|
+
- `npm run pack`
|
|
10855
|
+
|
|
10856
|
+
The bundle can be found in `dist/`. Altough usage should be largely consistent, smaller differences will exist. Documentation is still TODO.
|
|
10857
|
+
|
|
10858
|
+
## Use with LLMs & AI
|
|
10859
|
+
|
|
10860
|
+
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.
|
|
10861
|
+
|
|
10862
|
+
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.
|
|
10863
|
+
|
|
10864
|
+
---
|
|
10865
|
+
|
|
10866
|
+
## Used By
|
|
10867
|
+
|
|
10868
|
+
[](https://github.com/sieblyio/kraken-api/network/dependents)
|
|
10869
|
+
|
|
10870
|
+
---
|
|
10871
|
+
|
|
10872
|
+
<!-- template_contributions -->
|
|
10873
|
+
|
|
10874
|
+
### Contributions & Thanks
|
|
10875
|
+
|
|
10876
|
+
Have my projects helped you? Share the love, there are many ways you can show your thanks:
|
|
10877
|
+
|
|
10878
|
+
- Star & share my projects.
|
|
10879
|
+
- Are my projects useful? Sponsor me on Github and support my effort to maintain & improve them: https://github.com/sponsors/tiagosiebler
|
|
10880
|
+
- Have an interesting project? Get in touch & invite me to it.
|
|
10881
|
+
- Or buy me all the coffee:
|
|
10882
|
+
- ETH(ERC20): `0xA3Bda8BecaB4DCdA539Dc16F9C54a592553Be06C` <!-- metamask -->
|
|
10883
|
+
- Sign up with my referral links:
|
|
10884
|
+
- OKX (receive a 20% fee discount!): https://www.okx.com/join/42013004
|
|
10885
|
+
- Binance (receive a 20% fee discount!): https://accounts.binance.com/register?ref=OKFFGIJJ
|
|
10886
|
+
- HyperLiquid (receive a 4% fee discount!): https://app.hyperliquid.xyz/join/SDK
|
|
10887
|
+
- Gate: https://www.gate.io/signup/NODESDKS?ref_type=103
|
|
10888
|
+
|
|
10889
|
+
<!---
|
|
10890
|
+
old ones:
|
|
10891
|
+
- BTC: `1C6GWZL1XW3jrjpPTS863XtZiXL1aTK7Jk`
|
|
10892
|
+
- BTC(SegWit): `bc1ql64wr9z3khp2gy7dqlmqw7cp6h0lcusz0zjtls`
|
|
10893
|
+
- ETH(ERC20): `0xe0bbbc805e0e83341fadc210d6202f4022e50992`
|
|
10894
|
+
- USDT(TRC20): `TA18VUywcNEM9ahh3TTWF3sFpt9rkLnnQa
|
|
10895
|
+
- gate: https://www.gate.io/signup/AVNNU1WK?ref_type=103
|
|
10896
|
+
|
|
10897
|
+
-->
|
|
10898
|
+
<!-- template_contributions_end -->
|
|
10899
|
+
|
|
10900
|
+
### Contributions & Pull Requests
|
|
10901
|
+
|
|
10902
|
+
Contributions are encouraged, I will review any incoming pull requests. See the issues tab for todo items.
|
|
10903
|
+
|
|
10904
|
+
<!-- template_star_history -->
|
|
10905
|
+
|
|
10906
|
+
## Star History
|
|
10907
|
+
|
|
10908
|
+
[](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)
|
|
10909
|
+
|
|
10910
|
+
<!-- template_star_history_end -->
|
|
10911
|
+
|
|
10847
10912
|
================
|
|
10848
10913
|
File: src/DerivativesClient.ts
|
|
10849
10914
|
================
|
|
@@ -11770,6 +11835,8 @@ getAccountMarketShare(): Promise<FuturesMarketShare>
|
|
|
11770
11835
|
File: src/lib/BaseRestClient.ts
|
|
11771
11836
|
================
|
|
11772
11837
|
import axios, { AxiosRequestConfig, AxiosResponse, Method } from 'axios';
|
|
11838
|
+
// NOTE: https.Agent is Node.js-only and not available in browser environments
|
|
11839
|
+
// Browser builds (via webpack) exclude this module - see webpack.config.js fallback settings
|
|
11773
11840
|
import https from 'https';
|
|
11774
11841
|
⋮----
|
|
11775
11842
|
import { neverGuard } from './misc-util.js';
|
|
@@ -11870,6 +11937,9 @@ constructor(
|
|
|
11870
11937
|
/** inject custom request options based on axios specs - see axios docs for more guidance on AxiosRequestConfig: https://github.com/axios/axios#request-config */
|
|
11871
11938
|
⋮----
|
|
11872
11939
|
// If enabled, configure a https agent with keepAlive enabled
|
|
11940
|
+
// NOTE: This is Node.js-only functionality. In browser environments, this code is skipped
|
|
11941
|
+
// as the 'https' module is excluded via webpack fallback configuration.
|
|
11942
|
+
// Browser connection pooling is handled automatically by the browser itself.
|
|
11873
11943
|
⋮----
|
|
11874
11944
|
// Extract existing https agent parameters, if provided, to prevent the keepAlive flag from overwriting an existing https agent completely
|
|
11875
11945
|
⋮----
|
|
@@ -12169,12 +12239,6 @@ public subscribe(
|
|
|
12169
12239
|
wsKey: WsKey,
|
|
12170
12240
|
)
|
|
12171
12241
|
⋮----
|
|
12172
|
-
// Automatically route level3 subscriptions to the L3 endpoint
|
|
12173
|
-
⋮----
|
|
12174
|
-
// Subscribe level3 topics to the L3 endpoint
|
|
12175
|
-
⋮----
|
|
12176
|
-
// Subscribe other topics to the original wsKey
|
|
12177
|
-
⋮----
|
|
12178
12242
|
/**
|
|
12179
12243
|
* Unsubscribe from one or more topics. Similar to subscribe() but in reverse.
|
|
12180
12244
|
*
|
|
@@ -12188,12 +12252,6 @@ public unsubscribe(
|
|
|
12188
12252
|
wsKey: WsKey,
|
|
12189
12253
|
)
|
|
12190
12254
|
⋮----
|
|
12191
|
-
// Automatically route level3 unsubscriptions to the L3 endpoint
|
|
12192
|
-
⋮----
|
|
12193
|
-
// Unsubscribe level3 topics from the L3 endpoint
|
|
12194
|
-
⋮----
|
|
12195
|
-
// Unsubscribe other topics from the original wsKey
|
|
12196
|
-
⋮----
|
|
12197
12255
|
/**
|
|
12198
12256
|
* WS API Methods - similar to the REST API, but via WebSockets
|
|
12199
12257
|
*/
|