Skip to content
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.

ImportWhere it runsWhat it does
@asdk/js/widgetThe browserTyped asdk() commands and events, loadAsdk() to add the embed script
@asdk/js/clientYour servercreateAsdkClient({ apiKey }) for the REST API
@asdk/js/webhooksYour server, edge functionsverifyWebhook() for webhook deliveries

In the browser

TypeScript
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

TypeScript
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:

TypeScript
const { data, error } = await asdk.api.GET("/v1/sites/{siteId}/sources", {
  params: { path: { siteId: "site_…" } },
});

Webhooks

TypeScript
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.