@ambasada/miniapp

中文文档
0.8.19发布于 2026/11/06MITTypeScriptES Module零依赖近 30 天下载 4548

Run your web app inside the Ambasada app as a mini app: ready(), shell context, safe areas, open links, haptics, notifications. Zero dependencies; also a

原始使用文档
文档目录4 节
  1. Install
  2. API
  3. The bridge protocol
  4. Build

@ambasada/miniapp

The page side of the Ambasada app's mini-app shell. Call ready() when your first screen is drawn, read the launch context, respect the safe areas and the host bar, open links and other mini apps, ask for haptics, silent sign-in and notifications. Outside the Ambasada app every call is a safe no-op, so the same page is also a plain website. Zero dependencies, ~3 KB.

Install

npm install @ambasada/miniapp
import { ready, context, on } from "@ambasada/miniapp";

ready(); // after the first render: the shell drops the splash

Or, with no build step, the <script> build the hub serves (defines window.ambasada):

<script src="https://hub.ambasada.id/miniapp.js" defer></script>
<script>addEventListener("DOMContentLoaded", () => window.ambasada?.ready?.())</script>

The ambasada init templates ship both forms, and ambasada check warns when a page uses neither.

API

CallInside the appOutside
ready()tells the shell the page is drawn (once; idempotent); sets the CSS variablessets the CSS variables to 0
shell()the injected window.ambasadaShell (version ≥ 2)null
isInAmbasada()true (also for older builds that only set the AmbasadaChat/ user agent)false
context(){ app, platform, safeArea, hostBar, theme, locale, user, location }, with defaults for what an older shell omitsthe defaults
capabilities()what this shell build supports (version 2: openUrl, close)[]
applyCssVars()writes --ambasada-safe-area-{top,bottom,left,right} and --ambasada-host-bar on <html> and toggles the in-ambasada classsame, with zeros
openUrl(url)the system browser (verified apps) or a confirm sheeta new tab
openApp(id, path?)another mini app on top of this onehttps://ambasada.id/a/<id><path> in a new tab
close()closes this mini apphistory.back()
share({ url?, text? })the system share sheetthe Web Share API, or nothing
haptics.impact(style?), .notification(style?), .selection()hapticsnothing
setTitle(title)the host bar's title and document.titledocument.title
auth.getToken()a 60 s JWT (aud = your origin or app id, sub = the Ambasada user id), for installed apps whose Install granted identity; verify it against https://hub.ambasada.id/.well-known/jwks.json (auth.verifyShellToken in @ambasada/sdk)rejects: fall back to Sign in with Ambasada
auth.signInSilently({ endpoint?, me?, timeoutMs? })on open: when nobody is signed in, getToken() and POST it to /api/auth/shell, which sets your session; resolves the user. Once per page load, 5 s at mostnull, never rejects: show "Sign in with Ambasada"
auth.canSignInSilently()inside the app, a shell that mints tokens, a person in context.userfalse
notifications.request()the shell's permission sheet → { enabled }; your webhook gets notifications_enabled{ enabled: false }
on("context", fn) / on("notification", fn)a new context (safe areas, theme) / a notification tap { id, url }; returns the unsubscribenever fires
mockEnv(overrides?)local development: installs a version 3 shell, records messages, answers requests, returns { messages, emit, uninstall }same (ambasada dev uses it)

Layout: pad your first screen by var(--ambasada-safe-area-top, 0px) + var(--ambasada-host-bar, 0px) at the top and var(--ambasada-safe-area-bottom, 0px) at the bottom. The 0px fallbacks keep the page right on the web.

The bridge protocol

The native app injects window.ambasadaShell before the page loads:

{ version: 3, platform: "ios" | "android" | "web", capabilities?: Capability[],
  app: { id, tier } | null, safeArea: { top, bottom, left, right }, hostBar: number,
  theme: { accent, scheme: "light" }, locale, user: { id, name, avatar } | null, location: LaunchLocation }

Version 2 shells (the current app) carry only version, platform and safeArea; the SDK falls back to the v2 messages (chrome, open) for them.

Page → app: one JSON message per call, through webkit.messageHandlers.ambasada.postMessage(obj) on iOS or window.ambasada.postMessage(json) on Android:

MessageFields
ready
openUrlurl
openAppapp, path?
close
shareurl?, text?
haptickind: impact | notification | selection, style?
titletitle
requestid, method: auth.getToken | notifications.request
chrome (v2)header?, tabs?, title?, back?, statusBar?
open (v2)url, mode: in-app | external | sheet

App → page: CustomEvents dispatched on window:

Eventdetail
ambasada:reply{ id, result } or { id, error }, answering a request (the SDK gives up after 15 s)
ambasada:contexta (partial) new ShellContext; the SDK merges it into window.ambasadaShell and re-applies the CSS variables
ambasada:notification{ id, url }, a notification tapped while this app is open

The <script> build merges into an existing window.ambasada (Android's bridge object) instead of replacing it, so window.ambasada.postMessage keeps working next to window.ambasada.ready().

Build

node scripts/build.mjs writes dist/index.js (ESM), dist/miniapp.js (classic script, global ambasada) and dist/index.d.ts. The hub serves dist/miniapp.js at https://hub.ambasada.id/miniapp.js.

MIT, CENTYSO.

版本与发布记录

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

版本发布日期距今
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/miniapp 的常见问题

@ambasada/miniapp 是什么?

Run your web app inside the Ambasada app as a mini app: ready(), shell context, safe areas, open links, haptics, notifications. Zero dependencies; also a

如何安装 @ambasada/miniapp?

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

@ambasada/miniapp 支持 TypeScript 吗?

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

@ambasada/miniapp 支持 ESM 还是 CommonJS?

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

@ambasada/miniapp 可以商用吗?

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

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

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

@ambasada/miniapp 还在维护吗?

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