sendlit 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/LICENSE +21 -0
- package/README.md +207 -2
- package/dist/index.d.mts +8946 -0
- package/dist/index.d.ts +8946 -0
- package/dist/index.js +2875 -0
- package/dist/index.mjs +2863 -0
- package/package.json +64 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 CourseLit
|
|
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
CHANGED
|
@@ -1,3 +1,208 @@
|
|
|
1
|
-
#
|
|
1
|
+
# SendLit TypeScript SDK
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The official server-side TypeScript SDK for the [SendLit](https://sendlit.app)
|
|
4
|
+
API. Use it to sync contacts, run broadcasts and sequences, send
|
|
5
|
+
transactional email, and provision one SendLit team per tenant of your
|
|
6
|
+
platform.
|
|
7
|
+
|
|
8
|
+
Requests and responses are typed from the same contract the SendLit API
|
|
9
|
+
validates against, so the SDK can't drift from the server.
|
|
10
|
+
|
|
11
|
+
## Install
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install sendlit
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Requires Node.js 18 or later. The SDK is server-only and throws if it's
|
|
18
|
+
constructed in a browser. Never ship an API key to the client.
|
|
19
|
+
|
|
20
|
+
## Quickstart
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { SendLit } from "sendlit";
|
|
24
|
+
|
|
25
|
+
const sendlit = new SendLit({ apiKey: process.env.SENDLIT_API_KEY });
|
|
26
|
+
|
|
27
|
+
const contact = await sendlit.contacts.create({
|
|
28
|
+
email: "ada@example.com",
|
|
29
|
+
name: "Ada Lovelace",
|
|
30
|
+
tags: ["signup"],
|
|
31
|
+
customFields: { plan: "free" },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
await sendlit.contacts.addTag(contact.contactId, "onboarding");
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
new SendLit({
|
|
41
|
+
apiKey: "sl_live_…", // or set SENDLIT_API_KEY
|
|
42
|
+
endpoint: "https://api.sendlit.app", // or SENDLIT_ENDPOINT; for self-hosting
|
|
43
|
+
teamId: "team_…", // or SENDLIT_TEAM_ID
|
|
44
|
+
maxRetries: 2,
|
|
45
|
+
timeout: 30_000,
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
| Option | Env var | Notes |
|
|
50
|
+
| ------------- | ------------------ | --------------------------------------------------------------------------------------------------------------------------------- |
|
|
51
|
+
| `apiKey` | `SENDLIT_API_KEY` | A team key (`sl_live_…`) is sent as `x-sendlit-apikey`. An organization key (`sl_org_live_…`) is sent as `Authorization: Bearer`. |
|
|
52
|
+
| `accessToken` | — | An OAuth access token, sent as `Authorization: Bearer`. Use it instead of `apiKey`. |
|
|
53
|
+
| `endpoint` | `SENDLIT_ENDPOINT` | Defaults to `https://api.sendlit.app`. |
|
|
54
|
+
| `teamId` | `SENDLIT_TEAM_ID` | Sent as `x-sendlit-team-id`. Only needed when the credential can reach more than one team. A team key is bound to one team. |
|
|
55
|
+
| `maxRetries` | — | Default `2`. See [Retries](#retries). |
|
|
56
|
+
| `timeout` | — | Per-attempt timeout in milliseconds. Default `30000`. |
|
|
57
|
+
| `fetch` | — | A custom `fetch` implementation. |
|
|
58
|
+
|
|
59
|
+
## Resources
|
|
60
|
+
|
|
61
|
+
| Resource | Methods |
|
|
62
|
+
| ------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
63
|
+
| `sendlit.contacts` | `create`, `list`, `listAll`, `get`, `update`, `remove`, `addTag`, `removeTag`, `deliveries` |
|
|
64
|
+
| `sendlit.segments` | `create`, `list`, `get`, `update`, `remove` |
|
|
65
|
+
| `sendlit.templates` | `list`, `listSystem`, `get`, `create`, `update`, `duplicate`, `remove` |
|
|
66
|
+
| `sendlit.sequences` | Broadcasts and sequences: `create`, `list`, `listAll`, `get`, `update`, `remove`, `addEmail`, `updateEmail`, `removeEmail`, `start`, `pause`, `stats`, `subscribers` |
|
|
67
|
+
| `sendlit.emails` | Transactional email: `send`, `get`, `list`, `listAll` |
|
|
68
|
+
| `sendlit.provisioning` | Organization key only: `provisionTeam`, `getTeam`, `updateTeam`, `createTeamKey`, `suspendTeam`, `resumeTeam`, `archiveTeam`, `getTeamUsage` |
|
|
69
|
+
| `sendlit.suppressions` | `list`, `listAll`, `get`, `release` |
|
|
70
|
+
| `sendlit.deliveryEvents` | `list`, `listAll`, `get` |
|
|
71
|
+
|
|
72
|
+
Request and response types are exported too, for example `Contact`,
|
|
73
|
+
`CreateContactInput`, `Sequence`, and `SendEmailInput`.
|
|
74
|
+
|
|
75
|
+
### Contacts and onboarding sequences
|
|
76
|
+
|
|
77
|
+
`contacts.create` is find-or-create by email within the team:
|
|
78
|
+
|
|
79
|
+
- A **new** contact fires the `subscriber:added` event, which enrolls it in
|
|
80
|
+
every active sequence with that trigger. This is how you start an
|
|
81
|
+
onboarding sequence on signup.
|
|
82
|
+
- If the email **already exists**, the existing contact is returned unchanged
|
|
83
|
+
and **no event fires**, so a repeat signup doesn't re-enroll the contact.
|
|
84
|
+
- Tags passed to `create` don't fire `tag:added`. Use `subscriber:added` as
|
|
85
|
+
the onboarding trigger, or call `addTag` after creating the contact if you
|
|
86
|
+
want a tag trigger.
|
|
87
|
+
|
|
88
|
+
### Pagination
|
|
89
|
+
|
|
90
|
+
`list` returns one page as `{ items, total }`. `listAll` returns an async
|
|
91
|
+
iterator that fetches pages as you go:
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
for await (const contact of sendlit.contacts.listAll({ q: "example.com" })) {
|
|
95
|
+
console.log(contact.email);
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Errors
|
|
100
|
+
|
|
101
|
+
Every failed call rejects with a `SendLitError`:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { SendLitError } from "sendlit";
|
|
105
|
+
|
|
106
|
+
try {
|
|
107
|
+
await sendlit.contacts.get("con_missing");
|
|
108
|
+
} catch (error) {
|
|
109
|
+
if (error instanceof SendLitError && error.status === 404) {
|
|
110
|
+
// error.code is the API's `error` value, error.body the full response
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
| Field | Description |
|
|
116
|
+
| --------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
117
|
+
| `status` | The HTTP status, or `0` when no response was received. |
|
|
118
|
+
| `code` | The API's `error` value. The SDK's own codes are `validation_error`, `connection_error`, `timeout`, `invalid_argument` and `invalid_config`. |
|
|
119
|
+
| `message` | A readable description. |
|
|
120
|
+
| `body` | The parsed response body, including extra fields such as `missingVariables`. |
|
|
121
|
+
|
|
122
|
+
IDs are validated before a request is sent. An empty or malformed ID rejects
|
|
123
|
+
with `invalid_argument` and never reaches the network.
|
|
124
|
+
|
|
125
|
+
## Retries
|
|
126
|
+
|
|
127
|
+
The SDK retries up to `maxRetries` times, with exponential backoff and
|
|
128
|
+
jitter. It honors `Retry-After` when the response includes one.
|
|
129
|
+
|
|
130
|
+
- **429** responses are retried for every call, because the request was
|
|
131
|
+
rejected before any work was done.
|
|
132
|
+
- **5xx, timeouts, and network errors** are retried only for calls that are
|
|
133
|
+
safe to repeat. That means `GET`, `PUT` and `DELETE` requests,
|
|
134
|
+
`contacts.create` and `contacts.addTag` (both find-or-create),
|
|
135
|
+
`provisioning.provisionTeam` (idempotent by `externalId`), and
|
|
136
|
+
`emails.send` when you pass an `idempotencyKey`.
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
await sendlit.emails.send({
|
|
140
|
+
to: "ada@example.com",
|
|
141
|
+
subject: "Your receipt",
|
|
142
|
+
templateId: "tpl_…",
|
|
143
|
+
variables: { amount: "$10" },
|
|
144
|
+
idempotencyKey: `receipt-${order.id}`,
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Platform example: one team per tenant
|
|
149
|
+
|
|
150
|
+
A multi-tenant platform such as CourseLit provisions one SendLit team for
|
|
151
|
+
each of its own tenants, then uses that team's key for everything else. See
|
|
152
|
+
[Provisioning teams](https://sendlit.app/developers/provisioning) for the
|
|
153
|
+
organization setup and key scopes.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { SendLit } from "sendlit";
|
|
157
|
+
|
|
158
|
+
// 1. Provision the tenant's team with an organization key.
|
|
159
|
+
const platform = new SendLit({ apiKey: process.env.SENDLIT_ORG_API_KEY });
|
|
160
|
+
|
|
161
|
+
const team = await platform.provisioning.provisionTeam({
|
|
162
|
+
externalId: school.id, // an immutable ID from your database
|
|
163
|
+
name: school.name,
|
|
164
|
+
mailingAddress: school.address,
|
|
165
|
+
});
|
|
166
|
+
if (team.created) {
|
|
167
|
+
// The plaintext key is only returned once. Encrypt and store it now.
|
|
168
|
+
await saveTeamKey(school.id, team.teamId, team.apiKey);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 2. Act on that team with its own key.
|
|
172
|
+
const sendlit = new SendLit({ apiKey: await loadTeamKey(school.id) });
|
|
173
|
+
|
|
174
|
+
// Sync a signup. A new contact enters sequences triggered by subscriber:added.
|
|
175
|
+
await sendlit.contacts.create({
|
|
176
|
+
email: user.email,
|
|
177
|
+
name: user.name,
|
|
178
|
+
customFields: { courselit_user_id: user.id },
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// 3. Create and send a broadcast from a marketing template.
|
|
182
|
+
const templates = await sendlit.templates.list({ purpose: "marketing" });
|
|
183
|
+
let broadcast = await sendlit.sequences.create({
|
|
184
|
+
type: "broadcast",
|
|
185
|
+
templateId: templates[0].templateId,
|
|
186
|
+
});
|
|
187
|
+
broadcast = await sendlit.sequences.update(broadcast.sequenceId, {
|
|
188
|
+
title: "October newsletter",
|
|
189
|
+
});
|
|
190
|
+
await sendlit.sequences.updateEmail(
|
|
191
|
+
broadcast.sequenceId,
|
|
192
|
+
broadcast.emails[0].emailId,
|
|
193
|
+
{
|
|
194
|
+
subject: "What's new in October",
|
|
195
|
+
published: true,
|
|
196
|
+
delayInMillis: Date.now(),
|
|
197
|
+
},
|
|
198
|
+
);
|
|
199
|
+
await sendlit.sequences.start(broadcast.sequenceId);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Starting a broadcast or sequence requires a mailing address in the team's
|
|
203
|
+
settings (set it with `mailingAddress` when provisioning) and a way to send
|
|
204
|
+
email: either an organization delivery grant or the team's own ESP.
|
|
205
|
+
|
|
206
|
+
## License
|
|
207
|
+
|
|
208
|
+
MIT
|