Skip to content

Commit 5ad98d5

Browse files
committed
feat: add basic functionality
1 parent 932431d commit 5ad98d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+9693
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
response/**/snapshot

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v22

LICENSE

+674
Large diffs are not rendered by default.

README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# API Tools
2+
3+
Use appropriate Node.js version:
4+
```bash
5+
nvm use
6+
```
7+
8+
General syntax of commands:
9+
```bash
10+
node <api> <handler>[ <params>][ <key=value>][ <flag>]
11+
```
12+
13+
Full list of commands, params and options depends on implementation.
14+
15+
## Request
16+
17+
To run a scenario related to request processing of an API, change directory:
18+
```bash
19+
cd request
20+
```
21+
22+
Example of commands for Bybit API:
23+
```bash
24+
node bybit.mjs
25+
node bybit.mjs orderAll
26+
node bybit.mjs order 1234567890123456789
27+
node bybit.mjs order 1234567890123456789 symbol=BTC
28+
node bybit.mjs order 1234567890123456789 symbol=BTC --snapshot
29+
```
30+
31+
32+
## Response
33+
34+
To run a scenario related to response processing of an API, change directory:
35+
```bash
36+
cd response
37+
```
38+
39+
Example of commands for Bybit API:
40+
```bash
41+
node bybit.mjs
42+
node bybit.mjs orderAll
43+
node bybit.mjs order 1234567890123456789
44+
node bybit.mjs order 1234567890123456789 symbol=BTC
45+
node bybit.mjs order 1234567890123456789 symbol=BTC --snapshot
46+
```

coinbase/jwt_get.mjs

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Sign GET request with JSON Web Token for Coinbase Advanced API and send.
3+
*
4+
* @see https://nodejs.org/api/crypto.html#cryptocreatehmacalgorithm-key-options
5+
* @see https://nodejs.org/api/crypto.html#hmacdigestencoding
6+
* @see https://nodejs.org/api/crypto.html#hmacupdatedata-inputencoding
7+
* @module coinbase/jwt_get
8+
*/
9+
10+
import crypto from "node:crypto";
11+
import jsonwebtoken from "jsonwebtoken";
12+
13+
const data = {
14+
order_status: "OPEN",
15+
product_id: "SOL-USDT"
16+
},
17+
hostname = "api.coinbase.com",
18+
key = "********",
19+
method = "GET",
20+
path = "/api/v3/brokerage/orders/historical/batch",
21+
query = "?" + String(new URLSearchParams(data)),
22+
secret = "********",
23+
timestamp = Date.now(),
24+
url = "https://" + hostname + path + query,
25+
nonce = crypto.randomBytes(16).toString("hex"),
26+
payload = {
27+
exp: Math.floor(timestamp / 1_000) + 120,
28+
iss: "cdp",
29+
nbf: Math.floor(timestamp / 1_000),
30+
sub: key,
31+
uri: method + " " + hostname + path,
32+
},
33+
token = jsonwebtoken.sign(
34+
payload,
35+
secret,
36+
{
37+
algorithm: "ES256",
38+
header: {
39+
kid: key,
40+
nonce,
41+
},
42+
}
43+
);
44+
45+
console.log("Fetching\u2026");
46+
fetch(url, {
47+
headers: {
48+
Authorization: "Bearer " + token,
49+
},
50+
method,
51+
})
52+
.then((response) => response.text())
53+
.then((text) =>
54+
{
55+
console.log("request:", {
56+
data,
57+
hostname,
58+
key,
59+
method,
60+
nonce,
61+
path,
62+
payload,
63+
query,
64+
secret,
65+
timestamp,
66+
token,
67+
url
68+
});
69+
console.dir(JSON.parse(text), { depth: null })
70+
}
71+
);

coinbase/jwt_post.mjs

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Sign POST request with JSON Web Token for Coinbase Advanced API and send.
3+
*
4+
* @see https://docs.cdp.coinbase.com/advanced-trade/docs/rest-api-auth
5+
* @see https://docs.cdp.coinbase.com/advanced-trade/reference/retailbrokerageapi_postorder
6+
* @module coinbase/fetch_post
7+
*/
8+
9+
import crypto from "node:crypto";
10+
import jsonwebtoken from "jsonwebtoken";
11+
12+
const data = {
13+
client_order_id: crypto.randomUUID(),
14+
order_configuration: {
15+
limit_limit_gtc: {
16+
base_size: "0.01",
17+
limit_price: "1000"
18+
}
19+
},
20+
product_id: "BTC-USDT",
21+
retail_portfolio_id: "00000000-0000-000000000-000000000000",
22+
side: "BUY"
23+
},
24+
body = JSON.stringify(data),
25+
hostname = "api.coinbase.com",
26+
key = "********",
27+
method = "POST",
28+
path = "/api/v3/brokerage/orders",
29+
secret = "********",
30+
timestamp = Date.now(),
31+
nonce = crypto.randomBytes(16).toString("hex"),
32+
payload = {
33+
exp: Math.floor(timestamp / 1_000) + 120,
34+
iss: "cdp",
35+
nbf: Math.floor(timestamp / 1_000),
36+
sub: key,
37+
uri: method + " " + hostname + path,
38+
},
39+
token = jsonwebtoken.sign(
40+
payload,
41+
secret,
42+
{
43+
algorithm: "ES256",
44+
header: {
45+
kid: key,
46+
nonce,
47+
},
48+
}
49+
);
50+
51+
fetch("https://" + hostname + path, {
52+
body,
53+
headers: {
54+
Authorization: "Bearer " + token,
55+
"Content-Type": "application/json",
56+
},
57+
method,
58+
})
59+
.then((response) => response.text())
60+
.then((text) =>
61+
{
62+
console.log("request:", {
63+
body,
64+
data,
65+
hostname,
66+
key,
67+
method,
68+
nonce,
69+
path,
70+
payload,
71+
secret,
72+
timestamp,
73+
token,
74+
});
75+
console.dir(JSON.parse(text), { depth: null })
76+
}
77+
);

coinbase/products.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const axios = require('axios');
4+
5+
let config = {
6+
method: 'get',
7+
maxBodyLength: Infinity,
8+
url: 'https://api.coinbase.com/api/v3/brokerage/products',
9+
headers: {
10+
'Content-Type': 'application/json'
11+
}
12+
};
13+
14+
axios.request(config)
15+
.then((response) => {
16+
console.log(JSON.stringify(response.data));
17+
})
18+
.catch((error) => {
19+
console.log(error);
20+
});

0 commit comments

Comments
 (0)