TypeScript SDK

A fully-typed client for Node.js and the browser.

Install

$npm install @usgm/sdk

Requires Node.js 18+.

Initialize

1import { UsgmClient } from "@usgm/sdk";
2
3const usgm = new UsgmClient({
4 token: process.env.USGM_API_KEY!, // never hard-code the key
5});

Usage

1// Your account profile
2const account = await usgm.account.get();
3console.log(account.email, account.status);
4
5// List mail, one page at a time (cursor pagination)
6let cursor: string | undefined;
7do {
8 const page = await usgm.mails.list({ cursor });
9 for (const mail of page.data) {
10 console.log(mail.id, mail.sender_name);
11 }
12 cursor = page.next_cursor ?? undefined;
13} while (cursor);
14
15// Fetch one item
16const mail = await usgm.mails.get({ id: "80421" });
17
18// Create a folder
19const folder = await usgm.folders.create({ name: "Taxes" });

Error handling

1import { UsgmError } from "@usgm/sdk";
2
3try {
4 await usgm.mails.get({ id: "does-not-exist" });
5} catch (err) {
6 if (err instanceof UsgmError) {
7 // `body` is the RFC 9457 problem+json payload (code, request_id, detail, …)
8 console.error(err.statusCode, err.body);
9 }
10 throw err;
11}

Transient 429 and 5xx responses are retried automatically with exponential backoff. See Errors for the full problem+json contract.