TypeScript SDK
Call OpenPost from Node.js, Bun, or Workers with @getopenpost/sdk.
Call OpenPost from JavaScript or TypeScript when a script, CI job, or agent needs publications, media, or job status over HTTP. The SDK has no runtime dependencies and works on Node.js 20 and newer, Bun, and Cloudflare Workers. For shell work use the CLI; for raw HTTP use the API reference.
Install and connect
npm install @getopenpost/sdkCreate a token in Settings → Personal → Developer with the permissions your script needs, then connect:
import { OpenPost } from "@getopenpost/sdk";
const openpost = new OpenPost({
baseUrl: "https://app.openpo.st",
token: process.env.OPENPOST_TOKEN,
workspaceId: process.env.OPENPOST_WORKSPACE_ID,
});baseUrl defaults to Hosted. Point it at your origin when you self-host. The workspace default also accepts OPENPOST_WORKSPACE, shared with the CLI.
Draft, validate, and schedule
Publications are the authored record; renditions are the per-account versions. Copy Social Set defaults into renditions when you create the draft, and pass the revision you last saw to every lifecycle action so a stale script fails with a conflict instead of overwriting edits:
const draft = await openpost.publications.create({
workspace_id: await openpost.workspaceId(),
title: "Launch day",
content_profile: "short_text",
source_text: "We just shipped our TypeScript SDK.",
social_account_ids: ["acc_..."],
});
await openpost.publications.upsertRenditions(draft.id, draft.revision, [
{ social_account_id: "acc_...", body: "We just shipped our TypeScript SDK." },
]);
await openpost.publications.validate(draft.id, { throwOnInvalid: true });
await openpost.publications.schedule(draft.id, draft.revision);
const done = await openpost.publications.wait(draft.id);publishNow, cancel, and retryFailed follow the same revision pattern. events lists the publication lifecycle for inspection and retries.
Upload media
upload hides the session flow: it reserves the media row, PUTs the bytes to the storage target, and completes the row. External storage targets never receive your API token:
const asset = await openpost.media.upload({
workspaceId: "ws_...",
file: new Uint8Array(await Bun.file("cover.png").arrayBuffer()),
filename: "cover.png",
mimeType: "image/png",
altText: "Launch cover art",
});Handle errors
Every failure throws OpenPostError with a machine-readable code:
import { OpenPostError } from "@getopenpost/sdk";
try {
await openpost.publications.schedule(id, revision);
} catch (error) {
if (error instanceof OpenPostError && error.code === "conflict") {
// Someone edited the publication. Re-read it and retry.
}
if (error instanceof OpenPostError && error.retryable) {
// Rate limits, server errors, timeouts, and network faults.
}
}missing_config means the token or workspace is unset. validation means the input needs fixing. Publishing actions return a job_id when the work continues in the background; use jobs.wait when the caller needs the final outcome.
Next steps
- Prefer a terminal? Install the CLI, including from npm.
- Need an endpoint the SDK does not cover yet? Call it directly with the API reference using the same token.