@bizjaya/finclient 0.1.1 → 0.2.1

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 CHANGED
@@ -1,6 +1,7 @@
1
1
  # @bizjaya/finclient
2
2
 
3
- The official TypeScript SDK for FinCopy's public `/v1` API. It supports Node.js 18 and later, with ESM and CommonJS builds.
3
+ The official TypeScript SDK for FinCopy's public API, `https://api.fincopy.io`. Since version 0.2.0 it uses numeric ACNO
4
+ account paths under `/v1/fxs`, which changed the 0.1 API; see the changelog before upgrading.
4
5
 
5
6
  ```sh
6
7
  npm install @bizjaya/finclient
@@ -11,33 +12,67 @@ import { FinClient } from "@bizjaya/finclient";
11
12
 
12
13
  const fin = new FinClient({ apiKey: process.env.FINCOPY_API_KEY! });
13
14
  const accounts = await fin.copy.accounts.list();
14
- for (const account of accounts) console.log(account.Fleet?.Account?.Id, account.Fleet?.Account?.Login);
15
-
16
- // Read what changed since the saved cursor, one page at a time; save the cursor after each page.
17
- let cursor: string | undefined = loadSavedCursor();
18
- for await (const page of fin.copy.changes.readPages(cursor)) {
19
- for (const change of page.Changes ?? []) console.log(change.Entity, change.AccountId, change.OID, change.Status);
20
- if (page.Cursor) saveCursor(page.Cursor);
21
- }
15
+ for (const account of accounts.items) console.log(account.Fleet?.Account?.ACNO, account.Fleet?.Account?.Login);
16
+
17
+ // ACNO is in the route; the JSON body contains only trade fields.
18
+ // await fin.copy.orders.make(52014125, { SYM: "EURUSD", ACT: "NEW", OTY: "OP_BUY", LOT: 0.01 });
19
+ ```
20
+
21
+ The client defaults to `https://api.fincopy.io`. Account-scoped methods take the numeric ACNO, and paged list methods
22
+ return `{ items, paging, ... }`. DTO fields keep the API's PascalCase names.
23
+
24
+ - `copy.accounts`: settings and lifecycle, pairs, symbols, quotes, orders, history, DCA, performance, activity, strategy
25
+ preview and apply, and terminal operations.
26
+ - `copy.orders`: preflight, trade, hedge, modify, cancel and DCA answers.
27
+ - `copy.fx`: lists across every account you can reach (pairs, orders, history, activity, DCA, terminal operations,
28
+ performance window and bars), strategies, signals, alerts, brokers and the event stream.
29
+ - `copy.changes` and `copy.webhooks`: the change feed and webhook setup.
30
+
31
+ ## API key or member session
32
+
33
+ API-key mode is for trusted server-side code. Do not put an API key in browser JavaScript, a mobile app, a public
34
+ repository, or a client-side environment variable.
35
+
36
+ Member session mode sends the signed-in member's `fincopy_member` cookie instead of a key: omit `apiKey` and pass `fetch`.
37
+ A session client defaults to `credentials: "include"`, so in a browser page on a FinCopy site the browser's `fetch` sends
38
+ the cookie. On a server, pass a `fetch` that adds the cookie header.
39
+
40
+ ```ts
41
+ const fin = new FinClient({ fetch: (input, init) => fetch(input, init) });
42
+ const account = await fin.copy.accounts.get(52014125);
22
43
  ```
23
44
 
24
- `loadSavedCursor` and `saveCursor` stand for your own storage. The first call without a cursor starts at "now".
45
+ The API accepts browser requests only from FinCopy's own sites. Session trade writes pass the member's trade-PIN gate on
46
+ the server. The main entry uses no Node.js built-ins, so it runs in browsers, Node.js 20+, Deno and Bun.
47
+
48
+ `copy.accounts.remove(acno, { TrPin }, options)` disconnects the hosted terminal, forgets its broker password and frees its seat.
49
+ Customer account settings and trade history remain so the login can be restored later; a free demo's account and terminal records
50
+ are deleted. Open orders stay at the broker, and virtual TP, SL and TS stop working while no terminal runs. Include the member's
51
+ trading PIN for a session account when its PIN is enabled; API-key requests do not use a session PIN. Pass an `idempotencyKey`
52
+ in `options` so retries receive the same operation.
53
+
54
+ ## Results, errors and the stream
55
+
56
+ Trade calls report that an operation was queued; follow the stream, change feed or webhook for its terminal result. Errors
57
+ are raised as `FinApiError` with HTTP status, API code, validation errors, any `Retry-After` value, and an `errorCode` of
58
+ `timeout`, `network`, `rate_limited`, `invalid_response` or `empty_response` when the SDK set one. Retries reuse the same
59
+ `Idempotency-Key`.
25
60
 
26
- A failed call throws `FinApiError` with `statusCode`, FinCopy's `code`, `errors`, and, when the SDK set it, `errorCode`
27
- (`timeout`, `network`, `rate_limited`, `invalid_response` or `empty_response`) and `retryAfterMs`. Calls are retried on
28
- rate limits, gateway errors and network failures, honouring the API's `Retry-After`.
61
+ `copy.fx.stream()` does not reconnect. It ends when the server closes it (about every 30 minutes); a broken connection
62
+ raises a `network` error. Open it again in a loop, waiting a few seconds between attempts.
29
63
 
30
- The client is server-side only. Do not put an API key in browser JavaScript, a mobile application, a public repository, or a
31
- client-side environment variable. FinCopy's envelope is handled by the client; DTO fields keep the API's PascalCase wire names.
32
- Writes automatically carry an `Idempotency-Key`, which can be supplied explicitly in the request options when a caller needs
33
- to retry the same operation later. Orders complete asynchronously; use the change feed or webhook to learn their result.
64
+ ## Webhooks
34
65
 
35
- The API key grants the permissions assigned to that key. Store it as a server secret and give it only the permissions the
36
- application needs.
66
+ Webhook verification is server-side and uses Node.js's crypto module, so it has its own entry:
67
+
68
+ ```ts
69
+ import { FinWebhook } from "@bizjaya/finclient/webhook";
70
+
71
+ const valid = FinWebhook.verify(secret, timestampHeader, rawBody, signatureHeader);
72
+ ```
37
73
 
38
- See [FinCopy API docs](https://fincopy.io/docs) and the [FinClient source](https://github.com/bizjaya/FinClient). The
39
- [QuickStart](./examples/QuickStart.ts) example lists accounts and reads the change feed; its order call is commented out
40
- because it places a trade.
74
+ See the [FinCopy API docs](https://fincopy.io/docs), the [QuickStart](./examples/QuickStart.ts), and the
75
+ [FinClient source](https://github.com/bizjaya/FinClient).
41
76
 
42
77
  ## Build from source
43
78