unitpost 0.3.0 → 0.4.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
@@ -56,6 +56,7 @@ Options: `new Unitpost({ apiKey, baseUrl, timeout, fetch, headers, maxRetries, r
56
56
  | `email.campaigns` | Draft, schedule, send, pause/resume |
57
57
  | `email.templates` | Template CRUD |
58
58
  | `email.domains` | Domains + verify |
59
+ | `sms` | Send, get, list, `brands()`, `numbers()`, contact SMS consent. Available on paid plans: on the free plan `send` returns a `403` with code `limit_reached`, while the reads keep working. |
59
60
  | `contacts` | CRUD, import jobs |
60
61
  | `contactFields` | Custom field definitions |
61
62
  | `segments` | Segments + membership |
@@ -87,7 +88,7 @@ complaints are added automatically; you can also add your own:
87
88
  await unitpost.suppressions.create({ email: "bounced@example.com", reason: "manual" });
88
89
  await unitpost.suppressions.create({ emails: ["a@example.com", "b@example.com"] });
89
90
 
90
- // Look one up by id or email; list (optionally filter scope); un-suppress.
91
+ // Look one up by id or email; list your own; un-suppress.
91
92
  const { data } = await unitpost.suppressions.get("bounced@example.com");
92
93
  for await (const s of unitpost.suppressions.listAll()) console.log(s.email, s.reason);
93
94
  await unitpost.suppressions.delete("bounced@example.com");
@@ -118,6 +119,51 @@ You can also call it from the client (Resend-style alias):
118
119
  const event = unitpost.webhooks.verify({ payload: rawBody, secret, headers: req.headers });
119
120
  ```
120
121
 
122
+ ## Receive a text and answer it
123
+
124
+ US toll-free, 10DLC and Canadian numbers take replies (`two_way: true` on
125
+ `sms.numbers()`). Subscribe a webhook to `sms.received`, skip STOP and texts
126
+ Unitpost already answered, read the conversation with `participant`, and answer
127
+ without `from` so the reply leaves from the number they texted.
128
+
129
+ ```ts
130
+ import { Unitpost, verifyWebhook } from "unitpost";
131
+
132
+ const unitpost = new Unitpost(process.env.UNITPOST_API_KEY);
133
+
134
+ // Your webhook endpoint, subscribed to sms.received. Pass the RAW body.
135
+ export async function onWebhook(rawBody: string, headers: Record<string, string>) {
136
+ const event = verifyWebhook<{ type: string; data: SmsReceived }>({
137
+ payload: rawBody,
138
+ secret: process.env.UNITPOST_WEBHOOK_SECRET!, // whsec_…
139
+ headers,
140
+ });
141
+ if (event.type !== "sms.received") return;
142
+ const reply = event.data;
143
+ if (reply.keyword === "OPT_OUT" || reply.auto_replied) return; // STOP, or already answered
144
+
145
+ // The whole conversation with this person, both directions, newest first.
146
+ const { data: history } = await unitpost.sms.list({ participant: reply.from, limit: 20 });
147
+ const answer = await yourAgent(history?.data ?? [], reply.body ?? "");
148
+
149
+ // No `from`: the answer goes out from the number they texted.
150
+ await unitpost.sms.send({ to: reply.from, body: answer });
151
+ }
152
+
153
+ type SmsReceived = {
154
+ sms_id: string;
155
+ from: string;
156
+ to: string;
157
+ body: string | null; // empty when the reply carried no text
158
+ keyword: "OPT_OUT" | "OPT_IN" | "HELP" | null;
159
+ auto_replied: boolean;
160
+ in_reply_to: string | null;
161
+ contact_id: string | null;
162
+ received_at: string | null; // order a conversation by this
163
+ created_at: string | null;
164
+ };
165
+ ```
166
+
121
167
  ## Idempotency
122
168
 
123
169
  `email.send` and `email.batch` are safe to retry by default: if you don't pass
@@ -161,7 +207,8 @@ CI-gated, so the SDK never silently drifts from the API.
161
207
  | | |
162
208
  | --- | --- |
163
209
  | Docs + API reference | https://www.unitpost.com/docs |
164
- | Product / SMTP / MCP guides | https://www.unitpost.com/guides |
210
+ | Product guides | https://www.unitpost.com/guides |
211
+ | SMTP | https://www.unitpost.com/docs/smtp |
165
212
  | OpenAPI JSON | https://www.unitpost.com/api/v1/openapi.json |
166
213
  | LLM index | https://www.unitpost.com/llms.txt |
167
214
  | Dashboard / API keys | https://www.unitpost.com |
@@ -169,7 +216,7 @@ CI-gated, so the SDK never silently drifts from the API.
169
216
  | Template gallery | https://www.unitpost.com/templates/gallery |
170
217
  | Playground | https://www.unitpost.com/playground |
171
218
  | Migration Assistant | https://www.unitpost.com/migration |
172
- | MCP + Agent Skills | https://www.unitpost.com/guides#ai-mcp · https://www.unitpost.com/guides#ai-skills |
219
+ | MCP + Agent Skills | https://www.unitpost.com/docs/ai/mcp · https://www.unitpost.com/docs/ai/skills |
173
220
  | Support | support@unitpost.com |
174
221
 
175
222
  ## License
package/dist/index.cjs CHANGED
@@ -6,7 +6,7 @@ var crypto = require('crypto');
6
6
  var API_VERSION = "v1";
7
7
 
8
8
  // src/http.ts
9
- var SDK_VERSION = "0.3.0";
9
+ var SDK_VERSION = "0.4.0";
10
10
  var DEFAULT_BASE_URL = "https://www.unitpost.com";
11
11
  var UnitpostError = class extends Error {
12
12
  code;
@@ -191,13 +191,19 @@ function safeJsonParse(text) {
191
191
  }
192
192
 
193
193
  // src/pagination.ts
194
- async function* paginate(client, path, params = {}) {
194
+ var FORWARD_ONLY_MESSAGE = "listAll pages forward only: `before` is not supported (use list() to page backward).";
195
+ function paginate(client, path, params = {}) {
196
+ if (params.before) throw new TypeError(FORWARD_ONLY_MESSAGE);
197
+ return pages(client, path, params);
198
+ }
199
+ async function* pages(client, path, params) {
195
200
  let after = params.after;
196
201
  for (; ; ) {
197
202
  const { data, error } = await client.request({
198
203
  method: "GET",
199
204
  path,
200
- query: { limit: params.limit, after, before: params.before }
205
+ // Filters ride along on every page; only the cursor moves.
206
+ query: { ...params, after }
201
207
  });
202
208
  if (error) throw error;
203
209
  const page = data;
@@ -261,6 +267,215 @@ var Resource = class {
261
267
  }
262
268
  http;
263
269
  };
270
+ var Sms = class extends Resource {
271
+ /** SMS templates: plain-text bodies with `{{variable}}` placeholders. */
272
+ templates;
273
+ /** SMS campaigns: a published SMS template sent to a segment. */
274
+ campaigns;
275
+ /** SMS subscription topics (independent from email topics). */
276
+ topics;
277
+ constructor(http) {
278
+ super(http);
279
+ this.templates = new SmsTemplates(http);
280
+ this.campaigns = new SmsCampaigns(http);
281
+ this.topics = new SmsTopics(http);
282
+ }
283
+ /**
284
+ * Send or schedule an SMS message.
285
+ *
286
+ * The recipient must be E.164 (`+15551234567`). Marketing sends require the
287
+ * recipient's prior express consent on record and honor recipient-local
288
+ * quiet hours (deferred, never dropped). Billing is per segment. An
289
+ * `Idempotency-Key` is generated automatically when you don't pass one.
290
+ */
291
+ send(body, opts = {}) {
292
+ return this.http.request({
293
+ method: "POST",
294
+ path: "/sms",
295
+ body,
296
+ idempotencyKey: opts.idempotencyKey ?? crypto.randomUUID()
297
+ });
298
+ }
299
+ /** Retrieve one SMS message's status and delivery state. */
300
+ get(id) {
301
+ return this.http.request({ method: "GET", path: `/sms/${enc(id)}` });
302
+ }
303
+ /**
304
+ * Cancel a queued/scheduled SMS (`{ cancel: true }` — its reserved credit is
305
+ * refunded) or move a scheduled one (`{ scheduled_at }`).
306
+ */
307
+ update(id, body) {
308
+ return this.http.request({ method: "PATCH", path: `/sms/${enc(id)}`, body });
309
+ }
310
+ /**
311
+ * List SMS messages (cursor-paginated, newest first). `participant` returns
312
+ * the whole conversation with one number: texts you sent them and their
313
+ * replies.
314
+ */
315
+ list(params = {}) {
316
+ return this.http.request({ method: "GET", path: "/sms", query: params });
317
+ }
318
+ /** Async-iterate every SMS message (same filters as `list`). */
319
+ listAll(params = {}) {
320
+ return paginate(this.http, "/sms", params);
321
+ }
322
+ /**
323
+ * List SMS brands and their setup status.
324
+ *
325
+ * A brand is the business identity carriers register before they approve a
326
+ * phone number, so this is what to poll to answer "is SMS ready yet?":
327
+ * `status` says whether you or the carrier is the blocker, and `missing`
328
+ * lists exactly what is still required.
329
+ *
330
+ * Read-only — creating a brand is a paid carrier submission and is only
331
+ * available in the dashboard.
332
+ */
333
+ brands(params = {}) {
334
+ return this.http.request({
335
+ method: "GET",
336
+ path: "/sms/brands",
337
+ query: params
338
+ });
339
+ }
340
+ /**
341
+ * List SMS phone numbers.
342
+ *
343
+ * Only numbers with `status: "active"` can send. A `ten_dlc` number has no
344
+ * `phone_number` until carriers approve it, and a `simulator` number only
345
+ * reaches verified test destinations.
346
+ *
347
+ * Read-only — requesting or releasing a number is only available in the
348
+ * dashboard.
349
+ */
350
+ numbers(params = {}) {
351
+ return this.http.request({
352
+ method: "GET",
353
+ path: "/sms/numbers",
354
+ query: params
355
+ });
356
+ }
357
+ /**
358
+ * SMS delivery stats over a rolling window: queued, sent, delivered,
359
+ * failed, suppressed, canceled, inbound and segments billed, with delivery,
360
+ * failure and suppression rates. No opens or clicks on SMS.
361
+ *
362
+ * `days` is the window size (1–365); defaults to 30 server-side.
363
+ */
364
+ stats(params = {}) {
365
+ return this.http.request({ method: "GET", path: "/sms/stats", query: params });
366
+ }
367
+ /** Read a contact's SMS consent state and immutable consent history. */
368
+ smsConsent(contactId) {
369
+ return this.http.request({
370
+ method: "GET",
371
+ path: `/contacts/${enc(contactId)}/sms-consent`
372
+ });
373
+ }
374
+ /** Record an SMS consent change (opt-in or opt-out) for a contact. */
375
+ recordSmsConsent(contactId, body) {
376
+ return this.http.request({
377
+ method: "POST",
378
+ path: `/contacts/${enc(contactId)}/sms-consent`,
379
+ body
380
+ });
381
+ }
382
+ };
383
+ var SmsTemplates = class extends Resource {
384
+ list(params = {}) {
385
+ return this.http.request({ method: "GET", path: "/sms/templates", query: params });
386
+ }
387
+ listAll(params = {}) {
388
+ return paginate(this.http, "/sms/templates", params);
389
+ }
390
+ create(body) {
391
+ return this.http.request({ method: "POST", path: "/sms/templates", body });
392
+ }
393
+ get(id) {
394
+ return this.http.request({ method: "GET", path: `/sms/templates/${enc(id)}` });
395
+ }
396
+ update(id, body) {
397
+ return this.http.request({ method: "PATCH", path: `/sms/templates/${enc(id)}`, body });
398
+ }
399
+ delete(id) {
400
+ return this.http.request({ method: "DELETE", path: `/sms/templates/${enc(id)}` });
401
+ }
402
+ };
403
+ var SmsCampaigns = class extends Resource {
404
+ list(params = {}) {
405
+ return this.http.request({ method: "GET", path: "/sms/campaigns", query: params });
406
+ }
407
+ listAll(params = {}) {
408
+ return paginate(this.http, "/sms/campaigns", params);
409
+ }
410
+ /** Create a draft. Omit `sender_id` for automatic routing. */
411
+ create(body) {
412
+ return this.http.request({ method: "POST", path: "/sms/campaigns", body });
413
+ }
414
+ get(id) {
415
+ return this.http.request({ method: "GET", path: `/sms/campaigns/${enc(id)}` });
416
+ }
417
+ /** Edit a draft (409 once sent or scheduled). */
418
+ update(id, body) {
419
+ return this.http.request({ method: "PATCH", path: `/sms/campaigns/${enc(id)}`, body });
420
+ }
421
+ /** Archive a draft or finished campaign (cancel an in-flight one first). */
422
+ delete(id) {
423
+ return this.http.request({ method: "DELETE", path: `/sms/campaigns/${enc(id)}` });
424
+ }
425
+ /**
426
+ * Send now, or pass `{ scheduled_at }` to schedule. A large audience whose
427
+ * recipient check is still running is refused with `409 preflight_pending`:
428
+ * poll `validate(id)` until it returns the report, then send again.
429
+ */
430
+ send(id, body = {}) {
431
+ return this.http.request({ method: "POST", path: `/sms/campaigns/${enc(id)}/send`, body });
432
+ }
433
+ /**
434
+ * The campaign, already `canceled`. With more than 500 queued texts (202)
435
+ * they are canceled and refunded in the background.
436
+ */
437
+ cancel(id) {
438
+ return this.http.request({ method: "POST", path: `/sms/campaigns/${enc(id)}/cancel` });
439
+ }
440
+ pause(id) {
441
+ return this.http.request({ method: "POST", path: `/sms/campaigns/${enc(id)}/pause` });
442
+ }
443
+ resume(id) {
444
+ return this.http.request({ method: "POST", path: `/sms/campaigns/${enc(id)}/resume` });
445
+ }
446
+ reschedule(id, body) {
447
+ return this.http.request({ method: "POST", path: `/sms/campaigns/${enc(id)}/reschedule`, body });
448
+ }
449
+ /**
450
+ * Who the campaign would reach if sent now (non-mutating). A large audience
451
+ * is counted in the background: the first call returns that `Job` (202).
452
+ * Call again until it returns the report; tell them apart with `object`
453
+ * (`"job"`).
454
+ */
455
+ validate(id) {
456
+ return this.http.request({ method: "GET", path: `/sms/campaigns/${enc(id)}/validate` });
457
+ }
458
+ };
459
+ var SmsTopics = class extends Resource {
460
+ list(params = {}) {
461
+ return this.http.request({ method: "GET", path: "/sms/topics", query: params });
462
+ }
463
+ listAll(params = {}) {
464
+ return paginate(this.http, "/sms/topics", params);
465
+ }
466
+ create(body) {
467
+ return this.http.request({ method: "POST", path: "/sms/topics", body });
468
+ }
469
+ get(id) {
470
+ return this.http.request({ method: "GET", path: `/sms/topics/${enc(id)}` });
471
+ }
472
+ update(id, body) {
473
+ return this.http.request({ method: "PATCH", path: `/sms/topics/${enc(id)}`, body });
474
+ }
475
+ delete(id) {
476
+ return this.http.request({ method: "DELETE", path: `/sms/topics/${enc(id)}` });
477
+ }
478
+ };
264
479
  var Email = class extends Resource {
265
480
  topics;
266
481
  campaigns;
@@ -377,6 +592,9 @@ var Contacts = class extends Resource {
377
592
  getImport(id) {
378
593
  return this.http.request({ method: "GET", path: `/contacts/imports/${enc(id)}` });
379
594
  }
595
+ cancelImport(id) {
596
+ return this.http.request({ method: "POST", path: `/contacts/imports/${enc(id)}/cancel` });
597
+ }
380
598
  };
381
599
  var ContactFields = class extends Resource {
382
600
  list(params = {}) {
@@ -394,13 +612,33 @@ var ContactFields = class extends Resource {
394
612
  update(id, body) {
395
613
  return this.http.request({ method: "PATCH", path: `/contact-fields/${enc(id)}`, body });
396
614
  }
615
+ /**
616
+ * `null` (204) when the field was deleted at once; a `Job` (202) when it
617
+ * held a value on more than 5,000 contacts and the delete runs in the
618
+ * background — follow it with `jobs.get(job.id)`.
619
+ */
397
620
  delete(id) {
398
621
  return this.http.request({ method: "DELETE", path: `/contact-fields/${enc(id)}` });
399
622
  }
623
+ /**
624
+ * The renamed `ContactField` (200), or a `Job` (202) when more than 5,000
625
+ * contacts hold the field: the key is already renamed and the values move in
626
+ * the background — follow it with `jobs.get(job.id)`. Tell them apart with
627
+ * `object` (`"contact_field"` / `"job"`).
628
+ */
400
629
  rename(id, body) {
401
630
  return this.http.request({ method: "POST", path: `/contact-fields/${enc(id)}/rename`, body });
402
631
  }
403
632
  };
633
+ var Jobs = class extends Resource {
634
+ /**
635
+ * Retrieve a background job — the `202` answer of an operation too large for
636
+ * one request. Poll until `status` is `COMPLETED`, `FAILED` or `CANCELED`.
637
+ */
638
+ get(id) {
639
+ return this.http.request({ method: "GET", path: `/jobs/${enc(id)}` });
640
+ }
641
+ };
404
642
  var Segments = class extends Resource {
405
643
  list(params = {}) {
406
644
  return this.http.request({ method: "GET", path: "/segments", query: params });
@@ -479,8 +717,13 @@ var Campaigns = class extends Resource {
479
717
  delete(id) {
480
718
  return this.http.request({ method: "DELETE", path: `/email/campaigns/${enc(id)}` });
481
719
  }
482
- send(id) {
483
- return this.http.request({ method: "POST", path: `/email/campaigns/${enc(id)}/send` });
720
+ /**
721
+ * Send a draft. `{}` (the default) arms its saved `scheduled_at` while that
722
+ * is still ahead, otherwise sends NOW; `{ scheduled_at: "<ISO 8601>" }`
723
+ * schedules it; `{ scheduled_at: null }` sends now.
724
+ */
725
+ send(id, body = {}) {
726
+ return this.http.request({ method: "POST", path: `/email/campaigns/${enc(id)}/send`, body });
484
727
  }
485
728
  cancel(id) {
486
729
  return this.http.request({ method: "POST", path: `/email/campaigns/${enc(id)}/cancel` });
@@ -496,7 +739,11 @@ var Campaigns = class extends Resource {
496
739
  reschedule(id, body) {
497
740
  return this.http.request({ method: "POST", path: `/email/campaigns/${enc(id)}/reschedule`, body });
498
741
  }
499
- /** Non-mutating pre-send validation report. */
742
+ /**
743
+ * Non-mutating pre-send validation report. A large audience is counted in
744
+ * the background: the first call returns that `Job` (202). Call again until
745
+ * it returns the report.
746
+ */
500
747
  validate(id) {
501
748
  return this.http.request({ method: "GET", path: `/email/campaigns/${enc(id)}/validate` });
502
749
  }
@@ -520,6 +767,14 @@ var Templates = class extends Resource {
520
767
  delete(id) {
521
768
  return this.http.request({ method: "DELETE", path: `/email/templates/${enc(id)}` });
522
769
  }
770
+ /**
771
+ * Send ONE test of the template to `to`, rendered with sample `variables`
772
+ * exactly as a recipient gets it. Without a verified domain only workspace
773
+ * members may receive it.
774
+ */
775
+ testSend(id, body) {
776
+ return this.http.request({ method: "POST", path: `/email/templates/${enc(id)}/test-send`, body });
777
+ }
523
778
  };
524
779
  var BrandKits = class extends Resource {
525
780
  /** List profiles (max 5). Optional `name` filters by display name. */
@@ -554,6 +809,10 @@ var Domains = class extends Resource {
554
809
  verify(id) {
555
810
  return this.http.request({ method: "POST", path: `/email/domains/${enc(id)}/verify` });
556
811
  }
812
+ /** Explain the domain's DNS: every nameserver's answer per record, and ranked findings with fixes. Read-only. */
813
+ diagnose(id) {
814
+ return this.http.request({ method: "GET", path: `/email/domains/${enc(id)}/diagnose` });
815
+ }
557
816
  };
558
817
  var Webhooks = class extends Resource {
559
818
  list(params = {}) {
@@ -579,6 +838,21 @@ var Webhooks = class extends Resource {
579
838
  test(id) {
580
839
  return this.http.request({ method: "POST", path: `/webhooks/${enc(id)}/test` });
581
840
  }
841
+ /**
842
+ * Mint a new signing secret. The response carries it exactly once; the old
843
+ * secret co-signs deliveries until `previous_secret_expires_at` (24 h).
844
+ */
845
+ rotateSecret(id) {
846
+ return this.http.request({ method: "POST", path: `/webhooks/${enc(id)}/rotate-secret` });
847
+ }
848
+ /** The endpoint's delivery log, newest first (cursor-paginated). */
849
+ listDeliveries(id, params = {}) {
850
+ return this.http.request({ method: "GET", path: `/webhooks/${enc(id)}/deliveries`, query: params });
851
+ }
852
+ /** Async-iterate every delivery of the endpoint. */
853
+ listAllDeliveries(id, params = {}) {
854
+ return paginate(this.http, `/webhooks/${enc(id)}/deliveries`, params);
855
+ }
582
856
  /**
583
857
  * Verify a webhook delivery's signature and return the parsed event. Throws
584
858
  * `WebhookVerificationError` on a missing/invalid/stale signature. Alias of the
@@ -604,7 +878,10 @@ var ApiKeys = class extends Resource {
604
878
  }
605
879
  };
606
880
  var Suppressions = class extends Resource {
607
- /** List suppressed addresses (your own + read-only platform rows). */
881
+ /**
882
+ * List your workspace's suppressed addresses. Unitpost-wide (platform)
883
+ * blocks are enforced at send time but not listed — `get(email)` explains one.
884
+ */
608
885
  list(params = {}) {
609
886
  return this.http.request({ method: "GET", path: "/suppressions", query: params });
610
887
  }
@@ -646,8 +923,14 @@ function enc(segment) {
646
923
  // src/client.ts
647
924
  var Unitpost = class {
648
925
  email;
926
+ /**
927
+ * SMS channel. Available on paid plans: on the free plan `send` returns a
928
+ * 403 with code `limit_reached`; reads keep working.
929
+ */
930
+ sms;
649
931
  contacts;
650
932
  contactFields;
933
+ jobs;
651
934
  segments;
652
935
  brandKits;
653
936
  webhooks;
@@ -660,8 +943,10 @@ var Unitpost = class {
660
943
  const options = typeof apiKeyOrOptions === "string" ? { apiKey: apiKeyOrOptions } : apiKeyOrOptions ?? {};
661
944
  const http = new HttpClient(options);
662
945
  this.email = new Email(http);
946
+ this.sms = new Sms(http);
663
947
  this.contacts = new Contacts(http);
664
948
  this.contactFields = new ContactFields(http);
949
+ this.jobs = new Jobs(http);
665
950
  this.segments = new Segments(http);
666
951
  this.brandKits = new BrandKits(http);
667
952
  this.webhooks = new Webhooks(http);