Yak Docs
SDKs

Svelte SDK

The @yak-io/svelte package provides a Svelte-compatible provider for integrating Yak. It exposes Svelte readable stores for reactive state and explicit mount()/destroy() methods for lifecycle control.

Installation

npm install @yak-io/svelte @yak-io/javascript
pnpm add @yak-io/svelte @yak-io/javascript
yarn add @yak-io/svelte @yak-io/javascript
bun add @yak-io/svelte @yak-io/javascript

Quick Start

Set up the provider in your root component

Call createYakProvider and wire up mount()/destroy() to Svelte's lifecycle:

<!-- App.svelte -->
<script lang="ts">
import { onMount, onDestroy } from "svelte";
import { createYakProvider } from "@yak-io/svelte";

const yak = createYakProvider({
  appId: "your-app-id",
  getConfig: async () => {
    const res = await fetch("/api/yak");
    return res.json();
  },
  onToolCall: async (name, args) => {
    const res = await fetch("/api/yak", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ name, args }),
    });
    const data = await res.json();
    if (!data.ok) throw new Error(data.error);
    return data.result;
  },
});

onMount(() => yak.mount());
onDestroy(() => yak.destroy());
</script>

<slot />

Set up server handlers

Use @yak-io/javascript to create the API endpoints on your backend. See the JavaScript SDK for runtime-specific examples (Express, Hono, Cloudflare Workers, etc.).

// api/yak.ts
import { createYakHandler } from "@yak-io/javascript/server";

export const { GET, POST } = createYakHandler({
  routes: [
    { path: "/", title: "Home" },
    { path: "/products", title: "Products" },
  ],
});

API

createYakProvider

Creates a Yak widget instance with Svelte-compatible stores. Unlike the Vue and React SDKs, the Svelte SDK does not use framework-level context — you manage the lifecycle explicitly with mount() and destroy().

OptionTypeRequiredDescription
appIdstringYesYour Yak application ID
mode"chat" | "voice" | "both"NoWhich surfaces the trigger exposes. Defaults to "chat". See Voice Mode.
getConfig() => Promise<ChatConfig> | ChatConfigNoConfig provider for routes and tools (used by chat and voice)
onToolCall(name, args) => Promise<unknown>NoHandler for tool execution (used by chat and voice)
themeThemeNoWidget styling options
onRedirect(path: string) => voidNoCustom navigation handler
disableRestartButtonbooleanNoHide the restart button in the header
disablePageContentbooleanNoStop sending any page context (URL, title, and visible text) to the assistant. The widget still works, but it won't be aware of the page the user is on.
triggerboolean | TriggerButtonConfigNoConfigure the floating trigger button
user{ id, hash }NoSigned end-user identity. Enables conversation persistence and history. Call setUser() to change it after setup (login/logout).

Privacy: By default Yak shares the current page's URL, title, and visible text with the assistant so it can answer questions about the page the user is viewing. Set disablePageContent to turn this off entirely — the SDK then sends nothing about the page (not even the URL), so the assistant can't answer page-specific questions.

Return value (YakApi)

PropertyTypeDescription
isOpenReadable<boolean>Whether the chat panel is currently open
isReadyReadable<boolean>Whether the widget iframe is ready
chatLoadingReadable<boolean>isOpen && !isReady — opening but not yet interactive
open() => voidOpen the chat panel
close() => voidClose the chat panel
openWithPrompt(prompt: string) => voidOpen and send a specific prompt
subscribeToToolEvents(handler) => () => voidSubscribe to tool call events (returns unsubscribe)
voiceMachineReadable<VoiceMachine>Current voice state — see Voice Mode
voiceLoadingReadable<boolean>true while the voice session is connecting
voiceStart() => Promise<void>Start a voice session
voiceStop() => Promise<void>Stop the current voice session
voiceToggle() => Promise<void>Start if idle/error, stop if active
setUser(user?: { id, hash }) => voidSet or clear the signed end-user identity after setup — call on login/logout
mount() => voidMount the widget DOM — call in onMount
destroy() => voidDestroy the widget DOM — call in onDestroy

Reactive State

isOpen and isReady are Svelte readable stores. Access their values with the $ prefix in templates or subscribe manually:

<script lang="ts">
const { isOpen, isReady, open, close } = yak;
</script>

<button on:click={() => open()}>Open Chat</button>
{#if $isOpen}
  <button on:click={() => close()}>Close</button>
{/if}
<p>Ready: {$isReady}</p>

Tool Events

Subscribe to tool call completion events for UI synchronization:

<script lang="ts">
import { onDestroy } from "svelte";

const unsubscribe = yak.subscribeToToolEvents((event) => {
  if (event.ok && event.name.startsWith("order.")) {
    refreshOrders();
  }
});

onDestroy(unsubscribe);
</script>

Router Integration

Pass SvelteKit's navigation function to onRedirect for client-side navigation:

<script lang="ts">
import { goto } from "$app/navigation";
import { createYakProvider } from "@yak-io/svelte";

const yak = createYakProvider({
  appId: "your-app-id",
  onRedirect: (path) => goto(path),
  // ...other options
});
</script>

On this page