Skip to content

Commit 06a7456

Browse files
committed
chore(): updated examples, updated readme
1 parent c6a2148 commit 06a7456

4 files changed

Lines changed: 59 additions & 10 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,48 @@ Create API credentials at okx
104104
- If the response looks successful (HTTP 200 and "code" in the response body === "0"), only the `data` property is directly (without the `code`, `data` & `msg` properties).
105105
- If the response looks like an error (HTTP error OR the "code" property in the response does not equal "0"), the full response is thrown (including `code` and `msg` properties). See the interface for [APIResponse<T>](./src/types/rest/shared.ts).
106106

107+
### Example
108+
109+
```ts
110+
import { RestClient } from 'okx-api';
111+
112+
const client = new RestClient({
113+
apiKey: 'apiKeyHere',
114+
apiSecret: 'apiSecretHere',
115+
apiPass: 'apiPassHere',
116+
});
117+
118+
// Submit a buy and sell market order
119+
(async () => {
120+
try {
121+
const allBalances = await client.getBalance();
122+
console.log('All balances: ', allBalances);
123+
124+
const buyResult = await client.submitOrder({
125+
instId: 'BTC-USDT',
126+
ordType: 'market',
127+
side: 'buy',
128+
sz: '0.1',
129+
tdMode: 'cash',
130+
tgtCcy: 'base_ccy',
131+
});
132+
console.log('buy order result: ', buyResult);
133+
134+
const sellResult = await client.submitOrder({
135+
instId: 'BTC-USDT',
136+
ordType: 'market',
137+
side: 'sell',
138+
sz: '0.1',
139+
tdMode: 'cash',
140+
tgtCcy: 'base_ccy',
141+
});
142+
console.log('Sell order result: ', sellResult);
143+
} catch (e) {
144+
console.error('request failed: ', e);
145+
}
146+
})();
147+
```
148+
107149
## Websocket Client
108150

109151
This connector includes a high-performance node.js & typescript websocket client for the OKX public & private websockets.

examples/rest-private-trade-market.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ const API_KEY = process.env.API_KEY_COM;
88
const API_SECRET = process.env.API_SECRET_COM;
99
const API_PASS = process.env.API_PASSPHRASE_COM;
1010

11-
/**
12-
* Execute this script using ts-node. The above environmental variables can be passed in one command (unix & macOS):
13-
*
14-
* API_KEY_COM="yourapikey" API_SECRET_COM="yourapisecret" API_PASSPHRASE_COM="yourapipassphrase" ts-node examples/rest-private-trade-market.ts
15-
*
16-
* If you don't have ts-node, install it using npm: https://github.com/TypeStrong/ts-node#installation
17-
*/
11+
// If running from CLI in unix, you can pass env vars as such:
12+
// API_KEY_COM=‘lkm12n3-2ba3-1mxf-fn13-lkm12n3a’ API_SECRET_COM='035B2B9637E1BDFFEE2646BFBDDB8CE4' API_PASSPHRASE_COM='ComplexPa$$!23$5^' ts-node examples/rest-private-trade-market.ts
13+
14+
// note the single quotes, preventing special characters such as $ from being incorrectly passed
1815

1916
if (!API_KEY || !API_SECRET || !API_PASS) {
2017
throw new Error(

examples/rest-private-trade.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ const API_KEY = process.env.API_KEY_COM;
99
const API_SECRET = process.env.API_SECRET_COM;
1010
const API_PASS = process.env.API_PASS_COM;
1111

12+
// If running from CLI in unix, you can pass env vars as such:
13+
// API_KEY_COM=‘lkm12n3-2ba3-1mxf-fn13-lkm12n3a’ API_SECRET_COM='035B2B9637E1BDFFEE2646BFBDDB8CE4' API_PASSPHRASE_COM='ComplexPa$$!23$5^' ts-node examples/rest-private-trade.ts
14+
15+
// note the single quotes, preventing special characters such as $ from being incorrectly passed
16+
1217
if (!API_KEY || !API_SECRET || !API_PASS) {
1318
throw new Error(
14-
`Missing api credentials. Use environmental variables or hard code in the script`
19+
'Missing api credentials. Use environmental variables or hard code in the script',
1520
);
1621
}
1722

@@ -48,7 +53,7 @@ function promiseSleep(milliseconds) {
4853
// WARNING: for sensitive math you should be using a library such as decimal.js!
4954
function roundDown(value, decimals) {
5055
return Number(
51-
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals
56+
Math.floor(parseFloat(value + 'e' + decimals)) + 'e-' + decimals,
5257
);
5358
}
5459

@@ -74,7 +79,7 @@ function roundDown(value, decimals) {
7479
const allBalances = await client.getBalance();
7580
// const balances = allBalances.filter((bal) => Number(bal.available) != 0);
7681
const usdtBalanceResult = allBalances[0].details.find(
77-
(bal) => bal.ccy === 'USDT'
82+
(bal) => bal.ccy === 'USDT',
7883
);
7984
console.log('BTC balance result: ', usdtBalanceResult);
8085

examples/ws-private.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const API_KEY = process.env.API_KEY_COM;
1919
const API_SECRET = process.env.API_SECRET_COM;
2020
const API_PASSPHRASE = process.env.API_PASSPHRASE_COM;
2121

22+
// If running from CLI in unix, you can pass env vars as such:
23+
// API_KEY_COM=‘lkm12n3-2ba3-1mxf-fn13-lkm12n3a’ API_SECRET_COM='035B2B9637E1BDFFEE2646BFBDDB8CE4' API_PASSPHRASE_COM='ComplexPa$$!23$5^' ts-node examples/ws-private.ts
24+
25+
// note the single quotes, preventing special characters such as $ from being incorrectly passed
26+
2227
if (!API_KEY) {
2328
throw new Error('API_KEY is missing');
2429
}

0 commit comments

Comments
 (0)