Svelte Quickstart
Five minutes from clone to working chat in Svelte. Install, seed, wire the provider, drop in the library and chat components. Done.
One paste and your AI reads our docs and sets cStar up with you.
1. Install
bash
npm install @cstar.help/svelte 2. Seed sample data
Empty workspaces aren't fun to test against. Populate yours with realistic content first:
bash
cstar seed Creates 5 categories, 12 articles, 3 customers, and 5 tickets. Full breakdown over at the quickstart.
3. Wire up the providers
+layout.svelte
<!-- src/routes/+layout.svelte -->
<script>
import { CStarChatProvider, CStarLibraryProvider } from '@cstar.help/svelte';
let { children } = $props();
</script>
<!-- Two providers: Chat (authenticated) and Library (public) -->
<!-- Seeded with a test key (cstar seed)? Add publishableKey="pk_test_..." to read test articles. -->
<CStarLibraryProvider teamSlug="acme">
<CStarChatProvider teamSlug="acme">
{@render children()}
</CStarChatProvider>
</CStarLibraryProvider> 4. Browse the Knowledge Base
List categories and articles from your public help center.
+page.svelte
<!-- src/routes/help/+page.svelte -->
<script>
import { getLibraryClient, CategoriesState, ArticlesState } from '@cstar.help/svelte';
const client = getLibraryClient();
const cats = new CategoriesState(client);
const articles = new ArticlesState(client, { limit: 5 });
</script>
<h2>Help Center</h2>
{#if cats.isLoading}
<p>Loading...</p>
{:else}
{#each cats.categories as cat (cat.id)}
<div>
<h3>{cat.name} ({cat.article_count})</h3>
</div>
{/each}
{/if}
<h2>Latest Articles</h2>
{#each articles.articles as a (a.id)}
<a href="/help/{a.slug}">{a.title}</a>
{/each} 5. Add Customer Chat
Let customers open tickets and send messages in real-time.
ChatWidget.svelte
<!-- src/lib/components/ChatWidget.svelte -->
<script>
import { onDestroy } from 'svelte';
import {
getChatClient, ChatState, TicketsState, MessagesState
} from '@cstar.help/svelte';
const client = getChatClient();
const chat = new ChatState(client);
const tix = new TicketsState(client);
/** @type {MessagesState | null} */
let activeThread = $state(null);
let draft = $state('');
async function startChat() {
// Get HMAC signature from your server
const res = await fetch('/api/cstar-auth');
const { signature, customer } = await res.json();
await chat.identify(customer, signature);
const ticket = await tix.create({ subject: 'Help needed' });
activeThread = new MessagesState(client, ticket.id);
}
async function handleSend() {
await activeThread?.send(draft);
draft = '';
}
onDestroy(() => activeThread?.destroy());
</script>
{#if !chat.isIdentified}
<button onclick={startChat}>Chat with us</button>
{:else if activeThread}
{#each activeThread.messages as m (m.id)}
<p><b>{m.sender_name || 'Support'}</b>: {m.content}</p>
{/each}
<input bind:value={draft} />
<button onclick={handleSend}>Send</button>
{/if} startChat() asks your server who the customer is. This route signs it with your
identity secret from cStar's identity-verification settings; see the customer chat guide for the details.
src/routes/api/cstar-auth/+server.js
// src/routes/api/cstar-auth/+server.js: signs who the signed-in customer is.
import { createHmac } from 'node:crypto';
import { json, error } from '@sveltejs/kit';
import { CSTAR_IDENTITY_SECRET } from '$env/static/private';
export function GET({ locals }) {
const user = locals.user; // set by your own auth hook
if (!user) error(401, 'Unauthorized');
const customer = {
email: user.email,
externalId: String(user.id),
name: user.name ?? undefined,
timestamp: Math.floor(Date.now() / 1000)
};
// Sign the defined fields, keys sorted A to Z, as compact JSON.
const signed = Object.fromEntries(
Object.entries(customer)
.filter(([, value]) => value !== undefined)
.sort(([a], [b]) => a.localeCompare(b))
);
const signature = createHmac('sha256', CSTAR_IDENTITY_SECRET)
.update(JSON.stringify(signed))
.digest('hex');
return json({ customer, signature }, { headers: { 'Cache-Control': 'no-store' } });
} 6. Article Search
Full-text article search with built-in debounce.
+page.svelte
<!-- src/routes/help/search/+page.svelte -->
<script>
import { onDestroy } from 'svelte';
import { getLibraryClient, ArticleSearchState } from '@cstar.help/svelte';
const client = getLibraryClient();
const searcher = new ArticleSearchState(client);
let query = $state('');
$effect(() => {
searcher.search(query); // 300ms debounce built in
});
onDestroy(() => searcher.destroy());
</script>
<input bind:value={query} placeholder="Search help articles..." />
{#if searcher.isLoading}
<p>Searching...</p>
{:else}
<p>{searcher.totalCount} results</p>
{/if}
{#each searcher.results as a (a.id)}
<a href="/help/{a.slug}">{a.title}</a>
{/each} What's Next?
- Full API Reference: All endpoints and parameters
- SDK Documentation: Detailed Svelte SDK reference
- Webhooks Guide: Event types, signing, and verification
- CLI Reference: Local development tools