|
| 1 | +# spoo.me TypeScript SDK |
| 2 | + |
| 3 | +The official TypeScript SDK for the [spoo.me](https://spoo.me) link management API. |
| 4 | + |
| 5 | +```ts |
| 6 | +import { Spoo } from "spoo.me"; |
| 7 | + |
| 8 | +const spoo = new Spoo({ apiKey: "spoo_..." }); |
| 9 | + |
| 10 | +const link = await spoo.links.create({ long_url: "https://example.com/launch" }); |
| 11 | +console.log(link.short_url); // https://spoo.me/xyz |
| 12 | +``` |
| 13 | + |
| 14 | +- Zero runtime dependencies, built on global `fetch` |
| 15 | +- Runs on Node 20+, Cloudflare Workers, Vercel Edge, Deno, Bun and browsers |
| 16 | +- Typed errors, automatic retries, async-iterator pagination |
| 17 | +- Types generated from the API's OpenAPI spec, so they cannot drift |
| 18 | + |
| 19 | +## Install |
| 20 | + |
| 21 | +```sh |
| 22 | +npm install spoo.me |
| 23 | +``` |
| 24 | + |
| 25 | +## Authentication |
| 26 | + |
| 27 | +Create an API key from your [spoo.me dashboard](https://spoo.me). The client |
| 28 | +reads `SPOO_API_KEY` from the environment, or takes the key explicitly: |
| 29 | + |
| 30 | +```ts |
| 31 | +const spoo = new Spoo(); // uses SPOO_API_KEY |
| 32 | +const spoo = new Spoo({ apiKey: "spoo_..." }); // explicit |
| 33 | +const spoo = new Spoo({ token: async () => myJwt }); // app tokens |
| 34 | +``` |
| 35 | + |
| 36 | +Constructing without credentials is valid too: anonymous shortening and the |
| 37 | +public endpoints work without an account. |
| 38 | + |
| 39 | +Self-hosting spoo.me? Point the client at your instance with |
| 40 | +`new Spoo({ baseUrl: "https://links.example.com" })`. |
| 41 | + |
| 42 | +## Shorten links |
| 43 | + |
| 44 | +```ts |
| 45 | +const link = await spoo.links.create({ |
| 46 | + long_url: "https://example.com/launch", |
| 47 | + alias: "launch", // or emoji: "🚀🔥" |
| 48 | + password: "optional-password", |
| 49 | + max_clicks: 10_000, |
| 50 | + expire_after: new Date("2026-12-31T23:59:59Z"), |
| 51 | +}); |
| 52 | +``` |
| 53 | + |
| 54 | +Timestamps are accepted as `Date`, ISO 8601 strings, or unix epoch seconds |
| 55 | +everywhere, and returned as `Date` objects everywhere. |
| 56 | + |
| 57 | +Anonymous creations return a one-time `claim_token`. Store it and the link can |
| 58 | +be claimed into an account later with `spoo.links.claim()`. |
| 59 | + |
| 60 | +## Manage links |
| 61 | + |
| 62 | +```ts |
| 63 | +const link = await spoo.links.get(id); |
| 64 | +await spoo.links.update(id, { max_clicks: 500 }); |
| 65 | +await spoo.links.setStatus(id, "INACTIVE"); |
| 66 | +await spoo.links.delete(id); |
| 67 | +``` |
| 68 | + |
| 69 | +Bulk operations take up to 100 ids and report per-item results instead of |
| 70 | +throwing, so a partial failure never aborts the batch: |
| 71 | + |
| 72 | +```ts |
| 73 | +const result = await spoo.links.bulk.setStatus(ids, "INACTIVE"); |
| 74 | +console.log(result.summary); // { total, succeeded, failed } |
| 75 | +``` |
| 76 | + |
| 77 | +## Pagination |
| 78 | + |
| 79 | +Every list is a `Page`: use it directly, walk it by hand, or iterate items |
| 80 | +across all pages with `for await`. |
| 81 | + |
| 82 | +```ts |
| 83 | +for await (const link of await spoo.links.list({ sortBy: "total_clicks" })) { |
| 84 | + console.log(link.alias, link.total_clicks); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Analytics |
| 89 | + |
| 90 | +```ts |
| 91 | +// Aggregate across everything you own, sliced and filtered |
| 92 | +const stats = await spoo.stats.get({ |
| 93 | + startDate: new Date("2026-01-01"), |
| 94 | + groupBy: ["time", "country"], |
| 95 | + device: ["mobile"], |
| 96 | + timezone: "Asia/Kolkata", |
| 97 | +}); |
| 98 | + |
| 99 | +// One link, by id |
| 100 | +const one = await spoo.stats.getForLink(id, { groupBy: ["referrer"] }); |
| 101 | + |
| 102 | +// File exports (csv is a ZIP archive with one CSV per dimension) |
| 103 | +const file = await spoo.stats.export({ groupBy: ["country"] }, "xlsx"); |
| 104 | +``` |
| 105 | + |
| 106 | +Public per-link stats need no account: |
| 107 | + |
| 108 | +```ts |
| 109 | +const stats = await spoo.public.stats("alias"); |
| 110 | +const preview = await spoo.public.preview("alias"); |
| 111 | +``` |
| 112 | + |
| 113 | +## Errors |
| 114 | + |
| 115 | +Failed requests throw a typed subclass of `SpooError`: |
| 116 | + |
| 117 | +| Status | Class | |
| 118 | +| --- | --- | |
| 119 | +| 400, 422 | `ValidationError` | |
| 120 | +| 401 | `AuthenticationError` | |
| 121 | +| 403 | `ForbiddenError` | |
| 122 | +| 404 | `NotFoundError` | |
| 123 | +| 409 | `ConflictError` | |
| 124 | +| 410 | `GoneError` | |
| 125 | +| 413 | `PayloadTooLargeError` | |
| 126 | +| 429 | `RateLimitError` | |
| 127 | +| 451 | `ContentBlockedError` | |
| 128 | +| 5xx | `InternalServerError`, `ServiceUnavailableError` | |
| 129 | +| (no response) | `APIConnectionError`, `APITimeoutError` | |
| 130 | + |
| 131 | +Every error carries the machine-readable `code` from the API (a typed union |
| 132 | +such as `"password_required"`, `"blocked"`, `"conflict"`), the `requestId` to |
| 133 | +quote in support requests, and the response headers. `RateLimitError` also |
| 134 | +exposes the parsed rate-limit state: |
| 135 | + |
| 136 | +```ts |
| 137 | +try { |
| 138 | + await spoo.links.create({ long_url }); |
| 139 | +} catch (err) { |
| 140 | + if (err instanceof RateLimitError) { |
| 141 | + console.log(err.rateLimit.retryAfter, err.hint); |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +## Retries and timeouts |
| 147 | + |
| 148 | +Failed requests are retried twice by default with exponential backoff and |
| 149 | +jitter, honoring the `Retry-After` header on 429 responses. Retries and the |
| 150 | +60 second timeout are configurable per client and per request: |
| 151 | + |
| 152 | +```ts |
| 153 | +const spoo = new Spoo({ maxRetries: 3, timeout: 15_000 }); |
| 154 | +await spoo.links.get(id, { maxRetries: 0, signal: controller.signal }); |
| 155 | +``` |
| 156 | + |
| 157 | +Requests that are not idempotent are only retried when the server provably |
| 158 | +did no work. |
| 159 | + |
| 160 | +## Requirements |
| 161 | + |
| 162 | +Node 20 or later, or any runtime with WHATWG `fetch`: Cloudflare Workers, |
| 163 | +Vercel Edge, Deno, Bun, evergreen browsers. The package is ESM only. |
| 164 | + |
| 165 | +Using an API key in a browser exposes it to every visitor, so the client |
| 166 | +refuses to start with a key in a browser unless you pass |
| 167 | +`dangerouslyAllowBrowser: true`. Keyless anonymous usage needs no flag. |
| 168 | + |
| 169 | +## Versioning |
| 170 | + |
| 171 | +The SDK follows SemVer and is currently 0.x while the surface settles. New |
| 172 | +API endpoints and new optional fields ship as minor versions. Response types |
| 173 | +can gain fields at any time; the SDK does not validate responses at runtime, |
| 174 | +so additive API changes never break an installed version. |
| 175 | + |
| 176 | +## More |
| 177 | + |
| 178 | +Runnable samples live in [`examples/`](./examples). Full API documentation is |
| 179 | +at [docs.spoo.me](https://docs.spoo.me). |
| 180 | + |
| 181 | +## License |
| 182 | + |
| 183 | +[AGPL-3.0](./LICENSE) |
0 commit comments