@binance/derivatives-trading-options 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/LICENCE +21 -0
- package/README.md +271 -0
- package/dist/index.d.mts +9634 -0
- package/dist/index.d.ts +9634 -0
- package/dist/index.js +4448 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +4452 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +52 -0
package/LICENCE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Binance
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# Binance JavaScript Derivatives Trading (Options) Connector
|
|
2
|
+
|
|
3
|
+
[](https://github.com/binance/binance-connector-js/issues)
|
|
4
|
+
[](https://prettier.io/)
|
|
5
|
+
[](https://badge.fury.io/js/@binance%2Fderivatives-trading-options)
|
|
6
|
+
[](https://www.npmjs.com/package/@binance/derivatives-trading-options)
|
|
7
|
+

|
|
8
|
+
[](https://snyk.io/test/github/binance/binance-connector-js)
|
|
9
|
+
[](https://opensource.org/licenses/MIT)
|
|
10
|
+
|
|
11
|
+
This is a client library for the Binance Derivatives Trading Options API, enabling developers to interact programmatically with Binance's API to suit their derivative trading needs, through three distinct endpoints:
|
|
12
|
+
|
|
13
|
+
- [REST API](./src/rest-api/rest-api.ts)
|
|
14
|
+
- [Websocket Stream](./src/websocket-streams/websocket-streams-connection.ts)
|
|
15
|
+
|
|
16
|
+
## Table of Contents
|
|
17
|
+
|
|
18
|
+
- [Supported Features](#supported-features)
|
|
19
|
+
- [Installation](#installation)
|
|
20
|
+
- [Documentation](#documentation)
|
|
21
|
+
- [REST APIs](#rest-apis)
|
|
22
|
+
- [Websocket Streams](#websocket-streams)
|
|
23
|
+
- [Testing](#testing)
|
|
24
|
+
- [Migration Guide](#migration-guide)
|
|
25
|
+
- [Contributing](#contributing)
|
|
26
|
+
- [Licence](#licence)
|
|
27
|
+
|
|
28
|
+
## Supported Features
|
|
29
|
+
|
|
30
|
+
- REST API Endpoints:
|
|
31
|
+
- `/eapi/*`
|
|
32
|
+
- WebSocket Endpoints: Real-time data streaming.
|
|
33
|
+
- Inclusion of test cases and examples for quick onboarding.
|
|
34
|
+
|
|
35
|
+
## Installation
|
|
36
|
+
|
|
37
|
+
To use this library, ensure your environment is running Node.js version **22.12.0** or later. If you're using `nvm` (Node Version Manager), you can set the correct version as follows:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
nvm install 22.12.0
|
|
41
|
+
nvm use 22.12.0
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Then install the library using `npm`:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install @binance/derivatives-trading-options
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Documentation
|
|
51
|
+
|
|
52
|
+
For detailed information, refer to the [Binance API Documentation](https://developers.binance.com/docs/derivatives/option/general-info).
|
|
53
|
+
|
|
54
|
+
### REST APIs
|
|
55
|
+
|
|
56
|
+
All REST API endpoints are available through the [`rest-api`](./src/rest-api/rest-api.ts) module. Note that some endpoints require authentication using your Binance API credentials.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { DerivativesTradingOptions } from '@binance/derivatives-trading-options';
|
|
60
|
+
|
|
61
|
+
const configurationRestAPI = {
|
|
62
|
+
apiKey: 'your-api-key',
|
|
63
|
+
apiSecret: 'your-api-secret',
|
|
64
|
+
};
|
|
65
|
+
const client = new DerivativesTradingOptions({ configurationRestAPI });
|
|
66
|
+
|
|
67
|
+
client.restAPI
|
|
68
|
+
.optionAccountInformation()
|
|
69
|
+
.then((res) => res.data())
|
|
70
|
+
.then((data) => console.log(data))
|
|
71
|
+
.catch((err) => console.error(err));
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
More examples can be found in the [`examples/rest-api`](./examples/rest-api/) folder.
|
|
75
|
+
|
|
76
|
+
#### Configuration Options
|
|
77
|
+
|
|
78
|
+
The REST API supports the following advanced configuration options:
|
|
79
|
+
|
|
80
|
+
- `timeout`: Timeout for requests in milliseconds (default: 1000 ms).
|
|
81
|
+
- `proxy`: Proxy configuration:
|
|
82
|
+
- `host`: Proxy server hostname.
|
|
83
|
+
- `port`: Proxy server port.
|
|
84
|
+
- `protocol`: Proxy protocol (http or https).
|
|
85
|
+
- `auth`: Proxy authentication credentials:
|
|
86
|
+
- `username`: Proxy username.
|
|
87
|
+
- `password`: Proxy password.
|
|
88
|
+
- `keepAlive`: Enable HTTP keep-alive (default: true).
|
|
89
|
+
- `compression`: Enable response compression (default: true).
|
|
90
|
+
- `retries`: Number of retry attempts for failed requests (default: 3).
|
|
91
|
+
- `backoff`: Delay in milliseconds between retries (default: 1000 ms).
|
|
92
|
+
- `httpsAgent`: Custom HTTPS agent for advanced TLS configuration.
|
|
93
|
+
- `privateKey`: RSA or ED25519 private key for authentication.
|
|
94
|
+
- `privateKeyPassphrase`: Passphrase for the private key, if encrypted.
|
|
95
|
+
|
|
96
|
+
##### Timeout
|
|
97
|
+
|
|
98
|
+
You can configure a timeout for requests in milliseconds. If the request exceeds the specified timeout, it will be aborted. See the [Timeout example](./docs/rest-api/timeout.md) for detailed usage.
|
|
99
|
+
|
|
100
|
+
##### Proxy
|
|
101
|
+
|
|
102
|
+
The REST API supports HTTP/HTTPS proxy configurations. See the [Proxy example](./docs/rest-api/proxy.md) for detailed usage.
|
|
103
|
+
|
|
104
|
+
##### Keep-Alive
|
|
105
|
+
|
|
106
|
+
Enable HTTP keep-alive for persistent connections. See the [Keep-Alive example](./docs/rest-api/keepAlive.md) for detailed usage.
|
|
107
|
+
|
|
108
|
+
##### Compression
|
|
109
|
+
|
|
110
|
+
Enable or disable response compression. See the [Compression example](./docs/rest-api/compression.md) for detailed usage.
|
|
111
|
+
|
|
112
|
+
##### Retries
|
|
113
|
+
|
|
114
|
+
Configure the number of retry attempts and delay in milliseconds between retries for failed requests. See the [Retries example](./docs/rest-api/retries.md) for detailed usage.
|
|
115
|
+
|
|
116
|
+
##### HTTPS Agent
|
|
117
|
+
|
|
118
|
+
Customize the HTTPS agent for advanced TLS configurations. See the [HTTPS Agent example](./docs/rest-api/httpsAgent.md) for detailed usage.
|
|
119
|
+
|
|
120
|
+
##### Key Pair Based Authentication
|
|
121
|
+
|
|
122
|
+
The REST API supports key pair-based authentication for secure communication. You can use `RSA` or `ED25519` keys for signing requests. See the [Key Pair Based Authentication example](./docs/rest-api/key-pair-authentication.md) for detailed usage.
|
|
123
|
+
|
|
124
|
+
##### Certificate Pinning
|
|
125
|
+
|
|
126
|
+
To enhance security, you can use certificate pinning with the `httpsAgent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/rest-api/certificate-pinning.md) for detailed usage.
|
|
127
|
+
|
|
128
|
+
#### Error Handling
|
|
129
|
+
|
|
130
|
+
The REST API provides detailed error types to help you handle issues effectively:
|
|
131
|
+
|
|
132
|
+
- `ConnectorClientError`: General client error.
|
|
133
|
+
- `RequiredError`: Thrown when a required parameter is missing.
|
|
134
|
+
- `UnauthorizedError`: Indicates missing or invalid authentication credentials.
|
|
135
|
+
- `ForbiddenError`: Access to the requested resource is forbidden.
|
|
136
|
+
- `TooManyRequestsError`: Rate limit exceeded.
|
|
137
|
+
- `RateLimitBanError`: IP address banned for exceeding rate limits.
|
|
138
|
+
- `ServerError`: Internal server error.
|
|
139
|
+
- `NetworkError`: Issues with network connectivity.
|
|
140
|
+
- `NotFoundError`: Resource not found.
|
|
141
|
+
- `BadRequestError`: Invalid request.
|
|
142
|
+
|
|
143
|
+
See the [Error Handling example](./docs/rest-api/error-handling.md) for detailed usage.
|
|
144
|
+
|
|
145
|
+
If `basePath` is not provided, it defaults to `https://eapi.binance.com`.
|
|
146
|
+
|
|
147
|
+
### Websocket Streams
|
|
148
|
+
|
|
149
|
+
WebSocket Streams provide real-time data feeds for market trades, candlesticks, and more. Use the [websocket-streams](./src/websocket-streams/websocket-streams.ts) module to subscribe to these streams.
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
import { DerivativesTradingOptions, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-options';
|
|
153
|
+
|
|
154
|
+
const configurationWebsocketStreams = {
|
|
155
|
+
wsURL: DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL,
|
|
156
|
+
};
|
|
157
|
+
const client = new DerivativesTradingOptions({ configurationWebsocketStreams });
|
|
158
|
+
|
|
159
|
+
client.websocketStreams
|
|
160
|
+
.connect()
|
|
161
|
+
.then((connection) => {
|
|
162
|
+
const stream = connection.newSymbolInfo();
|
|
163
|
+
stream.on('message', (data) => console.info(data));
|
|
164
|
+
})
|
|
165
|
+
.catch((err) => console.error(err));
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
More examples are available in the [`examples/websocket-streams`](./examples/websocket-streams/) folder.
|
|
169
|
+
|
|
170
|
+
#### Configuration Options
|
|
171
|
+
|
|
172
|
+
The WebSocket Streams API supports the following advanced configuration options:
|
|
173
|
+
|
|
174
|
+
- `reconnectDelay`: Specify the delay between reconnection attempts (default: 5000 ms).
|
|
175
|
+
- `compression`: Enable or disable compression for WebSocket messages (default: true).
|
|
176
|
+
- `agent`: Customize the WebSocket agent for advanced configurations.
|
|
177
|
+
- `mode`: Choose between `single` and `pool` connection modes.
|
|
178
|
+
- `single`: A single WebSocket connection.
|
|
179
|
+
- `pool`: A pool of WebSocket connections.
|
|
180
|
+
- `poolSize`: Define the number of WebSocket connections in pool mode.
|
|
181
|
+
|
|
182
|
+
##### Reconnect Delay
|
|
183
|
+
|
|
184
|
+
Specify the delay in milliseconds between WebSocket reconnection attempts for streams. See the [Reconnect Delay example](./docs/websocket-streams/reconnect-delay.md) for detailed usage.
|
|
185
|
+
|
|
186
|
+
##### Compression
|
|
187
|
+
|
|
188
|
+
Enable or disable compression for WebSocket Streams messages. See the [Compression example](./docs/websocket-streams/compression.md) for detailed usage.
|
|
189
|
+
|
|
190
|
+
##### WebSocket Agent
|
|
191
|
+
|
|
192
|
+
Customize the agent for advanced configurations. See the [WebSocket Agent example](./docs/websocket-streams/agent.md) for detailed usage.
|
|
193
|
+
|
|
194
|
+
##### Connection Mode
|
|
195
|
+
|
|
196
|
+
Choose between `single` and `pool` connection modes for WebSocket Streams. The `single` mode uses a single WebSocket connection, while the `pool` mode uses a pool of WebSocket connections. See the [Connection Mode example](./docs/websocket-streams/connection-mode.md) for detailed usage.
|
|
197
|
+
|
|
198
|
+
##### Certificate Pinning
|
|
199
|
+
|
|
200
|
+
To enhance security, you can use certificate pinning with the `agent` option in the configuration. This ensures the client only communicates with servers using specific certificates. See the [Certificate Pinning example](./docs/websocket-streams/certificate-pinning.md) for detailed usage.
|
|
201
|
+
|
|
202
|
+
#### Unsubscribing from Streams
|
|
203
|
+
|
|
204
|
+
You can unsubscribe from specific WebSocket streams using the `unsubscribe` method. This is useful for managing active subscriptions without closing the connection.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
import { DerivativesTradingOptions, DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL } from '@binance/derivatives-trading-options';
|
|
208
|
+
|
|
209
|
+
const configurationWebsocketStreams = {
|
|
210
|
+
wsURL: DERIVATIVES_TRADING_OPTIONS_WS_STREAMS_PROD_URL,
|
|
211
|
+
};
|
|
212
|
+
const client = new DerivativesTradingOptions({ configurationWebsocketStreams });
|
|
213
|
+
|
|
214
|
+
client.websocketStreams
|
|
215
|
+
.connect()
|
|
216
|
+
.then((connection) => {
|
|
217
|
+
const stream = connection.newSymbolInfo();
|
|
218
|
+
stream.on('message', (data) => console.info(data));
|
|
219
|
+
|
|
220
|
+
setTimeout(() => {
|
|
221
|
+
stream.unsubscribe();
|
|
222
|
+
console.log('Unsubscribed from newSymbolInfo stream');
|
|
223
|
+
}, 10000);
|
|
224
|
+
})
|
|
225
|
+
.catch((err) => console.error(err));
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
If `wsURL` is not provided, it defaults to `wss://nbstream.binance.com/eoptions`.
|
|
229
|
+
|
|
230
|
+
### Automatic Connection Renewal
|
|
231
|
+
|
|
232
|
+
The WebSocket connection is automatically renewed for both WebSocket API and WebSocket Streams connections, before the 24 hours expiration of the API key. This ensures continuous connectivity.
|
|
233
|
+
|
|
234
|
+
## Testing
|
|
235
|
+
|
|
236
|
+
To run the tests:
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
npm install
|
|
240
|
+
|
|
241
|
+
npm run test
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The tests cover:
|
|
245
|
+
|
|
246
|
+
- REST API endpoints
|
|
247
|
+
- WebSocket Streams
|
|
248
|
+
- Error handling and edge cases
|
|
249
|
+
|
|
250
|
+
## Migration Guide
|
|
251
|
+
|
|
252
|
+
If you are upgrading to the new modularized structure, refer to the [Migration Guide](../../MIGRATION.md) for detailed steps.
|
|
253
|
+
|
|
254
|
+
## Contributing
|
|
255
|
+
|
|
256
|
+
Contributions are welcome!
|
|
257
|
+
|
|
258
|
+
Since this repository contains auto-generated code, we encourage you to start by opening a GitHub issue to discuss your ideas or suggest improvements. This helps ensure that changes align with the project's goals and auto-generation processes.
|
|
259
|
+
|
|
260
|
+
To contribute:
|
|
261
|
+
|
|
262
|
+
1. Open a GitHub issue describing your suggestion or the bug you've identified.
|
|
263
|
+
2. If it's determined that changes are necessary, the maintainers will merge the changes into the main branch.
|
|
264
|
+
|
|
265
|
+
Please ensure that all tests pass if you're making a direct contribution. Submit a pull request only after discussing and confirming the change.
|
|
266
|
+
|
|
267
|
+
Thank you for your contributions!
|
|
268
|
+
|
|
269
|
+
## Licence
|
|
270
|
+
|
|
271
|
+
This project is licensed under the MIT License. See the [LICENCE](./LICENCE) file for details.
|