@apostra/sdk

中文文档
0.41.0发布于 2026/10/31Apache-2.0TypeScriptES Module构建来源已验证

The official Apostra V3 HTTP SDK

原始使用文档

Apostra TypeScript SDK

Typed calls to the public V3 HTTP API. Requires Node.js 22.18 or later.

npm install @apostra/sdk

Start with the SDK quickstart. It covers account discovery, a campaign launch preview and confirmation, and delivery reporting. The runnable synthetic-seller example is in the SDK source checkout. It previews by default and only confirms a launch when APOSTRA_CONFIRM_LAUNCH=true is set deliberately.

import { Apostra } from '@apostra/sdk'
const api = new Apostra()
const status = await api.getStatus()

The client reads APOSTRA_API_KEY, APOSTRA_ACCOUNT_ID and APOSTRA_BASE_URL by default. Explicit constructor options override those values. Use accessToken for an existing REST/M2M bearer token, or tokenProvider for application-managed acquisition and refresh. Supply exactly one credential source. m2mTokenProvider acquires and caches a server-side client-credentials token with the published M2M endpoint; it refreshes 60 seconds before expiry and shares a concurrent refresh. Never put API keys or client secrets in browser bundles, URLs, examples or logs. With no explicit credential and no APOSTRA_API_KEY, a Node.js client uses the login that apostra login stored for the base URL, if there is one; the SDK never opens a browser and writes only to that login's file (refreshed tokens). The package does not replay 401 responses. M2M token refreshes have a 30-second deadline by default; pass timeoutMs to override it.

import { Apostra, m2mTokenProvider } from '@apostra/sdk'

const api = new Apostra({
  tokenProvider: m2mTokenProvider({
    clientId: process.env.APOSTRA_CLIENT_ID!,
    clientSecret: process.env.APOSTRA_CLIENT_SECRET!,
    scope: 'interchange:read',
  }),
})

Methods accept the generated wire input and return the envelope's data. Every generated method also has a WithResponse variant, such as getStatusWithResponse. It returns { data, requestId, headers, status } for successful calls, so support tools can retain x-request-id and response headers without dispatching by a wire operation name. ApostraError exposes status, code, recovery, retryAfter, retryable, the typed error, and requestId. Catch RateLimitError, ValidationError, or another typed subclass instead of parsing an exception string. The default exception string excludes response text. Static types do not replace server validation of patterns, lengths, conditional requirements or permissions.

Read calls accept { accountId, signal, timeoutMs }; write calls require those options to include a caller-owned { idempotencyKey }. The SDK sends it as Idempotency-Key, and never creates or replaces it. Account selection uses X-SCOPE3-CUSTOMER-ID; it must match the account resolved by the credential. Cancellation stops waiting for HTTP; it does not undo server work. Requests have a 30-second timeout, do not follow redirects, and retry transient network, 429 and 5xx failures twice by default with full-jitter backoff. Set maxRetries: 0 to opt out. Reads retry automatically. Writes retry only with the same caller-owned idempotency key, which generated write methods require. The SDK never creates or replaces that key. Server retry_after is a minimum retry delay.

const { data, requestId, headers } = await api.getStatusWithResponse()
const correlationId = headers.get('x-request-id')

A 202 raises InFlightReceiptError rather than returning a completed value. Use settle to replay the same keyed operation until it completes; it honours the receipt's Retry-After, deadline and cancellation signal:

const key = 'preview-123'
const result = await settle(
  (signal) => api.saveAsk(input, { idempotencyKey: key, signal }),
  { timeoutMs: 30_000 },
)

paginate and poll take typed callbacks. Preserve original query parameters, revisions and keys; provide the operation's cursor/status accessor. No helper assumes that a POST is safe to retry without its caller-owned key or that every list has the same cursor. Polling passes its signal to read(signal); pagination passes it to read(cursor, signal). Forward that signal into the SDK call to cancel the underlying HTTP request. Timeouts cover credentials, HTTP and response-body consumption; an injected fetch/provider must cooperate with cancellation to stop its own work. Cancellation still stops the SDK from waiting.

Import verifyWebhook from the server-only @apostra/sdk/webhooks subpath. It validates duplicate-preserving V3 webhook headers, the raw body, the five-minute delivery window, and a rotating key map before you parse JSON. Provide every raw occurrence of signature, timestamp, and deliveryId; do not flatten duplicate headers or log secrets. The browser-safe @apostra/sdk root does not load Node cryptography.

openHandoff(url, opener?) accepts an actual HTTPS URL returned by the API. Without an opener it just returns the URL. Some UI operations currently return only parameters. URL construction, login, wait/resume, and byte uploads are unsupported unless a future public contract defines them. getHandoff(result) narrows a supported human-action result to its kind, url, expiresAt and resume instructions, or returns null. Pass that typed value to openHandoff(handoff, opener?); without an opener it only returns the supplied HTTPS URL. Opening or closing the browser is not completion: follow resume and read the affected object before continuing. The SDK never constructs a URL or embeds a login flow. uploadCreativeAsset exposes the current typed human task and fallback URL.

@apostra/sdk/metadata exports generated operation names, paths, security and parameter descriptions for CLI discovery. The SDK has no terminal, browser, profile-store or MCP dependencies. Import wire models from @apostra/sdk/models.

Source and releases: https://github.com/apostra-public/apostra

版本与发布记录

@apostra/sdk 共发布 6 个版本,首次发布于 2026/10/13;过去 12 个月发布了 6 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.41.0latest2026/10/312 天前
0.40.02026/10/277 天前
0.39.02026/10/268 天前
0.38.02026/10/1815 天前
0.37.02026/10/1321 天前
0.0.0-stage2026/10/1321 天前

关于 @apostra/sdk 的常见问题

@apostra/sdk 是什么?

官方Apostra V3 HTTP SDK

如何安装 @apostra/sdk?

在项目目录运行 npm install @apostra/sdk,或使用 pnpm add / yarn add / bun add。

@apostra/sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@apostra/sdk 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@apostra/sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.18.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apostra/sdk 可以商用吗?

@apostra/sdk 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@apostra/sdk 还在维护吗?

最新版本 0.41.0 发布于 2026/10/31(2 天前);至今共发布 6 个版本,其中过去 12 个月发布了 6 个;首次发布于 2026/10/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。