@ambasada/sdk

中文文档
0.8.19发布于 2026/11/06MITTypeScriptES Module近 30 天下载 5776

The Ambasada platform SDK: Sign in with Ambasada, mini-app notifications, the project store, files.

原始使用文档

@ambasada/sdk

The Ambasada platform client: Sign in with Ambasada, mini-app notifications, the project store, files. Node ≥ 22, Bun, Cloudflare Workers, Next.js route handlers. No runtime dependencies.

import { ambasada } from "@ambasada/sdk"; // AMBASADA_API_KEY, AMBASADA_API_URL (default https://api.ambasada.dev)
ambasada.auth.login(req) / .callback(req) / .user(req) / .logout(req) / .shell(req)Sign in with Ambasada (below)
ambasada.apps.verifyWebhook(req) / .notify({ … })the Ambasada app's events and push notifications (docs/MINI-APPS.md §7)
ambasada.store.get(key) / .put(key, value) / .delete(key) / .list(prefix)JSON key-value, 1 MB per app
ambasada.files.put(data)a file at a public URL
ambasada.project(), ambasada.keys()the app's id; the API's signing keys
defineApp({ id, name, description, product })ambasada.config.ts

Errors are AmbasadaError with code from @ambasada/protocol (isAmbasadaError(e, "UNAUTHORIZED")), and WebhookInvalidError (400). Workers: createAmbasada({ env }). An app's own database, KV, files and Durable Objects are Worker bindings (env.DB, env.KV, env.FILES; docs/STORAGE.md), not SDK calls.

The Hack A Ton economy (TON, checkout, offerings, the agent) is the add-on @ambasada/village. Its 0.7 names (village, createVillage, VillageError, offering, defineCompany, …) are still exported here, @deprecated, until 2.0. Note that createAmbasada returns the platform client from 0.8; selling code uses createVillage.

Sign in with Ambasada

People sign in to your product with their Ambasada ID (OAuth 2.0 code flow + PKCE, consent screen). Zero setup on https://<slug>.ambasada.dev: ambasada deploy injects AMBASADA_AUTH_CLIENT_ID, AMBASADA_AUTH_CLIENT_SECRET and AMBASADA_AUTH_ISSUER; ambasada dev injects fake ones and its mock signs you in as "Fake Visitor". Fetch-style handlers for Workers and Next.js route handlers, WebCrypto only.

const ambasada = createAmbasada({ env });
// GET /api/auth/login?returnTo=/path   → redirect to the Ambasada ID consent screen
return ambasada.auth.login(req, { returnTo: "/", email: true }); // email: also ask for their email
// GET /api/auth/callback               → checks state, exchanges the code, reads the profile, sets the cookie
return ambasada.auth.callback(req);
// anywhere on the server
const user = await ambasada.auth.user(req); // AmbasadaUser | null
// GET /api/auth/logout
return ambasada.auth.logout(req, { returnTo: "/" });

AmbasadaUser is { id, name, avatar_url, builder, builder_since, member_since, pass, email? }: id is the stable Ambasada ID (user_…), pass is "builder" | "visitor" | "staff" | null for the current event, email only if you asked and they agreed. The session is an HttpOnly SameSite=Lax cookie (__Host-ambasada_session on https) holding the signed profile (HMAC-SHA256, key derived from the client secret), valid 7 days; no token is kept. returnTo must be a path on your site. user() also takes { headers } (Next server components: ambasada.auth.user({ headers: await headers() })). ambasada.auth.configured says whether credentials are set. Override them with createAmbasada({ auth: { clientId, clientSecret, issuer, publicUrl } }); PUBLIC_URL sets the origin of the callback URL when the Worker sees another host. More: docs/AUTH.md.

pnpm build writes dist/ (one ESM file + .d.ts); publishConfig points the published package at it.

Built by CENTYSO for Ambasada. MIT.

版本与发布记录

@ambasada/sdk 共发布 33 个版本,首次发布于 2026/11/02;过去 12 个月发布了 33 个版本。

版本发布日期距今
0.8.19latest2026/11/061 天前
0.8.182026/11/062 天前
0.8.172026/11/052 天前
0.8.162026/11/052 天前
0.8.152026/11/052 天前
0.8.142026/11/052 天前

关于 @ambasada/sdk 的常见问题

@ambasada/sdk 是什么?

The Ambasada platform SDK: Sign in with Ambasada, mini-app notifications, the project store, files.

如何安装 @ambasada/sdk?

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

@ambasada/sdk 支持 TypeScript 吗?

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

@ambasada/sdk 支持 ESM 还是 CommonJS?

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

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

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

@ambasada/sdk 可以商用吗?

@ambasada/sdk 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@ambasada/sdk 的下载量有多少?

根据 npm 官方下载统计,@ambasada/sdk 在 2026/10/07 – 2026/11/05 这 30 天内被下载 5,776 次,日均约 193 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@ambasada/sdk 还在维护吗?

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