@bmap-analytics/node
v0.12.0
Published
BMAP server SDK — mint embed tokens and manage applications from your backend.
Maintainers
Readme
@bmap-analytics/node
Mint BMAP embed tokens from your backend.
npm install @bmap-analytics/nodeQuick start
import { Bmap } from "@bmap-analytics/node";
const bmap = new Bmap({
apiKey: process.env.BMAP_API_KEY!,
baseUrl: "https://analytics.acme.com",
});
// Express, Fastify, Next route handler — anything.
app.get("/api/bmap-token", async (req, res) => {
const session = await bmap.embed.createToken({
dashboardId: req.query.dashboard as string,
// Resolved from YOUR session. Never from the request body.
context: { region: req.user.region, accountId: req.user.accountId },
ttlSeconds: 600,
});
res.json({ token: session.token, expiresAt: session.expiresAt });
});Hand token to the browser and nothing else. @bmap-analytics/react takes it from there.
This package must not run in a browser
The constructor throws if it detects one. It holds your API key, and an API key in a browser lets any viewer mint a token with any security context they like — which is the one thing embedding exists to prevent.
The check looks for a window with a document, so a server runtime that resembles a
browser — an Electron main process, Deno — trips it. allowBrowser: true is the escape
hatch for exactly that, and for nothing else: a real browser with the flag set is still a
browser holding your API key.
API
bmap.embed.createToken(params)
| Param | | |
|---|---|---|
| dashboardId | required | Must be published, and granted to this application. |
| context | | The viewer's security context. Sealed into the signed token. |
| ttlSeconds | | Default 600. Short by design; the client SDK refreshes silently. |
| origin | | Pin the token to one origin. Presented elsewhere, it is refused. |
Returns { token, sessionId, dashboardId, expiresAt }.
bmap.embed.listSessions({ applicationId? })
Every token minted, for audit. contextHash is a hash — BMAP never stores the context
values themselves, because these rows are retained and would otherwise accumulate your
customers' identifiers.
bmap.embed.revokeSession(sessionId)
Cuts a viewer off on their next request, not at token expiry — so it is usable during an incident rather than being a ten-minute wait.
bmap.dashboards.list({ grantedTo?, status? })
The menu behind a dashboard picker: which dashboards this integration may actually show.
const dashboards = await bmap.dashboards.list({ grantedTo: process.env.BMAP_APPLICATION_ID });
// [{ id, name, description, status, currentVersionId }, …]With grantedTo, the list is narrowed to what createToken will accept — granted to that
application and published. Both halves matter: a grant without a published version is a
menu item that mints nothing, and a published dashboard nobody granted you is not yours to
show. Without grantedTo you get every dashboard in the tenant, which is the right call for
an internal admin screen and the wrong one for a customer-facing picker.
This talks to BMAP directly. You do not need a route of your own in front of it — a forwarding endpoint between your server and BMAP would transform nothing.
Errors
Every failure is a BmapError with a code:
import { BmapError } from "@bmap-analytics/node";
try {
await bmap.embed.createToken({ dashboardId });
} catch (error) {
if (error instanceof BmapError && error.code === "forbidden") {
// Usually: this application has no grant for that dashboard.
}
}Setup checklist
Each is enforced when you mint, so a missing one produces a clear 403 rather than a puzzling empty dashboard:
- An application —
POST /v1/developer/applications - An API key for it — the secret is shown once
- A grant for the dashboard — an allow-list, so a leaked key reaches only what you shared
- The dashboard is published
Node 18+ (needs a global fetch), ESM and CJS, TypeScript declarations, source maps.
