@openemail/sdk 0.0.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.
Files changed (6) hide show
  1. package/README.md +183 -0
  2. package/index.cjs +1776 -0
  3. package/index.d.cts +1531 -0
  4. package/index.d.ts +1531 -0
  5. package/index.js +1744 -0
  6. package/package.json +60 -0
package/README.md ADDED
@@ -0,0 +1,183 @@
1
+ <div align='center'>
2
+ <a href='https://openemail.uk'>
3
+ <img
4
+ src='https://openemail.uk/logo.svg'
5
+ alt='OpenEmail Logo'
6
+ width='180'
7
+ />
8
+ </a>
9
+
10
+ <br />
11
+ </div>
12
+
13
+ <p align='center'>
14
+ Email you can build on. Send mail, read the mailbox and automate a workspace from code.
15
+ </p>
16
+
17
+ <p align='center'>
18
+ <a href='https://openemail.uk'>
19
+ <b>
20
+ Website
21
+ </b>
22
+ </a>
23
+ •
24
+ <a href='https://openemail.uk/docs/sdk'>
25
+ <b>
26
+ Documentation
27
+ </b>
28
+ </a>
29
+ •
30
+ <a href='https://openemail.uk/docs/api/reference'>
31
+ <b>
32
+ API Reference
33
+ </b>
34
+ </a>
35
+ •
36
+ <a href='https://openemail.uk/docs/mcp/overview'>
37
+ <b>
38
+ MCP Server
39
+ </b>
40
+ </a>
41
+ </p>
42
+
43
+ <br />
44
+
45
+ ## Intro to the Npm Package
46
+
47
+ The official TypeScript client for the OpenEmail API. One method per endpoint, 103 in all, typed end to end, with zero dependencies. It runs on Node 20+, Bun, Deno and Cloudflare Workers, and ships as ESM and CommonJS.
48
+
49
+ It carries a workspace API key, so it belongs on a server. The one exception is disposable inboxes, which need no key and work in a browser.
50
+
51
+ ### Installing
52
+ ```bash
53
+ npm i @openemail/sdk@latest
54
+ ```
55
+
56
+ ### Using
57
+ ```typescript
58
+ import { OpenEmail } from '@openemail/sdk'
59
+
60
+ const openemail = new OpenEmail('oe_live_...')
61
+
62
+ const sent = await openemail.emails.send({
63
+ from: 'Acme Billing <billing@acme.com>',
64
+ to: 'ada@example.com',
65
+ subject: 'Your September invoice',
66
+ html: '<p>Your invoice is attached.</p>',
67
+ attachments: [{ filename: 'invoice.pdf', content: pdfBytes, contentType: 'application/pdf' }]
68
+ })
69
+
70
+ console.log(sent.id, sent.status)
71
+ ```
72
+
73
+ Create a key in OpenEmail under Settings, API keys. It is shown once, and it belongs in an environment variable rather than in code.
74
+
75
+ Build the client once, in a module of its own, and import it everywhere else:
76
+
77
+ ```typescript
78
+ import { OpenEmail } from '@openemail/sdk'
79
+
80
+ export const openemail = new OpenEmail(process.env.OPENEMAIL_API_KEY!)
81
+ ```
82
+
83
+ Or skip even that. The package ships a ready made `openemail` that reads `OPENEMAIL_API_KEY` the first time it is touched:
84
+
85
+ ```typescript
86
+ import { openemail } from '@openemail/sdk'
87
+
88
+ await openemail.emails.send({ from, to, subject, text })
89
+ ```
90
+
91
+ Every send carries an idempotency key, generated once per call and reused by its retries, so a retried request replays the original message rather than sending a second one. Pass your own with `{ idempotencyKey }` to make that hold across processes and restarts.
92
+
93
+ ### Reading the mailbox
94
+ ```typescript
95
+ const page = await openemail.threads.list({ folder: 'inbox', limit: 25 })
96
+
97
+ for await (const thread of openemail.threads.iterate({ folder: 'inbox', query: 'invoice' })) {
98
+ const full = await openemail.threads.get(thread.id)
99
+
100
+ console.log(full.messageCount, full.hasUnread)
101
+ }
102
+ ```
103
+
104
+ Every paginated resource has `list` for one page, `listAll` for every page as one array, and `iterate` to stream items and stop whenever you like.
105
+
106
+ ### Errors
107
+ ```typescript
108
+ import { OpenEmailApiError, openemail } from '@openemail/sdk'
109
+
110
+ try {
111
+ await openemail.templates.send('order-shipped', {
112
+ from: 'dispatch@acme.com',
113
+ to: 'ada@example.com',
114
+ props: { orderId: 'AC-4192' }
115
+ })
116
+ }
117
+
118
+ catch (error) {
119
+ if (error instanceof OpenEmailApiError && error.isValidation) {
120
+ console.error(error.code, error.param, error.requestId)
121
+ }
122
+
123
+ throw error
124
+ }
125
+ ```
126
+
127
+ An API refusal is one class, `OpenEmailApiError`, with `status`, `type`, `code`, `param` and `requestId`, plus `isValidation`, `isNotFound`, `isRateLimited` and friends to branch on. No response at all is `OpenEmailNetworkError`, with `isTimeout` when the deadline passed.
128
+
129
+ ### Webhooks
130
+ ```typescript
131
+ import { verifyWebhookSignature } from '@openemail/sdk'
132
+
133
+ export default async (request: Request) => {
134
+ const event = await verifyWebhookSignature({
135
+ payload: await request.text(),
136
+ headers: request.headers,
137
+ secret: process.env.OPENEMAIL_WEBHOOK_SECRET!
138
+ })
139
+
140
+ console.log(event.type, event.data)
141
+
142
+ return new Response(null, { status: 204 })
143
+ }
144
+ ```
145
+
146
+ It checks the HMAC in constant time and rejects a delivery more than five minutes old, then returns the parsed event. Pass the raw body: re-serialising it changes the bytes and the signature will not match.
147
+
148
+ ### Disposable inboxes
149
+ ```typescript
150
+ import { createTempMail } from '@openemail/sdk'
151
+
152
+ const temp = createTempMail()
153
+
154
+ const inbox = await temp.create({ ttlMinutes: 60 })
155
+
156
+ const { items } = await temp.listMessages(inbox.id, { inboxToken: inbox.token })
157
+ ```
158
+
159
+ `create` needs no credential and is the only call that returns the inbox token, so keep it.
160
+
161
+ ### Configuring
162
+ Pass an options object instead of the bare key when the defaults are not right:
163
+
164
+ ```typescript
165
+ import { OpenEmail } from '@openemail/sdk'
166
+
167
+ export const openemail = new OpenEmail({
168
+ apiKey: process.env.OPENEMAIL_API_KEY!,
169
+ baseUrl: 'https://api.openemail.uk',
170
+ timeoutMs: 30_000,
171
+ maxRetries: 2,
172
+ fetch: myFetch,
173
+ headers: { 'X-Team': 'billing' }
174
+ })
175
+ ```
176
+
177
+ `createOpenEmail(options)` is the same constructor with one difference: anything you leave out is read from the environment. The shipped `openemail` takes the same options through `init(options)`, called once at startup.
178
+
179
+ `baseUrl` also comes from `OPENEMAIL_BASE_URL`, for a server you run yourself. Reads are retried on 408, 429 and 5xx with backoff, honouring `Retry-After` up to a minute. Writes that cannot safely repeat are not. Every method takes `{ signal, apiKey }` as its last argument, so one process can serve several workspaces with one client.
180
+
181
+ When a newer version is on npm the client says so once on a TTY. `OPENEMAIL_DISABLE_UPDATE_NOTICE=1` or `{ disableUpdateNotice: true }` turns that off.
182
+
183
+ Everything else, templates, rules, tracking, calendar, roles, members, settings and the full reference for all 103 methods, lives in the [documentation](https://openemail.uk/docs/sdk).