Extend: Scripts — ad-hoc mailers, syncs, and imports
Lane 3 of Extend Tinbox — SDK, CLI and MCP access with account and scoped service credentials, including named-template render on send.
Lane 3 is the programmatic surface: a script you run on your own machine (or a Worker, a cron box, a laptop) that reaches into your Tinbox instance over HTTP. One SDK exposes sending, mailbox reads, and the agent tools your Assistant uses. Each call still requires its own current account authority or explicit service-token grants. This is the lane for the one-off mailer, the nightly CRM sync, the "import my old inbox" chore. Here's the whole thing in fifteen lines: read an audience, render a stored template per recipient, and send — idempotently.
import { Tinbox } from "tinbox/sdk";
const mailbox = new Tinbox({ host: "https://acme.dev", apiKey: process.env.TINBOX_TOKEN });
const sender = new Tinbox({ host: "https://acme.dev", apiKey: process.env.TINBOX_API_KEY });
// Read an audience with a runtime credential authorized for audience membership…
const { members } = await mailbox.api.audiences.members.query({ audienceId: "aud_weekly" });
// …and send each member a per-recipient render of a stored template, idempotently.
await sender.emails.batch(members.map((m) => ({
from: "Acme <news@acme.dev>",
to: m.email,
subject: "Your weekly digest",
template: "weekly-digest", // a template you stored (Settings → Templates)
data: { FNAME: m.name, TOP_PICK: m.topPick },
idempotencyKey: `DIGEST-${weekId}-${m.email}`,
})));
That's a real, running program. No template engine to wire up, no merge loop, no dedupe table — the server does the render and the ledger does the deduping.
The SDK
The SDK is tinbox/sdk, imported from the main tinbox npm package — one install, no separate dependency. It is dependency-free (fetch-only) and runs anywhere fetch does: Cloudflare Workers, Node 22.12+, browsers, Bun, Deno.
Construct it with your instance origin and a token:
import { Tinbox } from "tinbox/sdk";
const tb = new Tinbox({
host: "https://acme.dev", // your instance origin
apiKey: process.env.TINBOX_API_KEY, // one tbx_… capability token
});
host is your deployment's origin; the SDK derives /api/send and /api/trpc from it. You never build those URLs by hand.
Choose the credential for the job
Sign in to the tenant with your existing account to connect operator tooling:
tinbox login --api-url https://acme.dev --browser --no-open --ttl 1d
tinbox whoami --require-inbox support@acme.dev --json
The browser-approved account credential follows current account permissions.
An inbox admin can issue a separate read-only worker token with
tinbox tokens create --inbox support@acme.dev --role viewer --ttl 30d --json.
Store its one-time secret privately as TINBOX_TOKEN, paired with
TINBOX_API_URL. A viewer token cannot read audience membership or administer
the account; the audience-read example above requires an appropriately
authorized runtime credential.
For application sending, create a separate key in Settings → API keys or
with tinbox tokens create-send-key. Store that secret as TINBOX_API_KEY.
Identity, domain and account selection limit its From addresses; they do not
grant mailbox reads, MCP tools or administration. The example above deliberately
uses separate mailbox and sending clients.
Three surfaces hang off the one client:
tb.emails.*— Resend-stylesend/batch/get. The transactional lane.tb.api.*— the advanced tRPC client over authorized deployment procedures: audiences, contacts, threads, campaigns, settings. Mailbox wrappers such asinboxes.list,emails.searchandthreads.historyprovide self-contained types; the advanced passthrough remains available astb.api.*.tb.tools.*— aliases for the generated agent tools, so a script can call exactly what the Assistant calls.
Named-template render on send
The template + data pair on emails.send / emails.batch is the newest piece. Instead of shipping html from your script, you pass:
template— a stored template's id or name (e.g."weekly-digest").data— merge-tag values keyed by tag, e.g.{ FNAME: "Ada", TOP_PICK: "…" }.
The server looks up the stored template, substitutes merge tags (*|FNAME|*, conditionals) per recipient, and sends. Store templates in Settings → Templates or via the templates API.
await tb.emails.send({
from: "Acme <news@acme.dev>",
to: "ada@example.com",
subject: "Welcome",
template: "welcome",
data: { FNAME: "Ada" },
});
Two rules worth internalizing:
templateandhtmlare mutually exclusive. Send both and you get a 422 — no guessing which one wins.- A missing template is a hard error, never a silent empty send. If the server doesn't have the named template, it rejects the send with a clear error. You'll never blast a batch of blank emails because a name was mistyped.
Idempotency
Pass idempotencyKey for at-most-once business sends. A retry with the same key is a safe replay against the send ledger — the message goes out once, and a re-run of your script (a crashed batch, a cron double-fire) doesn't double-mail anyone. In the opener, DIGEST-${weekId}-${m.email} gives every recipient a stable key for the week, so the whole batch is re-runnable.
Both camelCase and snake_case are accepted throughout — replyTo/reply_to, idempotencyKey/idempotency_key — so paste from either convention and it just works.
Thread directives
The reply-and-drip directives from Lane 2 work here too: onReply and drip on a send set up automated thread behavior driven from your script. See Extend: Replies for the full grammar.
For everything the send surface accepts — attachments, headers, scheduling, tags — the fuller reference is the Sending API guide.
The CLI and MCP
The tinbox bin is the scripting lane's on-ramp for bulk moves. Its import tooling pulls existing mail and contacts into your instance:
tinbox import-gmail
tinbox import-imap
tinbox import-mailchimp
tinbox import-mbox
These are the ad-hoc scripting entry points for "migrate me onto Tinbox" — run once, glue into a script, or schedule.
MCP: the /mcp endpoint exposes the manifest tools to the Claude app on the same token. Point Claude at your instance and it can call the same tb.tools.* surface conversationally.
Which lane instead?
Scripts run outside the worker, reaching in over HTTP. If that's not the shape you need:
- To run code inside the worker — a webhook, a cron job, a custom route — that's Plugins (lane 4).
- To give the Assistant a new callable tool — a query or action it can invoke in conversation — that's Views (lane 5).
All six lanes, side by side, live on the Extend Tinbox hub.