@bitgen/sdk 0.1.5 → 1.0.3
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/CHANGELOG.md +55 -0
- package/README.md +27 -106
- package/dist/cjs/client.d.ts +20 -8
- package/dist/cjs/client.d.ts.map +1 -1
- package/dist/cjs/client.js +33 -12
- package/dist/cjs/client.js.map +1 -1
- package/dist/cjs/constants.d.ts +256 -0
- package/dist/cjs/constants.d.ts.map +1 -0
- package/dist/cjs/constants.js +214 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/error.d.ts +17 -10
- package/dist/cjs/error.d.ts.map +1 -1
- package/dist/cjs/error.js +25 -7
- package/dist/cjs/error.js.map +1 -1
- package/dist/cjs/http.d.ts +13 -8
- package/dist/cjs/http.d.ts.map +1 -1
- package/dist/cjs/http.js +135 -43
- package/dist/cjs/http.js.map +1 -1
- package/dist/cjs/index.d.ts +14 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +30 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/resources/apikeys.d.ts +18 -0
- package/dist/cjs/resources/apikeys.d.ts.map +1 -0
- package/dist/cjs/resources/apikeys.js +31 -0
- package/dist/cjs/resources/apikeys.js.map +1 -0
- package/dist/cjs/resources/asset.d.ts +17 -5
- package/dist/cjs/resources/asset.d.ts.map +1 -1
- package/dist/cjs/resources/asset.js +17 -6
- package/dist/cjs/resources/asset.js.map +1 -1
- package/dist/cjs/resources/bank.d.ts +11 -6
- package/dist/cjs/resources/bank.d.ts.map +1 -1
- package/dist/cjs/resources/bank.js +17 -9
- package/dist/cjs/resources/bank.js.map +1 -1
- package/dist/cjs/resources/core.d.ts +12 -0
- package/dist/cjs/resources/core.d.ts.map +1 -0
- package/dist/cjs/resources/core.js +22 -0
- package/dist/cjs/resources/core.js.map +1 -0
- package/dist/cjs/resources/custody.d.ts +27 -0
- package/dist/cjs/resources/custody.d.ts.map +1 -0
- package/dist/cjs/resources/custody.js +38 -0
- package/dist/cjs/resources/custody.js.map +1 -0
- package/dist/cjs/resources/customer.d.ts +20 -0
- package/dist/cjs/resources/customer.d.ts.map +1 -0
- package/dist/cjs/resources/customer.js +38 -0
- package/dist/cjs/resources/customer.js.map +1 -0
- package/dist/cjs/resources/staking.d.ts +32 -0
- package/dist/cjs/resources/staking.d.ts.map +1 -0
- package/dist/cjs/resources/staking.js +69 -0
- package/dist/cjs/resources/staking.js.map +1 -0
- package/dist/cjs/resources/trading.d.ts +24 -0
- package/dist/cjs/resources/trading.d.ts.map +1 -0
- package/dist/cjs/resources/trading.js +41 -0
- package/dist/cjs/resources/trading.js.map +1 -0
- package/dist/cjs/resources/transaction.d.ts +6 -9
- package/dist/cjs/resources/transaction.d.ts.map +1 -1
- package/dist/cjs/resources/transaction.js +10 -15
- package/dist/cjs/resources/transaction.js.map +1 -1
- package/dist/cjs/resources/webhooks.d.ts +46 -0
- package/dist/cjs/resources/webhooks.d.ts.map +1 -0
- package/dist/cjs/resources/webhooks.js +142 -0
- package/dist/cjs/resources/webhooks.js.map +1 -0
- package/dist/cjs/types/apikeys.d.ts +43 -0
- package/dist/cjs/types/apikeys.d.ts.map +1 -0
- package/dist/cjs/types/apikeys.js +3 -0
- package/dist/cjs/types/apikeys.js.map +1 -0
- package/dist/cjs/types/asset.d.ts +57 -0
- package/dist/cjs/types/asset.d.ts.map +1 -0
- package/dist/cjs/types/asset.js +3 -0
- package/dist/cjs/types/asset.js.map +1 -0
- package/dist/cjs/types/bank.d.ts +58 -0
- package/dist/cjs/types/bank.d.ts.map +1 -0
- package/dist/cjs/types/bank.js +3 -0
- package/dist/cjs/types/bank.js.map +1 -0
- package/dist/cjs/types/core.d.ts +32 -0
- package/dist/cjs/types/core.d.ts.map +1 -0
- package/dist/cjs/types/core.js +3 -0
- package/dist/cjs/types/core.js.map +1 -0
- package/dist/cjs/types/custody.d.ts +50 -0
- package/dist/cjs/types/custody.d.ts.map +1 -0
- package/dist/cjs/types/custody.js +3 -0
- package/dist/cjs/types/custody.js.map +1 -0
- package/dist/cjs/types/customer.d.ts +190 -0
- package/dist/cjs/types/customer.d.ts.map +1 -0
- package/dist/cjs/types/customer.js +3 -0
- package/dist/cjs/types/customer.js.map +1 -0
- package/dist/cjs/types/index.d.ts +79 -194
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/index.js +0 -1
- package/dist/cjs/types/index.js.map +1 -1
- package/dist/cjs/types/staking.d.ts +88 -0
- package/dist/cjs/types/staking.d.ts.map +1 -0
- package/dist/cjs/types/staking.js +3 -0
- package/dist/cjs/types/staking.js.map +1 -0
- package/dist/cjs/types/trading.d.ts +51 -0
- package/dist/cjs/types/trading.d.ts.map +1 -0
- package/dist/cjs/types/trading.js +3 -0
- package/dist/cjs/types/trading.js.map +1 -0
- package/dist/cjs/types/transaction.d.ts +61 -0
- package/dist/cjs/types/transaction.d.ts.map +1 -0
- package/dist/cjs/types/transaction.js +3 -0
- package/dist/cjs/types/transaction.js.map +1 -0
- package/dist/cjs/types/webhooks.d.ts +55 -0
- package/dist/cjs/types/webhooks.d.ts.map +1 -0
- package/dist/cjs/types/webhooks.js +3 -0
- package/dist/cjs/types/webhooks.js.map +1 -0
- package/dist/cjs/utils.d.ts +21 -2
- package/dist/cjs/utils.d.ts.map +1 -1
- package/dist/cjs/utils.js +67 -4
- package/dist/cjs/utils.js.map +1 -1
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.d.ts.map +1 -0
- package/dist/cjs/version.js +6 -0
- package/dist/cjs/version.js.map +1 -0
- package/dist/esm/client.d.ts +20 -8
- package/dist/esm/client.d.ts.map +1 -1
- package/dist/esm/client.js +33 -12
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/constants.d.ts +256 -0
- package/dist/esm/constants.d.ts.map +1 -0
- package/dist/esm/constants.js +211 -0
- package/dist/esm/constants.js.map +1 -0
- package/dist/esm/error.d.ts +17 -10
- package/dist/esm/error.d.ts.map +1 -1
- package/dist/esm/error.js +24 -6
- package/dist/esm/error.js.map +1 -1
- package/dist/esm/http.d.ts +13 -8
- package/dist/esm/http.d.ts.map +1 -1
- package/dist/esm/http.js +136 -44
- package/dist/esm/http.js.map +1 -1
- package/dist/esm/index.d.ts +14 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/resources/apikeys.d.ts +18 -0
- package/dist/esm/resources/apikeys.d.ts.map +1 -0
- package/dist/esm/resources/apikeys.js +27 -0
- package/dist/esm/resources/apikeys.js.map +1 -0
- package/dist/esm/resources/asset.d.ts +17 -5
- package/dist/esm/resources/asset.d.ts.map +1 -1
- package/dist/esm/resources/asset.js +17 -6
- package/dist/esm/resources/asset.js.map +1 -1
- package/dist/esm/resources/bank.d.ts +11 -6
- package/dist/esm/resources/bank.d.ts.map +1 -1
- package/dist/esm/resources/bank.js +18 -10
- package/dist/esm/resources/bank.js.map +1 -1
- package/dist/esm/resources/core.d.ts +12 -0
- package/dist/esm/resources/core.d.ts.map +1 -0
- package/dist/esm/resources/core.js +18 -0
- package/dist/esm/resources/core.js.map +1 -0
- package/dist/esm/resources/custody.d.ts +27 -0
- package/dist/esm/resources/custody.d.ts.map +1 -0
- package/dist/esm/resources/custody.js +34 -0
- package/dist/esm/resources/custody.js.map +1 -0
- package/dist/esm/resources/customer.d.ts +20 -0
- package/dist/esm/resources/customer.d.ts.map +1 -0
- package/dist/esm/resources/customer.js +34 -0
- package/dist/esm/resources/customer.js.map +1 -0
- package/dist/esm/resources/staking.d.ts +32 -0
- package/dist/esm/resources/staking.d.ts.map +1 -0
- package/dist/esm/resources/staking.js +65 -0
- package/dist/esm/resources/staking.js.map +1 -0
- package/dist/esm/resources/trading.d.ts +24 -0
- package/dist/esm/resources/trading.d.ts.map +1 -0
- package/dist/esm/resources/trading.js +37 -0
- package/dist/esm/resources/trading.js.map +1 -0
- package/dist/esm/resources/transaction.d.ts +6 -9
- package/dist/esm/resources/transaction.d.ts.map +1 -1
- package/dist/esm/resources/transaction.js +11 -16
- package/dist/esm/resources/transaction.js.map +1 -1
- package/dist/esm/resources/webhooks.d.ts +46 -0
- package/dist/esm/resources/webhooks.d.ts.map +1 -0
- package/dist/esm/resources/webhooks.js +138 -0
- package/dist/esm/resources/webhooks.js.map +1 -0
- package/dist/esm/types/apikeys.d.ts +43 -0
- package/dist/esm/types/apikeys.d.ts.map +1 -0
- package/dist/esm/types/apikeys.js +2 -0
- package/dist/esm/types/apikeys.js.map +1 -0
- package/dist/esm/types/asset.d.ts +57 -0
- package/dist/esm/types/asset.d.ts.map +1 -0
- package/dist/esm/types/asset.js +2 -0
- package/dist/esm/types/asset.js.map +1 -0
- package/dist/esm/types/bank.d.ts +58 -0
- package/dist/esm/types/bank.d.ts.map +1 -0
- package/dist/esm/types/bank.js +2 -0
- package/dist/esm/types/bank.js.map +1 -0
- package/dist/esm/types/core.d.ts +32 -0
- package/dist/esm/types/core.d.ts.map +1 -0
- package/dist/esm/types/core.js +2 -0
- package/dist/esm/types/core.js.map +1 -0
- package/dist/esm/types/custody.d.ts +50 -0
- package/dist/esm/types/custody.d.ts.map +1 -0
- package/dist/esm/types/custody.js +2 -0
- package/dist/esm/types/custody.js.map +1 -0
- package/dist/esm/types/customer.d.ts +190 -0
- package/dist/esm/types/customer.d.ts.map +1 -0
- package/dist/esm/types/customer.js +2 -0
- package/dist/esm/types/customer.js.map +1 -0
- package/dist/esm/types/index.d.ts +79 -194
- package/dist/esm/types/index.d.ts.map +1 -1
- package/dist/esm/types/index.js +0 -1
- package/dist/esm/types/index.js.map +1 -1
- package/dist/esm/types/staking.d.ts +88 -0
- package/dist/esm/types/staking.d.ts.map +1 -0
- package/dist/esm/types/staking.js +2 -0
- package/dist/esm/types/staking.js.map +1 -0
- package/dist/esm/types/trading.d.ts +51 -0
- package/dist/esm/types/trading.d.ts.map +1 -0
- package/dist/esm/types/trading.js +2 -0
- package/dist/esm/types/trading.js.map +1 -0
- package/dist/esm/types/transaction.d.ts +61 -0
- package/dist/esm/types/transaction.d.ts.map +1 -0
- package/dist/esm/types/transaction.js +2 -0
- package/dist/esm/types/transaction.js.map +1 -0
- package/dist/esm/types/webhooks.d.ts +55 -0
- package/dist/esm/types/webhooks.d.ts.map +1 -0
- package/dist/esm/types/webhooks.js +2 -0
- package/dist/esm/types/webhooks.js.map +1 -0
- package/dist/esm/utils.d.ts +21 -2
- package/dist/esm/utils.d.ts.map +1 -1
- package/dist/esm/utils.js +62 -4
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/package.json +29 -12
- package/readme/concepts.md +104 -0
- package/readme/configuration.md +72 -0
- package/readme/errors.md +67 -0
- package/readme/installation.md +43 -0
- package/readme/quick-start.md +87 -0
- package/readme/resource/apikeys.md +103 -0
- package/readme/resource/asset.md +121 -0
- package/readme/resource/bank.md +156 -0
- package/readme/resource/core.md +91 -0
- package/readme/resource/custody.md +166 -0
- package/readme/resource/customer.md +208 -0
- package/readme/resource/staking.md +277 -0
- package/readme/resource/trading.md +166 -0
- package/readme/resource/transaction.md +106 -0
- package/readme/resource/webhooks.md +307 -0
- package/src/client.ts +46 -0
- package/src/constants.ts +281 -0
- package/src/error.ts +36 -0
- package/src/http.ts +201 -0
- package/src/index.ts +111 -0
- package/src/resources/apikeys.ts +30 -0
- package/src/resources/asset.ts +28 -0
- package/src/resources/bank.ts +39 -0
- package/src/resources/core.ts +19 -0
- package/src/resources/custody.ts +37 -0
- package/src/resources/customer.ts +46 -0
- package/src/resources/staking.ts +88 -0
- package/src/resources/trading.ts +41 -0
- package/src/resources/transaction.ts +20 -0
- package/src/resources/webhooks.ts +169 -0
- package/src/types/apikeys.ts +35 -0
- package/src/types/asset.ts +59 -0
- package/src/types/bank.ts +60 -0
- package/src/types/core.ts +32 -0
- package/src/types/custody.ts +40 -0
- package/src/types/customer.ts +189 -0
- package/src/types/index.ts +215 -0
- package/src/types/staking.ts +87 -0
- package/src/types/trading.ts +56 -0
- package/src/types/transaction.ts +62 -0
- package/src/types/webhooks.ts +60 -0
- package/src/utils.ts +74 -0
- package/src/version.ts +2 -0
- package/dist/cjs/resources/order.d.ts +0 -17
- package/dist/cjs/resources/order.d.ts.map +0 -1
- package/dist/cjs/resources/order.js +0 -30
- package/dist/cjs/resources/order.js.map +0 -1
- package/dist/cjs/resources/stats.d.ts +0 -11
- package/dist/cjs/resources/stats.d.ts.map +0 -1
- package/dist/cjs/resources/stats.js +0 -18
- package/dist/cjs/resources/stats.js.map +0 -1
- package/dist/cjs/resources/user.d.ts +0 -22
- package/dist/cjs/resources/user.d.ts.map +0 -1
- package/dist/cjs/resources/user.js +0 -37
- package/dist/cjs/resources/user.js.map +0 -1
- package/dist/cjs/resources/wallet.d.ts +0 -11
- package/dist/cjs/resources/wallet.d.ts.map +0 -1
- package/dist/cjs/resources/wallet.js +0 -19
- package/dist/cjs/resources/wallet.js.map +0 -1
- package/dist/esm/resources/order.d.ts +0 -17
- package/dist/esm/resources/order.d.ts.map +0 -1
- package/dist/esm/resources/order.js +0 -26
- package/dist/esm/resources/order.js.map +0 -1
- package/dist/esm/resources/stats.d.ts +0 -11
- package/dist/esm/resources/stats.d.ts.map +0 -1
- package/dist/esm/resources/stats.js +0 -14
- package/dist/esm/resources/stats.js.map +0 -1
- package/dist/esm/resources/user.d.ts +0 -22
- package/dist/esm/resources/user.d.ts.map +0 -1
- package/dist/esm/resources/user.js +0 -33
- package/dist/esm/resources/user.js.map +0 -1
- package/dist/esm/resources/wallet.d.ts +0 -11
- package/dist/esm/resources/wallet.d.ts.map +0 -1
- package/dist/esm/resources/wallet.js +0 -15
- package/dist/esm/resources/wallet.js.map +0 -1
- package/dist/types/client.d.ts +0 -19
- package/dist/types/client.d.ts.map +0 -1
- package/dist/types/error.d.ts +0 -15
- package/dist/types/error.d.ts.map +0 -1
- package/dist/types/http.d.ts +0 -21
- package/dist/types/http.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/resources/asset.d.ts +0 -11
- package/dist/types/resources/asset.d.ts.map +0 -1
- package/dist/types/resources/bank.d.ts +0 -13
- package/dist/types/resources/bank.d.ts.map +0 -1
- package/dist/types/resources/order.d.ts +0 -17
- package/dist/types/resources/order.d.ts.map +0 -1
- package/dist/types/resources/stats.d.ts +0 -11
- package/dist/types/resources/stats.d.ts.map +0 -1
- package/dist/types/resources/transaction.d.ts +0 -15
- package/dist/types/resources/transaction.d.ts.map +0 -1
- package/dist/types/resources/user.d.ts +0 -22
- package/dist/types/resources/user.d.ts.map +0 -1
- package/dist/types/resources/wallet.d.ts +0 -11
- package/dist/types/resources/wallet.d.ts.map +0 -1
- package/dist/types/types/index.d.ts +0 -212
- package/dist/types/types/index.d.ts.map +0 -1
- package/dist/types/utils.d.ts +0 -7
- package/dist/types/utils.d.ts.map +0 -1
package/dist/esm/utils.d.ts
CHANGED
|
@@ -1,7 +1,26 @@
|
|
|
1
|
-
import type { UserRef } from './types/index.js';
|
|
1
|
+
import type { Amount, AssetInput, UserRef } from './types/index.js';
|
|
2
2
|
/**
|
|
3
|
-
* Resolve a UserRef (
|
|
3
|
+
* Resolve a UserRef (uuid or email string, or a model carrying a `uuid`) to its string.
|
|
4
4
|
* Used by all resource methods that accept a user parameter.
|
|
5
5
|
*/
|
|
6
6
|
export declare function resolveUuid(ref: UserRef): string;
|
|
7
|
+
/**
|
|
8
|
+
* The uuid of a reference given as a string (sent as is) or as a model carrying a `uuid` (an `Order`, a
|
|
9
|
+
* `StakingPosition`, an `Apikey`…). An empty string, or a model without one, is refused before any request.
|
|
10
|
+
*/
|
|
11
|
+
export declare function idOf(ref: string | {
|
|
12
|
+
uuid: string;
|
|
13
|
+
}, name: string): string;
|
|
14
|
+
/** An asset as given — a uuid or ISO code string as is, or the uuid of an `Asset` / `AssetRef` model; empty → `TypeError` before any request */
|
|
15
|
+
export declare function assetId(asset: AssetInput): string;
|
|
16
|
+
/** An optional asset filter of a list: absent or `null` stays absent, otherwise `assetId` */
|
|
17
|
+
export declare function assetFilter(asset: AssetInput | null | undefined): string | undefined;
|
|
18
|
+
/** Validate and encode a value used as a path segment (`/asset/{asset}`, `/account/{user}`…) */
|
|
19
|
+
export declare function pathSegment(value: string, name: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Normalize an amount for the API, which always receives a string: a string is sent as is
|
|
22
|
+
* (trimmed), a finite number ≥ 0 is converted with `String(n)`. Numbers that `String()` writes
|
|
23
|
+
* in exponent notation (below 1e-6 or from 1e21) are refused: pass them as strings.
|
|
24
|
+
*/
|
|
25
|
+
export declare function amount(value: Amount): string;
|
|
7
26
|
//# sourceMappingURL=utils.d.ts.map
|
package/dist/esm/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAEnE;;;GAGG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAMhD;AAED;;;GAGG;AACH,wBAAgB,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAMzE;AAED,gJAAgJ;AAChJ,wBAAgB,OAAO,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMjD;AAED,6FAA6F;AAC7F,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,SAAS,CAEpF;AAED,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAS/D;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAgB5C"}
|
package/dist/esm/utils.js
CHANGED
|
@@ -1,10 +1,68 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Resolve a UserRef (
|
|
2
|
+
* Resolve a UserRef (uuid or email string, or a model carrying a `uuid`) to its string.
|
|
3
3
|
* Used by all resource methods that accept a user parameter.
|
|
4
4
|
*/
|
|
5
5
|
export function resolveUuid(ref) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
const value = typeof ref === 'string' ? ref : ref?.uuid;
|
|
7
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
8
|
+
throw new TypeError('user must be a non-empty uuid or email, or a model with a non-empty uuid');
|
|
9
|
+
}
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The uuid of a reference given as a string (sent as is) or as a model carrying a `uuid` (an `Order`, a
|
|
14
|
+
* `StakingPosition`, an `Apikey`…). An empty string, or a model without one, is refused before any request.
|
|
15
|
+
*/
|
|
16
|
+
export function idOf(ref, name) {
|
|
17
|
+
const value = typeof ref === 'string' ? ref : ref?.uuid;
|
|
18
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
19
|
+
throw new TypeError(`${name} must be a non-empty string, or a model with a non-empty uuid`);
|
|
20
|
+
}
|
|
21
|
+
return value;
|
|
22
|
+
}
|
|
23
|
+
/** An asset as given — a uuid or ISO code string as is, or the uuid of an `Asset` / `AssetRef` model; empty → `TypeError` before any request */
|
|
24
|
+
export function assetId(asset) {
|
|
25
|
+
const value = typeof asset === 'string' ? asset : asset?.uuid;
|
|
26
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
27
|
+
throw new TypeError('asset must be a uuid or an ISO code, or a model with a non-empty uuid');
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
/** An optional asset filter of a list: absent or `null` stays absent, otherwise `assetId` */
|
|
32
|
+
export function assetFilter(asset) {
|
|
33
|
+
return asset === undefined || asset === null ? undefined : assetId(asset);
|
|
34
|
+
}
|
|
35
|
+
/** Validate and encode a value used as a path segment (`/asset/{asset}`, `/account/{user}`…) */
|
|
36
|
+
export function pathSegment(value, name) {
|
|
37
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
38
|
+
throw new TypeError(`${name} must be a non-empty string`);
|
|
39
|
+
}
|
|
40
|
+
// `.` and `..` are not encoded by encodeURIComponent and would be normalized away by the URL parser
|
|
41
|
+
if (value === '.' || value === '..') {
|
|
42
|
+
throw new TypeError(`${name} must not be "." or ".."`);
|
|
43
|
+
}
|
|
44
|
+
return encodeURIComponent(value);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Normalize an amount for the API, which always receives a string: a string is sent as is
|
|
48
|
+
* (trimmed), a finite number ≥ 0 is converted with `String(n)`. Numbers that `String()` writes
|
|
49
|
+
* in exponent notation (below 1e-6 or from 1e21) are refused: pass them as strings.
|
|
50
|
+
*/
|
|
51
|
+
export function amount(value) {
|
|
52
|
+
if (typeof value === 'string') {
|
|
53
|
+
const trimmed = value.trim();
|
|
54
|
+
if (trimmed === '') {
|
|
55
|
+
throw new TypeError('amount must be a non-empty string or a finite number >= 0');
|
|
56
|
+
}
|
|
57
|
+
return trimmed;
|
|
58
|
+
}
|
|
59
|
+
if (typeof value !== 'number' || !Number.isFinite(value) || value < 0) {
|
|
60
|
+
throw new TypeError('amount must be a non-empty string or a finite number >= 0');
|
|
61
|
+
}
|
|
62
|
+
const text = String(value);
|
|
63
|
+
if (text.includes('e')) {
|
|
64
|
+
throw new TypeError(`amount ${text} would be sent in exponent notation: pass it as a decimal string`);
|
|
65
|
+
}
|
|
66
|
+
return text;
|
|
9
67
|
}
|
|
10
68
|
//# sourceMappingURL=utils.js.map
|
package/dist/esm/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,IAAI,OAAO,GAAG,KAAK,QAAQ;
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,GAAY;IACtC,MAAM,KAAK,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAA;IACvD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,0EAA0E,CAAC,CAAA;IACjG,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,GAA8B,EAAE,IAAY;IAC/D,MAAM,KAAK,GAAG,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,IAAI,CAAA;IACvD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,+DAA+D,CAAC,CAAA;IAC7F,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,gJAAgJ;AAChJ,MAAM,UAAU,OAAO,CAAC,KAAiB;IACvC,MAAM,KAAK,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,IAAI,CAAA;IAC7D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,uEAAuE,CAAC,CAAA;IAC9F,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC;AAED,6FAA6F;AAC7F,MAAM,UAAU,WAAW,CAAC,KAAoC;IAC9D,OAAO,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAC3E,CAAC;AAED,gGAAgG;AAChG,MAAM,UAAU,WAAW,CAAC,KAAa,EAAE,IAAY;IACrD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;QACrD,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,6BAA6B,CAAC,CAAA;IAC3D,CAAC;IACD,oGAAoG;IACpG,IAAI,KAAK,KAAK,GAAG,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACpC,MAAM,IAAI,SAAS,CAAC,GAAG,IAAI,0BAA0B,CAAC,CAAA;IACxD,CAAC;IACD,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAA;AAClC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,MAAM,CAAC,KAAa;IAClC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAA;QAC5B,IAAI,OAAO,KAAK,EAAE,EAAE,CAAC;YACnB,MAAM,IAAI,SAAS,CAAC,2DAA2D,CAAC,CAAA;QAClF,CAAC;QACD,OAAO,OAAO,CAAA;IAChB,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACtE,MAAM,IAAI,SAAS,CAAC,2DAA2D,CAAC,CAAA;IAClF,CAAC;IACD,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAA;IAC1B,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,SAAS,CAAC,UAAU,IAAI,kEAAkE,CAAC,CAAA;IACvG,CAAC;IACD,OAAO,IAAI,CAAA;AACb,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,OAAO,UAAU,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,CAAC,MAAM,OAAO,GAAG,OAAO,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,28 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitgen/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"license": "
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
5
|
"author": "BITGEN",
|
|
6
|
-
"description": "Official Node.js SDK for the BITGEN API
|
|
6
|
+
"description": "Official Node.js SDK for the BITGEN API v4.0.x",
|
|
7
7
|
"main": "./dist/cjs/index.js",
|
|
8
8
|
"module": "./dist/esm/index.js",
|
|
9
9
|
"type": "module",
|
|
10
|
-
"types": "./dist/
|
|
10
|
+
"types": "./dist/esm/index.d.ts",
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
|
13
|
-
"import":
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/esm/index.d.ts",
|
|
15
|
+
"default": "./dist/esm/index.js"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/cjs/index.d.ts",
|
|
19
|
+
"default": "./dist/cjs/index.js"
|
|
20
|
+
}
|
|
16
21
|
}
|
|
17
22
|
},
|
|
23
|
+
"sideEffects": false,
|
|
18
24
|
"files": [
|
|
19
25
|
"dist",
|
|
26
|
+
"src",
|
|
27
|
+
"readme",
|
|
20
28
|
"README.md",
|
|
21
29
|
"CHANGELOG.md"
|
|
22
30
|
],
|
|
23
31
|
"scripts": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
32
|
+
"prebuild": "node scripts/prebuild.js",
|
|
33
|
+
"build": "tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
|
|
34
|
+
"lint": "eslint .",
|
|
35
|
+
"pretest": "npm run build",
|
|
36
|
+
"test": "node --test",
|
|
37
|
+
"prepublishOnly": "npm run lint && npm test",
|
|
38
|
+
"version": "node scripts/prebuild.js && git add src/version.ts"
|
|
26
39
|
},
|
|
27
40
|
"keywords": [
|
|
28
41
|
"bitgen",
|
|
@@ -31,16 +44,20 @@
|
|
|
31
44
|
],
|
|
32
45
|
"repository": {
|
|
33
46
|
"type": "git",
|
|
34
|
-
"url": "https://github.com/bitgenplatform/sdk-js.git"
|
|
47
|
+
"url": "git+https://github.com/bitgenplatform/sdk-js.git"
|
|
35
48
|
},
|
|
36
49
|
"homepage": "https://github.com/bitgenplatform/sdk-js#readme",
|
|
37
50
|
"bugs": {
|
|
38
51
|
"url": "https://github.com/bitgenplatform/sdk-js/issues"
|
|
39
52
|
},
|
|
40
53
|
"engines": {
|
|
41
|
-
"node": ">=
|
|
54
|
+
"node": ">=20.0.0"
|
|
42
55
|
},
|
|
43
56
|
"devDependencies": {
|
|
44
|
-
"
|
|
57
|
+
"@eslint/js": "^10.0.1",
|
|
58
|
+
"@types/node": "^20.19.43",
|
|
59
|
+
"eslint": "^10.10.0",
|
|
60
|
+
"typescript": "^5.9.3",
|
|
61
|
+
"typescript-eslint": "^8.70.0"
|
|
45
62
|
}
|
|
46
63
|
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# Concepts
|
|
2
|
+
|
|
3
|
+
The conventions shared by every resource of the SDK: how a customer is designated, how amounts, pages, booleans, assets and dates travel, and which customers the financial resources accept. Examples use `client`, a configured `BitgenClient` ([Configuration](configuration.md)).
|
|
4
|
+
|
|
5
|
+
## User references
|
|
6
|
+
|
|
7
|
+
Wherever a method expects a customer, it takes a `UserRef`: a string — the customer's **uuid** — or one of the models that carry it. The SDK sends the uuid of the model: the `Created` returned by `client.customer.create`, a `Customer` of `client.customer.list`, an `Account` of `client.customer.get`, the `user` of an `Order`, the `owner` of a `Transaction` or of a `StakingMovement`.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
type UserRef = string | Created | Customer | Account | UserSummary | OrderUser
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
await client.customer.get('CUSTOMER_UUID')
|
|
15
|
+
await client.customer.get(customer) // the Created returned by client.customer.create
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
An email works too where the API resolves it (`customer`, `bank`, `custody`, `staking` — not `trading`, nor the `user` filter of the lists), but the uuid is cheaper for the API — prefer it. TypeScript checks the shape, not the origin: any object carrying a `uuid` string is accepted as a `UserRef`, a `Wallet` included — a uuid that is not a customer's is refused by the API, with the code of the resource called (`404 unknown_user`, `404 unknown_bank`, `403 org_forbidden`…).
|
|
19
|
+
|
|
20
|
+
The same goes for the other objects the SDK returns: wherever a method expects the uuid of an order, a movement, a position, a connector, a transaction, a subscription, an event of the catalogue or a key, it also takes the model itself (`client.staking.rewards(movement.staking)`, `client.trading.get(order)`) and sends its uuid — there, passing another model (a `StakingMovement` as a position, a `Wallet` as an order) is a type error in TypeScript.
|
|
21
|
+
|
|
22
|
+
## Amounts
|
|
23
|
+
|
|
24
|
+
The API handles crypto amounts as **strings** (up to 18 decimals): a JavaScript `number` only keeps about 15 significant digits and nothing on the server side restores what it lost. The SDK therefore:
|
|
25
|
+
|
|
26
|
+
- accepts an `Amount = string | number` and always sends it as a string — a string is sent as is, a number is converted with `String(n)`;
|
|
27
|
+
- rejects, with a `TypeError` and before any request, an empty string, a negative or non-finite number, and a number that `String()` would write in exponent notation (`1e-8`, `1e21`): pass those as strings;
|
|
28
|
+
- never rounds or reformats a string: `'0.000000000000000001'` reaches the API untouched.
|
|
29
|
+
|
|
30
|
+
**Prefer strings**, even for EUR. In responses, crypto quantities are strings (`Wallet.balance`, `Order.amount`, `StakingMovement.amount`) and EUR amounts are numbers with 2 decimals (`BankAccount.balance`, `BankOperation.amount`, `StakingPortfolio.balances`).
|
|
31
|
+
|
|
32
|
+
**Minimums.** Purchases, sales, on-chain withdrawals and staking movements have minimums — set by BITGEN per environment, subject to change, never hard-coded in the SDK. The API's answer is the source of truth: `416 invalid_amount` for a purchase or a sale, `416 withdraw_below_minimum` for an on-chain withdrawal, `422 amount_below_minimum` for a staking movement. For staking, the provider's minimums are readable in its configuration (`client.staking.providers()`, [Staking](resource/staking.md#providers)).
|
|
33
|
+
|
|
34
|
+
## Pagination
|
|
35
|
+
|
|
36
|
+
Paginated lists take `PageParams` and return a `Page`:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
interface PageParams { offset?: number, limit?: number } // limit: default 10, max 50
|
|
40
|
+
interface Page<T> { count: number, items: T[] }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
`transaction.list` accepts a `limit` up to 100.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
const { count, items } = await client.customer.list({ offset: 0, limit: 50 })
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`client.core.list` is not paginated: its `count` is everything that matches.
|
|
50
|
+
|
|
51
|
+
## Query booleans
|
|
52
|
+
|
|
53
|
+
The boolean filters of the lists — `includeClosed` (`customer.list`), `includeRevoked` (`apikeys.list`), `includeArchived` (`webhooks.list`) — are sent as `true` / `false`. The API also reads `1` / `0`, treats an absent parameter as `false`, and answers `422 invalid_<param>` for any other value: `invalid_include_closed`, `invalid_include_revoked`, `invalid_include_archived`.
|
|
54
|
+
|
|
55
|
+
## Assets
|
|
56
|
+
|
|
57
|
+
Wherever an asset is expected, the SDK accepts its **uuid** or its **ISO code** as a string, in any case (the API normalizes it) — or an `Asset` / `AssetRef` model returned by the SDK, whose uuid is then sent. The `Asset` constants are the ISO codes of the main assets; any other code known to the catalogue ([Assets](resource/asset.md)) is passed as a plain string.
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
import { Asset } from '@bitgen/sdk'
|
|
61
|
+
|
|
62
|
+
Asset.BTC // 'btc'
|
|
63
|
+
Asset.ETH // 'eth'
|
|
64
|
+
Asset.USDC // 'usdc'
|
|
65
|
+
Asset.XRP // 'xrp'
|
|
66
|
+
Asset.SOL // 'sol'
|
|
67
|
+
|
|
68
|
+
const eth = await client.asset.get(Asset.ETH) // or by uuid
|
|
69
|
+
const wallet = await client.custody.wallet(customer, eth) // the model: its uuid is sent
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
The `iso` the API returns has the case it is stored with (`ETH` today): **compare it case-insensitively**. In TypeScript, `Asset` is also the type of an asset returned by `client.asset`; `AssetInput` is the type of an asset argument (`string | Asset | AssetRef`).
|
|
73
|
+
|
|
74
|
+
## Constants
|
|
75
|
+
|
|
76
|
+
Every value the API enumerates is a **frozen object of string constants**, exported by the package with a type of the same name — `Env.SANDBOX` is `'sandbox'`, `Locale.FR` is `'FR'`, `TradingDirection.BUY` is `'buy'`, `AssetState.AVAILABLE` is `'AVAILABLE'`, `WebhookEventName.CUSTODY_SENT` is `'custody.sent'`. Inputs take the constant; outputs are strings you compare with the constants.
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
import { Asset, AssetState, Locale } from '@bitgen/sdk'
|
|
80
|
+
|
|
81
|
+
const eth = await client.asset.get(Asset.ETH)
|
|
82
|
+
if (eth.state === AssetState.AVAILABLE) { // outputs are strings: compare them with the constants
|
|
83
|
+
await client.customer.update(customer, { locale: Locale.EN }) // inputs take the constant
|
|
84
|
+
}
|
|
85
|
+
console.log(Object.values(Locale)) // [ 'FR', 'EN' ]
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
In TypeScript, an input that is not one of the values (`locale: 'en'`, `direction: 'Buy'`) is a compile-time error: the case matters. From JavaScript the string is sent as is, and the API's answer is the source of truth. An output the SDK does not know yet (a state the API added) is kept as is, as a string — `WebhookEventName` accepts any string, as the catalogue may grow.
|
|
89
|
+
|
|
90
|
+
## Timestamps and histories
|
|
91
|
+
|
|
92
|
+
Timestamps are **epochs in seconds** (`createdAt`, `updatedAt`, `date`, `expiresAt`…). Time series are a `History`:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
type History = Record<'d' | 'w' | 'm' | 'y' | 'all', [number, number][]>
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Each key is a list of `[epoch seconds, value]` points: `d` covers the last 24 hours with one point per hour, `w` and `m` one point per day, `y` and `all` one point per month; the last point is the current value. Histories are the EUR price of an asset (`client.asset`), the EUR balance of a bank account, the EUR value of a wallet or of a whole custody, and the capital and revenues of a staking portfolio.
|
|
99
|
+
|
|
100
|
+
## Activation and identity
|
|
101
|
+
|
|
102
|
+
By default `client.customer.create` sends the customer an activation email. Until they click it, the account stays `CustomerState.CREATED` (`setup.needActivation` is `true`) and the **financial resources do not see it**: `client.bank` answers `404 unknown_bank`, `client.custody` and `client.staking` `403 org_forbidden`, `client.trading` `403 user_not_in_scope`. Only `client.customer.list` (where the customer appears as `CREATED`) and `client.customer.get` see it. An organization that handles onboarding itself creates its customers with `needActivation: false` — usable right away, no BITGEN email — and `notify: false` for no BITGEN emails at all ([Customers › create](resource/customer.md#create)).
|
|
103
|
+
|
|
104
|
+
If your organization uses BITGEN's identity verification, the customer's identity (KYC for a person, KYB for a business) must be validated first — `412 owner_identity_not_validated` when reading the EUR account, `403 kyc_not_validated` on custody, trading and staking otherwise. An organization that verifies the identity of its customers by its own means has no such requirement. The verification itself is not part of the SDK; its state is `identity.state` ([Customers › Identity](resource/customer.md#identity)).
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Configuration
|
|
2
|
+
|
|
3
|
+
A `BitgenClient` is built once per API key and reused: it holds the credentials, the target environment and the request timeout. Every resource hangs off it (`client.customer`, `client.bank`, `client.custody`…).
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { BitgenClient, Env } from '@bitgen/sdk'
|
|
7
|
+
|
|
8
|
+
const client = new BitgenClient({
|
|
9
|
+
scope: 'YOUR_SCOPE_UUID',
|
|
10
|
+
apiKey: 'YOUR_API_KEY',
|
|
11
|
+
env: Env.PRODUCTION, // default
|
|
12
|
+
timeout: 30, // seconds, default 30
|
|
13
|
+
})
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Credentials
|
|
17
|
+
|
|
18
|
+
| Option | Description |
|
|
19
|
+
|---|---|
|
|
20
|
+
| `scope` | uuid of the organization that owns the key. Sent as the `BITGEN-Scope` header. It is also the organization the SDK uses wherever the API expects yours (webhooks, API keys, the organization of a new customer). |
|
|
21
|
+
| `apiKey` | The raw key, shown once when it is created. Sent as the `Api-key` header. |
|
|
22
|
+
|
|
23
|
+
A missing key, an unknown, revoked or expired key, or a `scope` that is not the key's organization, is refused with a `401` ([Common errors](errors.md#common-errors)).
|
|
24
|
+
|
|
25
|
+
## Environments
|
|
26
|
+
|
|
27
|
+
| `env` | Constant | URL |
|
|
28
|
+
|---|---|---|
|
|
29
|
+
| `production` | `Env.PRODUCTION` (default) | `https://api.bitgen.com` |
|
|
30
|
+
| `sandbox` | `Env.SANDBOX` | `https://api.sandbox.bitgen.com` |
|
|
31
|
+
|
|
32
|
+
`Env` holds these names as a frozen object (`Object.values(Env)` lists them): pass the constant — the type of its values is `BitgenEnv`. Anything else is refused before any request ([Validation](#validation), [Constants](concepts.md#constants)).
|
|
33
|
+
|
|
34
|
+
## Custom host
|
|
35
|
+
|
|
36
|
+
To reach the API through another hostname — a container, a tunnel — give `host` instead of `env`:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const client = new BitgenClient({
|
|
40
|
+
scope: 'YOUR_SCOPE_UUID',
|
|
41
|
+
apiKey: 'YOUR_API_KEY',
|
|
42
|
+
host: 'my-hostname', // bare hostname: no scheme, port or path
|
|
43
|
+
port: 8080, // default 80
|
|
44
|
+
isSsl: false, // default true (https)
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Timeout
|
|
49
|
+
|
|
50
|
+
`timeout` is the maximum time, in seconds, the SDK waits for the API to answer: `30` by default, `0` disables it. When it expires, the call rejects with `BitgenError { status: 0, code: 'request_timeout' }` ([No HTTP response](errors.md#no-http-response)).
|
|
51
|
+
|
|
52
|
+
## Requests
|
|
53
|
+
|
|
54
|
+
Every request carries the headers `BITGEN-Scope`, `Api-key`, `Content-Type: application/json` and `User-Agent: bitgen-sdk-nodejs/<version>`, where `<version>` is the installed version of the SDK. Redirects are never followed.
|
|
55
|
+
|
|
56
|
+
## Validation
|
|
57
|
+
|
|
58
|
+
An invalid configuration throws a `TypeError` from the constructor, before any request is sent: empty `scope` or `apiKey` (or one that is not printable ASCII), `env` that is not one of `Env`, `host` that is not a bare hostname (letters, digits, `.`, `-` and `_` only — no scheme, port, path, credentials or brackets), `port` outside 1–65535, `timeout` that is not a number of seconds between `0` and `2_147_483`. The values of `scope`, `apiKey` and `env` never appear in the message. This is the only synchronous error of the SDK: everything else rejects a promise ([Rejected promises](errors.md#rejected-promises)).
|
|
59
|
+
|
|
60
|
+
## Options
|
|
61
|
+
|
|
62
|
+
`BitgenConfig`:
|
|
63
|
+
|
|
64
|
+
| Option | Type | Default | Description |
|
|
65
|
+
|---|---|---|---|
|
|
66
|
+
| `scope` | `string` | — | Organization uuid |
|
|
67
|
+
| `apiKey` | `string` | — | API key |
|
|
68
|
+
| `env` | `BitgenEnv` | `Env.PRODUCTION` | Target environment — an `Env` constant |
|
|
69
|
+
| `host` | `string` | — | Custom hostname, used instead of `env` |
|
|
70
|
+
| `port` | `number` | `80` | Port, with `host` |
|
|
71
|
+
| `isSsl` | `boolean` | `true` | `https` or `http`, with `host` |
|
|
72
|
+
| `timeout` | `number` | `30` | Request timeout in seconds, `0` = none |
|
package/readme/errors.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Errors
|
|
2
|
+
|
|
3
|
+
The API answers with real HTTP status codes and, on failure, a JSON body `{ error: true, message: '<code>', code: <status> }` where `message` is a stable snake_case code (`invalid_amount`, `unknown_asset`…), never a sentence. The SDK turns every non-2xx answer — and every request that gets no HTTP answer at all — into a `BitgenError` that rejects the returned promise.
|
|
4
|
+
|
|
5
|
+
Examples use `client`, a configured `BitgenClient` ([Configuration](configuration.md)).
|
|
6
|
+
|
|
7
|
+
## BitgenError
|
|
8
|
+
|
|
9
|
+
| Property | Type | Description |
|
|
10
|
+
|---|---|---|
|
|
11
|
+
| `status` | `number` | The HTTP status (`416`…), or `0` when no HTTP response was received |
|
|
12
|
+
| `code` | `string` | The stable code of the API (`requested_amount_error`), or the raw response text when the body is not the API's JSON error |
|
|
13
|
+
| `message` | `string` | `"[<status>] <code>"` |
|
|
14
|
+
| `cause` | `unknown` | The native error, for `network_error` |
|
|
15
|
+
| `name` | `string` | `'BitgenError'` |
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { BitgenError } from '@bitgen/sdk'
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
await client.bank.withdraw('CUSTOMER_UUID', { amount: '50.00' })
|
|
22
|
+
} catch (err) {
|
|
23
|
+
if (err instanceof BitgenError) {
|
|
24
|
+
console.log(err.status) // 416
|
|
25
|
+
console.log(err.code) // 'requested_amount_error'
|
|
26
|
+
console.log(err.message) // '[416] requested_amount_error'
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`instanceof BitgenError` is reliable even when the ESM and CommonJS builds of the SDK are both loaded in the same process. `JSON.stringify(err)` gives `{ name, message, status, code }` — `cause` is left out.
|
|
32
|
+
|
|
33
|
+
## No HTTP response
|
|
34
|
+
|
|
35
|
+
| Status | `code` | Meaning |
|
|
36
|
+
|---|---|---|
|
|
37
|
+
| `0` | `request_timeout` | No response within the configured `timeout` — 30 seconds by default ([Timeout](configuration.md#timeout)) |
|
|
38
|
+
| `0` | `network_error` | The request never got an HTTP answer: DNS, connection refused, TLS… `err.cause` holds the native error |
|
|
39
|
+
|
|
40
|
+
## Unexpected answers
|
|
41
|
+
|
|
42
|
+
When the body is not the API's JSON error payload (proxy error page, unexpected `500`…), `code` holds the raw response text, truncated to 200 characters; the same goes for a 2xx answer that is not JSON. A `500` is not always a failure of the API: it is also its answer to a request it does not recognize — with a custom `host`, check that it reaches the API unchanged. Redirects are never followed: a `3xx` answer is reported as a `BitgenError`, and the key is never replayed to another host. A value the SDK enumerates (a `state`, a `mode`…) is never checked: the API may add one, the types keep it as a string ([Constants](concepts.md#constants)).
|
|
43
|
+
|
|
44
|
+
## Rejected promises
|
|
45
|
+
|
|
46
|
+
Every method returns a promise. An invalid argument — an empty user, asset, order or amount, a model without uuid, a negative or non-finite number, a `tolerance` that is not a number of seconds… — rejects it with a `TypeError` **before any request is sent**, so `.catch()` and `Promise.all` see every failure. The only synchronous error of the SDK is an invalid configuration, thrown by `new BitgenClient()` ([Validation](configuration.md#validation)).
|
|
47
|
+
|
|
48
|
+
## Common errors
|
|
49
|
+
|
|
50
|
+
The errors any call can answer. Each resource page lists its own codes.
|
|
51
|
+
|
|
52
|
+
| Status | `code` | Meaning |
|
|
53
|
+
|---|---|---|
|
|
54
|
+
| `401` | `auth_missing` | The `BITGEN-Scope` or `Api-key` header is missing |
|
|
55
|
+
| `401` | `api_key_missmatch` | The key is unknown, revoked or expired, or `scope` is not the key's organization |
|
|
56
|
+
| `403` | `unknown_organization` | The organization of the key is unknown |
|
|
57
|
+
| `403` | `organization_not_enabled` | The organization is not enabled |
|
|
58
|
+
| `403` | `api_disabled` | API access is not enabled for the organization |
|
|
59
|
+
| `403` | `invalid_api_key` | The key is invalid |
|
|
60
|
+
| `403` | `expired_api_key` | The key has expired |
|
|
61
|
+
| `403` | `forbidden_permission` | The key does not carry the permission for this call — contact BITGEN |
|
|
62
|
+
| `400` | `required_index_missing::<field>` | A mandatory field is missing or empty |
|
|
63
|
+
| `404` | `unknown_<resource>` | The target does not exist — `unknown_user`, `unknown_asset`, `unknown_bank`… — or the API does not reveal it |
|
|
64
|
+
| `422` | `invalid_<param>` | A boolean filter (`includeClosed`, `includeRevoked`, `includeArchived`) is not a boolean value ([Query booleans](concepts.md#query-booleans)) |
|
|
65
|
+
| `423` | `blocked_by_alert` | The customer is under an active compliance alert (each resource page lists its `423` codes, with `account_frozen` and the lock codes) |
|
|
66
|
+
|
|
67
|
+
Webhook verification has its own codes, with `status: 0`: [Webhooks › verify](resource/webhooks.md#verify).
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Installation
|
|
2
|
+
|
|
3
|
+
`@bitgen/sdk` is the official Node.js SDK for the BITGEN API v4. It runs server-side only: the API accepts browser requests from a fixed list of origins, so the SDK is not meant to be used from a browser.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Node.js 20 or later
|
|
8
|
+
- No dependency: the SDK only uses what Node.js 20 already provides
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install @bitgen/sdk
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Import
|
|
17
|
+
|
|
18
|
+
ESM:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { BitgenClient, BitgenError, Env, Asset } from '@bitgen/sdk'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
CommonJS:
|
|
25
|
+
|
|
26
|
+
```js
|
|
27
|
+
const { BitgenClient, BitgenError, Env, Asset } = require('@bitgen/sdk')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The package exports the client (`BitgenClient`), its error class (`BitgenError`), two constants — `Env`, the environments, and `Asset`, the ISO codes of the main assets ([Configuration](configuration.md), [Assets](concepts.md#assets)) — and the constants naming the known values of the API (`AssetState`, `OrderState`, `Locale`, `OrganizationCategory`…), each also a type ([Constants](concepts.md#constants)).
|
|
31
|
+
|
|
32
|
+
## TypeScript
|
|
33
|
+
|
|
34
|
+
Type declarations ship with the package: nothing else to install. Every type a method takes or returns is exported under the same name as in the documentation (`BitgenConfig`, `Customer`, `Wallet`, `Order`, `UserRef`, `AssetInput`…):
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import type { BitgenConfig, Customer, Wallet } from '@bitgen/sdk'
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Next steps
|
|
41
|
+
|
|
42
|
+
- [Quick start](quick-start.md) — create the client and run a first customer journey
|
|
43
|
+
- [Configuration](configuration.md) — credentials, environments, custom host, timeout
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Quick start
|
|
2
|
+
|
|
3
|
+
Create the client once, then run a first journey: create a customer, read their EUR account, open a wallet, buy crypto.
|
|
4
|
+
|
|
5
|
+
## 1. Create the client
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { BitgenClient, Env } from '@bitgen/sdk'
|
|
9
|
+
|
|
10
|
+
const client = new BitgenClient({
|
|
11
|
+
scope: 'YOUR_SCOPE_UUID', // uuid of the organization that owns the key
|
|
12
|
+
apiKey: 'YOUR_API_KEY',
|
|
13
|
+
env: Env.SANDBOX, // Env.PRODUCTION by default
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
One instance per API key, reused for every call. `scope`, `apiKey`, environments and the other options are detailed in [Configuration](configuration.md).
|
|
18
|
+
|
|
19
|
+
## 2. Create a customer
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { Locale } from '@bitgen/sdk'
|
|
23
|
+
|
|
24
|
+
const customer = await client.customer.create({
|
|
25
|
+
account: { email: 'jean@valjean.fr', firstname: 'Jean', lastname: 'Valjean' },
|
|
26
|
+
group: { manager: 'MANAGER_UUID' }, // the collaborator of your organization who follows this customer
|
|
27
|
+
locale: Locale.FR,
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The customer receives an activation email: until they click it, their account stays `CustomerState.CREATED` and the bank, custody, trading and staking resources do not see it (unless you create them with `needActivation: false` — [Customers › create](resource/customer.md#create)). If your organization uses BITGEN's identity verification, their identity must also be validated before the next steps ([Activation and identity](concepts.md#activation-and-identity)). The `Created` object the API returned is a `UserRef`: the next steps pass it as is, wherever a customer is expected ([User references](concepts.md#user-references)).
|
|
32
|
+
|
|
33
|
+
## 3. Read the EUR account
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const account = await client.bank.get(customer)
|
|
37
|
+
|
|
38
|
+
console.log(account.message) // wire reference: the customer puts it on their bank transfer
|
|
39
|
+
console.log(account.balance) // EUR balance, credited once the transfer is received
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
The EUR account is created on first read. Balance, operations and withdrawals: [Bank accounts](resource/bank.md).
|
|
43
|
+
|
|
44
|
+
## 4. Open a wallet
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { Asset } from '@bitgen/sdk'
|
|
48
|
+
|
|
49
|
+
const wallet = await client.custody.wallet(customer, Asset.ETH)
|
|
50
|
+
|
|
51
|
+
console.log(wallet.address) // deposit address, created at the custodian on first read
|
|
52
|
+
console.log(wallet.balance) // exact quantity, as a string
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Wallets, balances and on-chain withdrawals: [Custody wallets](resource/custody.md).
|
|
56
|
+
|
|
57
|
+
## 5. Buy crypto
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const { tunnel, state } = await client.trading.buy(customer, {
|
|
61
|
+
asset: Asset.ETH,
|
|
62
|
+
amount: '25.00', // EUR, taken from the customer's EUR account
|
|
63
|
+
reference: 'order-42', // optional idempotency key
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const order = await client.trading.get(tunnel) // `tunnel` is the order uuid; `order.state` follows its lifecycle (`OrderState`)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Orders, states and sales: [Trading](resource/trading.md).
|
|
70
|
+
|
|
71
|
+
## Handling errors
|
|
72
|
+
|
|
73
|
+
Every call returns a promise. An error of the API rejects it with a `BitgenError` carrying the HTTP `status` and a stable `code`:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { BitgenError } from '@bitgen/sdk'
|
|
77
|
+
|
|
78
|
+
try {
|
|
79
|
+
await client.bank.withdraw(customer, { amount: '50.00' })
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err instanceof BitgenError && err.code === 'requested_amount_error') {
|
|
82
|
+
// insufficient EUR balance
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
All the details, including the errors that happen before any request is sent: [Errors](errors.md).
|