Browse the docs
Developers
JavaScript SDK (@asdk/js)
Typed helpers for the widget's asdk() API in the browser, a typed client for the REST API on your server, and webhook signature verification.
Last updated
On this page
@asdk/js is ASDK's JavaScript and TypeScript SDK: ES modules with types, built from the same API contract as the API reference.
| Import | Where it runs | What it does |
|---|---|---|
@asdk/js/widget | The browser | Typed asdk() commands and events, loadAsdk() to add the embed script |
@asdk/js/client | Your server | createAsdkClient({ apiKey }) for the REST API |
@asdk/js/webhooks | Your server, edge functions | verifyWebhook() for webhook deliveries |
In the browser
import { asdk, loadAsdk, onAsdk } from "@asdk/js/widget";
loadAsdk({ siteId: "site_AbCdEfGhIjKlMnOpQrSt" });
asdk("identify", { email: user.email, name: user.name });
const stop = onAsdk("lead", (lead) => {
analytics.track("Lead", { leadId: lead.leadId });
});loadAsdk() adds the embed script once. Calls made before the widget has loaded are queued and replayed in order, exactly like the inline snippet, and on the server every helper does nothing, so they are safe in frameworks that render on the server. The commands are open, close, toggle, identify, track, on, off, setLocale, search and shutdown (call it when a person signs out); the events are open, close, lead, message and meeting. Importing the module also types window.asdk and window.asdkConfig.
On your server
import { AsdkError, createAsdkClient } from "@asdk/js/client";
const asdk = createAsdkClient({ apiKey: process.env.ASDK_API_KEY! });
const { answer, sources, grounded } = await asdk.ask("site_…", {
question: "Do you ship to Canada?",
});
const { leads } = await asdk.leads.list("wsp_…", { status: "new" });The helpers cover the common calls (sites, knowledge, leads, webhooks, ask) and throw an AsdkError with the problem's status, type, field and retryAfter. asdk.api reaches every endpoint of the reference with typed paths, parameters and bodies:
const { data, error } = await asdk.api.GET("/v1/sites/{siteId}/sources", {
params: { path: { siteId: "site_…" } },
});Webhooks
import { verifyWebhook, WebhookSignatureError } from "@asdk/js/webhooks";
export async function POST(request: Request) {
try {
const event = await verifyWebhook({
secret: process.env.ASDK_WEBHOOK_SECRET!,
body: await request.text(),
signature: request.headers.get("X-ASDK-Signature"),
});
if (event.type === "lead.created") {
// event.data.lead.email
}
return new Response(null, { status: 204 });
} catch (e) {
if (e instanceof WebhookSignatureError) return new Response("bad signature", { status: 400 });
throw e;
}
}See webhooks for the events and the signing scheme.