klingex 1.0.0
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 +310 -0
- package/dist/index.d.mts +895 -0
- package/dist/index.d.ts +895 -0
- package/dist/index.js +1098 -0
- package/dist/index.mjs +1061 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
# KlingEx JavaScript/TypeScript SDK
|
|
2
|
+
|
|
3
|
+
Official JavaScript/TypeScript SDK for the [KlingEx](https://klingex.io) cryptocurrency exchange API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install klingex
|
|
9
|
+
# or
|
|
10
|
+
yarn add klingex
|
|
11
|
+
# or
|
|
12
|
+
pnpm add klingex
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { KlingEx } from 'klingex';
|
|
19
|
+
|
|
20
|
+
// Initialize client with API key
|
|
21
|
+
const client = new KlingEx({
|
|
22
|
+
apiKey: 'your-api-key-here'
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Get all markets
|
|
26
|
+
const markets = await client.markets.list();
|
|
27
|
+
console.log(markets);
|
|
28
|
+
|
|
29
|
+
// Get your balances
|
|
30
|
+
const balances = await client.wallet.balances();
|
|
31
|
+
console.log(balances);
|
|
32
|
+
|
|
33
|
+
// Place a limit order (human-readable values by default)
|
|
34
|
+
const order = await client.orders.submit({
|
|
35
|
+
symbol: 'BTC-USDT',
|
|
36
|
+
tradingPairId: 1,
|
|
37
|
+
side: 'buy',
|
|
38
|
+
quantity: '0.5', // 0.5 BTC
|
|
39
|
+
price: '50000.00' // $50,000 per BTC
|
|
40
|
+
});
|
|
41
|
+
console.log(`Order placed: ${order.order_id}`);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Configuration
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
const client = new KlingEx({
|
|
48
|
+
// Authentication (choose one)
|
|
49
|
+
apiKey: 'your-api-key', // API key authentication
|
|
50
|
+
// jwt: 'your-jwt-token', // Or JWT authentication
|
|
51
|
+
|
|
52
|
+
// Optional settings
|
|
53
|
+
baseUrl: 'https://api.klingex.io', // API base URL
|
|
54
|
+
wsUrl: 'wss://api.klingex.io/ws', // WebSocket URL
|
|
55
|
+
timeout: 30000, // Request timeout (ms)
|
|
56
|
+
humanReadable: true, // Use human-readable values (default: true)
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## API Reference
|
|
61
|
+
|
|
62
|
+
### Markets
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Get all trading pairs
|
|
66
|
+
const markets = await client.markets.list();
|
|
67
|
+
for (const market of markets.slice(0, 5)) {
|
|
68
|
+
console.log(`${market.base_asset_symbol}-${market.quote_asset_symbol}: ${market.last_price}`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Get 24h tickers (CMC format - uses underscore "BTC_USDT")
|
|
72
|
+
const tickers = await client.markets.tickers();
|
|
73
|
+
const btcTicker = await client.markets.ticker('BTC_USDT');
|
|
74
|
+
console.log(`BTC_USDT: ${btcTicker?.last_price}`);
|
|
75
|
+
|
|
76
|
+
// Get orderbook (marketId is a number)
|
|
77
|
+
const orderbook = await client.markets.orderbook(1);
|
|
78
|
+
console.log('Best bid:', orderbook.bids[0]);
|
|
79
|
+
console.log('Best ask:', orderbook.asks[0]);
|
|
80
|
+
|
|
81
|
+
// Get OHLCV (candlestick) data
|
|
82
|
+
const candles = await client.markets.ohlcv(1, '1h', { limit: 100 });
|
|
83
|
+
|
|
84
|
+
// Get recent trades (marketId is a number)
|
|
85
|
+
const trades = await client.markets.trades(1);
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Orders
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// Place a limit order
|
|
92
|
+
const order = await client.orders.submit({
|
|
93
|
+
symbol: 'BTC-USDT',
|
|
94
|
+
tradingPairId: 1,
|
|
95
|
+
side: 'buy',
|
|
96
|
+
quantity: '1.0',
|
|
97
|
+
price: '50000.00'
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Convenience methods
|
|
101
|
+
await client.orders.limitBuy('BTC-USDT', 1, '1.0', '50000.00');
|
|
102
|
+
await client.orders.limitSell('BTC-USDT', 1, '1.0', '55000.00');
|
|
103
|
+
await client.orders.marketBuy('BTC-USDT', 1, '1.0', 0.01); // 1% slippage
|
|
104
|
+
await client.orders.marketSell('BTC-USDT', 1, '1.0', 0.01);
|
|
105
|
+
|
|
106
|
+
// Get open orders
|
|
107
|
+
const orders = await client.orders.list();
|
|
108
|
+
const btcOrders = await client.orders.list({ tradingPairId: 1 });
|
|
109
|
+
|
|
110
|
+
// Cancel an order
|
|
111
|
+
await client.orders.cancel({
|
|
112
|
+
orderId: 'order-uuid',
|
|
113
|
+
tradingPairId: 1
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Cancel all orders for a trading pair
|
|
117
|
+
const result = await client.orders.cancelAll(1);
|
|
118
|
+
console.log(`Cancelled ${result.cancelledCount} orders`);
|
|
119
|
+
|
|
120
|
+
// Get order history
|
|
121
|
+
const history = await client.orders.history({ limit: 100 });
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Raw Values vs Human-Readable
|
|
125
|
+
|
|
126
|
+
By default, the SDK uses human-readable values (e.g., `"1.5"` for 1.5 BTC). To use raw base units:
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
// Human-readable (default)
|
|
130
|
+
await client.orders.submit({
|
|
131
|
+
symbol: 'BTC-USDT',
|
|
132
|
+
tradingPairId: 1,
|
|
133
|
+
side: 'buy',
|
|
134
|
+
quantity: '1.5', // 1.5 BTC
|
|
135
|
+
price: '50000.00', // $50,000
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
// Raw base units
|
|
139
|
+
await client.orders.submit({
|
|
140
|
+
symbol: 'BTC-USDT',
|
|
141
|
+
tradingPairId: 1,
|
|
142
|
+
side: 'buy',
|
|
143
|
+
quantity: '150000000', // 1.5 BTC in satoshis (8 decimals)
|
|
144
|
+
price: '5000000000', // $50,000 in base units
|
|
145
|
+
rawValues: true
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Or configure globally
|
|
149
|
+
const client = new KlingEx({
|
|
150
|
+
apiKey: 'your-key',
|
|
151
|
+
humanReadable: false // Use raw values by default
|
|
152
|
+
});
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
### Wallet
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
// Get all balances
|
|
159
|
+
const balances = await client.wallet.balances();
|
|
160
|
+
|
|
161
|
+
// Get specific balance
|
|
162
|
+
const btc = await client.wallet.balance('BTC');
|
|
163
|
+
console.log(`Available: ${btc?.human_available} BTC`);
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Invoices (Payment Processing)
|
|
167
|
+
|
|
168
|
+
```typescript
|
|
169
|
+
// Create invoice
|
|
170
|
+
const invoice = await client.invoices.create({
|
|
171
|
+
amount: '100.00',
|
|
172
|
+
asset: 'USDT',
|
|
173
|
+
description: 'Order #12345',
|
|
174
|
+
webhook_url: 'https://yoursite.com/webhook',
|
|
175
|
+
expires_in: 60 // minutes
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
console.log(`Payment address: ${invoice.payment_address}`);
|
|
179
|
+
console.log(`Invoice URL: https://klingex.io/pay/${invoice.id}`);
|
|
180
|
+
|
|
181
|
+
// List invoices
|
|
182
|
+
const invoices = await client.invoices.list({ status: 'pending' });
|
|
183
|
+
|
|
184
|
+
// Check status
|
|
185
|
+
const status = await client.invoices.status(invoice.id);
|
|
186
|
+
|
|
187
|
+
// Cancel
|
|
188
|
+
await client.invoices.cancel(invoice.id);
|
|
189
|
+
|
|
190
|
+
// Get PDF
|
|
191
|
+
const pdf = await client.invoices.pdf(invoice.id);
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### WebSocket (Real-time Data)
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
// Connect to WebSocket
|
|
198
|
+
await client.ws.connect();
|
|
199
|
+
|
|
200
|
+
// Subscribe to orderbook updates
|
|
201
|
+
const unsubOrderbook = client.ws.orderbook('BTC-USDT', (data) => {
|
|
202
|
+
console.log('Orderbook update:', data);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
// Subscribe to trades
|
|
206
|
+
const unsubTrades = client.ws.trades('BTC-USDT', (data) => {
|
|
207
|
+
console.log('New trade:', data);
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
// Subscribe to ticker
|
|
211
|
+
client.ws.ticker('BTC-USDT', (ticker) => {
|
|
212
|
+
console.log(`Price: ${ticker.last_price}`);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// User-specific channels (requires auth)
|
|
216
|
+
client.ws.userOrders((order) => {
|
|
217
|
+
console.log('Order update:', order);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
client.ws.userBalances((balance) => {
|
|
221
|
+
console.log('Balance update:', balance);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// Handle errors
|
|
225
|
+
client.ws.onError((error) => {
|
|
226
|
+
console.error('WebSocket error:', error);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
// Unsubscribe
|
|
230
|
+
unsubOrderbook();
|
|
231
|
+
unsubTrades();
|
|
232
|
+
|
|
233
|
+
// Disconnect
|
|
234
|
+
client.ws.disconnect();
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Error Handling
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
import {
|
|
241
|
+
KlingEx,
|
|
242
|
+
KlingExError,
|
|
243
|
+
AuthenticationError,
|
|
244
|
+
RateLimitError,
|
|
245
|
+
ValidationError,
|
|
246
|
+
InsufficientFundsError
|
|
247
|
+
} from 'klingex';
|
|
248
|
+
|
|
249
|
+
try {
|
|
250
|
+
await client.orders.submit({ /* ... */ });
|
|
251
|
+
} catch (error) {
|
|
252
|
+
if (error instanceof AuthenticationError) {
|
|
253
|
+
console.error('Invalid API key');
|
|
254
|
+
} else if (error instanceof RateLimitError) {
|
|
255
|
+
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
|
|
256
|
+
} else if (error instanceof InsufficientFundsError) {
|
|
257
|
+
console.error('Not enough balance');
|
|
258
|
+
} else if (error instanceof ValidationError) {
|
|
259
|
+
console.error('Invalid parameters:', error.details);
|
|
260
|
+
} else if (error instanceof KlingExError) {
|
|
261
|
+
console.error(`API error: ${error.message} (${error.code})`);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## TypeScript Support
|
|
267
|
+
|
|
268
|
+
Full TypeScript support with type definitions included:
|
|
269
|
+
|
|
270
|
+
```typescript
|
|
271
|
+
import type {
|
|
272
|
+
Market,
|
|
273
|
+
Order,
|
|
274
|
+
Balance,
|
|
275
|
+
Ticker,
|
|
276
|
+
Orderbook,
|
|
277
|
+
SubmitOrderParams
|
|
278
|
+
} from 'klingex';
|
|
279
|
+
|
|
280
|
+
const params: SubmitOrderParams = {
|
|
281
|
+
symbol: 'BTC-USDT',
|
|
282
|
+
tradingPairId: 1,
|
|
283
|
+
side: 'buy',
|
|
284
|
+
quantity: '1.0',
|
|
285
|
+
price: '50000.00'
|
|
286
|
+
};
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## Browser Support
|
|
290
|
+
|
|
291
|
+
The SDK works in both Node.js and browsers. For browsers, make sure you have a WebSocket polyfill if needed.
|
|
292
|
+
|
|
293
|
+
```html
|
|
294
|
+
<script type="module">
|
|
295
|
+
import { KlingEx } from 'https://unpkg.com/klingex/dist/index.mjs';
|
|
296
|
+
|
|
297
|
+
const client = new KlingEx({ apiKey: 'your-key' });
|
|
298
|
+
const markets = await client.markets.list();
|
|
299
|
+
</script>
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## License
|
|
303
|
+
|
|
304
|
+
MIT
|
|
305
|
+
|
|
306
|
+
## Support
|
|
307
|
+
|
|
308
|
+
- Documentation: https://klingex.io/support/api-docs
|
|
309
|
+
- Issues: https://github.com/Klingon-tech/klingex-js/issues
|
|
310
|
+
- Email: support@klingex.io
|