@messagebird/sdk 0.1.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/LICENSE +21 -0
- package/README.md +132 -0
- package/dist/index.d.ts +1692 -0
- package/dist/index.js +1608 -0
- package/dist/index.js.map +1 -0
- package/package.json +62 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Bird
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# @messagebird/sdk
|
|
2
|
+
|
|
3
|
+
The official TypeScript SDK for the [Bird](https://bird.com) API — fully typed, edge-ready, ESM.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- **Node.js 20.3+** or a modern edge runtime (Cloudflare Workers, Vercel Edge, Deno). The SDK uses only web-standard APIs (`fetch`, `AbortSignal`, Web Crypto) and ships no Node built-ins.
|
|
8
|
+
- **ESM package.** `import` works everywhere. `require("@messagebird/sdk")` also works on Node 20.19+ (via `require(esm)`); on older runtimes, load it with `await import("@messagebird/sdk")`.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
pnpm add @messagebird/sdk
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
> This SDK is generated from Bird's public OpenAPI bundle inside Bird's internal monorepo, which is the single source of truth; this repository tracks tagged releases. Generation runs in the monorepo, so `pnpm generate` won't work from a clone here — see [CONTRIBUTING.md](./CONTRIBUTING.md).
|
|
17
|
+
|
|
18
|
+
## Quickstart
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { BirdClient } from "@messagebird/sdk";
|
|
22
|
+
|
|
23
|
+
const bird = new BirdClient({ apiKey: process.env.BIRD_API_KEY! });
|
|
24
|
+
|
|
25
|
+
const email = await bird.email.send({
|
|
26
|
+
from: "hello@acme.com",
|
|
27
|
+
to: ["customer@example.com"],
|
|
28
|
+
subject: "Welcome aboard",
|
|
29
|
+
html: "<h1>Hi there</h1>",
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log(email.id);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
The region is inferred from the API key prefix (`bk_{region}_…`). For a local or self-hosted server, pass `baseUrl` (which overrides region resolution).
|
|
36
|
+
|
|
37
|
+
## Client defaults
|
|
38
|
+
|
|
39
|
+
Set channel defaults and the webhook secret once at construction. Defaulted fields become optional on each call (the per-call value wins), enforced by the types.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
const bird = new BirdClient({
|
|
43
|
+
apiKey: process.env.BIRD_API_KEY!,
|
|
44
|
+
email: { from: "hello@acme.com" }, // now optional in email.send
|
|
45
|
+
webhooks: { secret: process.env.BIRD_WEBHOOK_SECRET! },
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
await bird.email.send({ to: ["customer@example.com"], subject: "Hi", html: "…" });
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Email
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
await bird.email.send({ from, to, subject, html }); // resolves when accepted (202)
|
|
55
|
+
await bird.email.get(messageId); // aggregate delivery status
|
|
56
|
+
|
|
57
|
+
// `await` yields the first page; `for await` walks every message across pages.
|
|
58
|
+
for await (const message of bird.email.list()) {
|
|
59
|
+
console.log(message.id);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Webhooks
|
|
64
|
+
|
|
65
|
+
`unwrap` verifies a delivery's Standard Webhooks signature and returns a typed, discriminated event. **Pass the raw request body** — never the parsed JSON. Set the signing secret once via `webhooks: { secret }` on the client (or pass `{ secret }` per call).
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
const event = bird.webhooks.unwrap(rawBody, request.headers);
|
|
69
|
+
switch (event.type) {
|
|
70
|
+
case "email.delivered":
|
|
71
|
+
console.log(event.email_id, event.recipient); // narrowed; fields are flat
|
|
72
|
+
break;
|
|
73
|
+
default: // unknown future events land here — forward-compatible
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
> Endpoint management (registering/listing webhook endpoints) is not in this release; it returns once the delivery substrate stabilises.
|
|
78
|
+
|
|
79
|
+
## Errors
|
|
80
|
+
|
|
81
|
+
Methods **throw** on failure with a typed error hierarchy you narrow with `instanceof`:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
import { BirdRateLimitError, BirdValidationError } from "@messagebird/sdk";
|
|
85
|
+
|
|
86
|
+
try {
|
|
87
|
+
await bird.email.send({ from, to, subject, html });
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (err instanceof BirdRateLimitError) await sleep(err.retryAfter);
|
|
90
|
+
else if (err instanceof BirdValidationError) console.error(err.details);
|
|
91
|
+
else throw err;
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Every API error carries `statusCode`, `requestId`, and `type`. The core retries safely on `429`/`5xx`/network failures (mutations reuse one idempotency key across attempts).
|
|
96
|
+
|
|
97
|
+
Prefer to branch on a value instead of catching? Use `.safe()`:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const { data, error } = await bird.email.send({ from, to, subject, html }).safe();
|
|
101
|
+
if (error) return; // data is null; error is a BirdError
|
|
102
|
+
data.id; // narrowed
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
And `.withResponse()` exposes transport metadata (status, headers, request id) on success:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const { data, response } = await bird.email.list().withResponse();
|
|
109
|
+
response.headers.get("ratelimit-remaining");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Escape hatch
|
|
113
|
+
|
|
114
|
+
For endpoints the typed resources don't cover yet, `request<T>()` runs the full lifecycle (auth, retries, idempotency, error mapping) — you supply the response type:
|
|
115
|
+
|
|
116
|
+
```ts
|
|
117
|
+
const domains = await bird.request<DomainList>({ method: "GET", path: "/v1/email/domains" });
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Configuration
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
new BirdClient({
|
|
124
|
+
apiKey: "bk_eu1_…",
|
|
125
|
+
region: "eu1", // override the key-prefix region
|
|
126
|
+
baseUrl: "http://localhost:8080", // override entirely (local/self-hosted)
|
|
127
|
+
timeout: 60_000, // per-attempt timeout (ms)
|
|
128
|
+
maxRetries: 2,
|
|
129
|
+
fetch: customFetch, // proxying, edge adapters, testing
|
|
130
|
+
defaultHeaders: { "X-My-Header": "…" },
|
|
131
|
+
});
|
|
132
|
+
```
|