@kite-01/sdk 0.0.0-stage → 0.1.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/README.md CHANGED
@@ -1,3 +1,108 @@
1
- # Temporary Holding Version
1
+ # @kite-01/sdk
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Typed client for a [Kite](https://github.com/Kite-01/server) server. It signs every request for you and verifies webhooks. Use it on your server; it refuses to run in a browser.
4
+
5
+ ```sh
6
+ npm install @kite-01/sdk
7
+ ```
8
+
9
+ Node 20+, Bun, Deno and edge runtimes are supported: signing uses Web Crypto.
10
+
11
+ ## Connect
12
+
13
+ With an API key (an Ed25519 key pair whose public half you registered with Kite):
14
+
15
+ ```ts
16
+ import { Kite } from "@kite-01/sdk";
17
+
18
+ const kite = new Kite({
19
+ baseUrl: "https://pay.example.com",
20
+ auth: {
21
+ apiKeyId: process.env.KITE_API_KEY_ID,
22
+ privateKey: process.env.KITE_PRIVATE_KEY,
23
+ },
24
+ });
25
+ ```
26
+
27
+ Or with the server's root key, optionally acting as one app:
28
+
29
+ ```ts
30
+ const kite = new Kite({
31
+ baseUrl: "https://pay.example.com",
32
+ auth: { rootKey: process.env.KITE_ROOT_KEY, appId },
33
+ });
34
+ ```
35
+
36
+ ## Take a payment
37
+
38
+ Create a payment link for the order, tagged with your own reference, and hand its id to your frontend to open with [`@kite-01/js`](https://github.com/Kite-01/monorepo/tree/main/packages/js):
39
+
40
+ ```ts
41
+ const { data: link } = await kite.paymentLinks.create({
42
+ body: {
43
+ title: "Pro plan",
44
+ currency: "USD",
45
+ amount: "49",
46
+ reference: order.id,
47
+ meta: { customerEmail: order.email },
48
+ },
49
+ });
50
+
51
+ return { paymentLinkId: link.id };
52
+ ```
53
+
54
+ Find it again by reference with `kite.paymentLinks.list({ query: { reference: order.id } })`.
55
+
56
+ Every operation in the API is available, grouped as `kite.<group>.<action>`: `apps`, `apiKeys`, `customers`, `paymentLinks`, `quotes`, `transfers`, `webhooks`, `fees`, `tokens`, `chains`, `x402` and more. Calls return `{ data, request, response }`, typed from Kite's OpenAPI spec.
57
+
58
+ ## Errors
59
+
60
+ A failed call throws a `KiteError` with the HTTP `status`, Kite's error `code` and its `message`. A request that never got a response has no status and the code `NetworkError`.
61
+
62
+ ```ts
63
+ import { KiteError } from "@kite-01/sdk";
64
+
65
+ try {
66
+ await kite.paymentLinks.create({ body });
67
+ } catch (error) {
68
+ if (error instanceof KiteError && error.code === "Conflict") {
69
+ // a link with this reference already exists
70
+ }
71
+ }
72
+ ```
73
+
74
+ ## Webhooks
75
+
76
+ Pass the raw body, before any JSON parsing, with the request headers and the webhook's signing secret:
77
+
78
+ ```ts
79
+ import { verifyWebhook } from "@kite-01/sdk";
80
+
81
+ app.post(
82
+ "/kite/webhooks",
83
+ express.raw({ type: "application/json" }),
84
+ async (req, res) => {
85
+ const event = await verifyWebhook({
86
+ body: req.body,
87
+ headers: req.headers,
88
+ secret: process.env.KITE_WEBHOOK_SECRET,
89
+ });
90
+
91
+ if (event.category === "Transfer" && event.current.status === "Settled") {
92
+ const transfer = event.data as {
93
+ quote: { paymentLink: { reference: string | null } | null };
94
+ };
95
+ const orderId = transfer.quote.paymentLink?.reference;
96
+ // fulfil the order
97
+ }
98
+
99
+ res.sendStatus(200);
100
+ },
101
+ );
102
+ ```
103
+
104
+ It throws a `WebhookVerificationError` when the signature doesn't match or the delivery is more than five minutes old. A retried delivery keeps its `eventId`, so use that to ignore repeats.
105
+
106
+ ## License
107
+
108
+ [MIT](https://github.com/Kite-01/monorepo/blob/main/LICENSE)