@messagebird/sdk 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
@@ -2,6 +2,8 @@
2
2
 
3
3
  The official TypeScript SDK for the [Bird](https://bird.com) API — fully typed, edge-ready, ESM.
4
4
 
5
+ 📚 **Documentation:** https://bird.com/docs/sdks/typescript
6
+
5
7
  ## Requirements
6
8
 
7
9
  - **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.
@@ -17,19 +19,21 @@ pnpm add @messagebird/sdk
17
19
 
18
20
  ## Quickstart
19
21
 
22
+ <!-- bird:snippet quickstart-email -->
23
+
20
24
  ```ts
21
25
  import { BirdClient } from "@messagebird/sdk";
22
26
 
23
27
  const bird = new BirdClient({ apiKey: process.env.BIRD_API_KEY! });
24
28
 
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>",
29
+ const msg = await bird.email.send({
30
+ from: { email: "onboarding@messagebird.dev", name: "Bird" },
31
+ to: ["delivered@messagebird.dev"],
32
+ subject: "Hello from Bird",
33
+ html: "<p>My first Bird email.</p>",
30
34
  });
31
35
 
32
- console.log(email.id);
36
+ console.log(msg.id, msg.status);
33
37
  ```
34
38
 
35
39
  The region is inferred from the API key prefix (`bk_{region}_…`). For a local or self-hosted server, pass `baseUrl` (which overrides region resolution).
@@ -80,14 +84,22 @@ switch (event.type) {
80
84
 
81
85
  Methods **throw** on failure with a typed error hierarchy you narrow with `instanceof`:
82
86
 
87
+ <!-- bird:snippet email.errors -->
88
+
83
89
  ```ts
84
- import { BirdRateLimitError, BirdValidationError } from "@messagebird/sdk";
90
+ import { BirdRateLimitError, BirdValidationError, BirdAPIError } from "@messagebird/sdk";
85
91
 
86
92
  try {
87
- await bird.email.send({ from, to, subject, html });
93
+ await bird.email.send({
94
+ from: { email: "onboarding@messagebird.dev", name: "Bird" },
95
+ to: ["delivered@messagebird.dev"],
96
+ subject: "Hello from Bird",
97
+ html: "<p>My first Bird email.</p>",
98
+ });
88
99
  } catch (err) {
89
- if (err instanceof BirdRateLimitError) await sleep(err.retryAfter);
100
+ if (err instanceof BirdRateLimitError) console.log(`rate limited — retry in ${err.retryAfter}s`);
90
101
  else if (err instanceof BirdValidationError) console.error(err.details);
102
+ else if (err instanceof BirdAPIError) console.error(err.code, err.requestId);
91
103
  else throw err;
92
104
  }
93
105
  ```
@@ -96,10 +108,19 @@ Every API error carries `statusCode`, `requestId`, and `type`. The core retries
96
108
 
97
109
  Prefer to branch on a value instead of catching? Use `.safe()`:
98
110
 
111
+ <!-- bird:snippet email.safe -->
112
+
99
113
  ```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
114
+ const { data, error } = await bird.email
115
+ .send({
116
+ from: { email: "onboarding@messagebird.dev", name: "Bird" },
117
+ to: ["delivered@messagebird.dev"],
118
+ subject: "Hello from Bird",
119
+ html: "<p>My first Bird email.</p>",
120
+ })
121
+ .safe();
122
+ if (error) console.error(error.message);
123
+ else console.log(data.id);
103
124
  ```
104
125
 
105
126
  And `.withResponse()` exposes transport metadata (status, headers, request id) on success: