React / Next.js Quickstart

Five minutes from clone to working chat in React / Next.js. 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/react

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

providers.tsx
'use client';
// app/providers.tsx: a Client Component, so the Next.js App Router can use
// the providers. Two providers: Chat (authenticated) and Library (public).
import type { ReactNode } from 'react';
import { CStarChatProvider, CStarLibraryProvider } from '@cstar.help/react';

export default function Providers({ children }: { children: ReactNode }) {
  // Seeded with a test key (cstar seed)? Add publishableKey="pk_test_..." to read test articles.
  return (
    <CStarLibraryProvider teamSlug="acme">
      <CStarChatProvider teamSlug="acme">
        {children}
      </CStarChatProvider>
    </CStarLibraryProvider>
  );
}

4. Browse the Knowledge Base

List categories and articles from your public help center.

HelpCenter.tsx
// components/HelpCenter.tsx
import { useCategories, useArticles } from '@cstar.help/react';

export function HelpCenter() {
  const { categories, isLoading } = useCategories();
  const { articles } = useArticles({ limit: 5 });

  if (isLoading) return <p>Loading...</p>;

  return (
    <div>
      <h2>Help Center</h2>
      {categories.map(cat => (
        <div key={cat.id}>
          <h3>{cat.name} ({cat.article_count})</h3>
        </div>
      ))}
      <h2>Latest Articles</h2>
      {articles.map(a => (
        <a key={a.id} href={`/help/${a.slug}`}>{a.title}</a>
      ))}
    </div>
  );
}

5. Add Customer Chat

Let customers open tickets and send messages in real-time.

ChatWidget.tsx
'use client';
// components/ChatWidget.tsx
import { useChat, useTickets, useMessages } from '@cstar.help/react';
import { useState } from 'react';

export function ChatWidget() {
  const { identify, isIdentified } = useChat();
  const { tickets, create } = useTickets();
  const [activeId, setActiveId] = useState<string | null>(null);
  const { messages, send } = useMessages(activeId);
  const [draft, setDraft] = useState('');

  async function startChat() {
    // Get HMAC signature from your server
    const res = await fetch('/api/cstar-auth');
    const { signature, customer } = await res.json();
    await identify(customer, signature);

    const ticket = await create({ subject: 'Help needed' });
    setActiveId(ticket.id);
  }

  if (!isIdentified) {
    return <button onClick={startChat}>Chat with us</button>;
  }

  return (
    <div>
      {messages.map(m => (
        <p key={m.id}><b>{m.sender_name || 'Support'}</b>: {m.content}</p>
      ))}
      <input value={draft} onChange={e => setDraft(e.target.value)} />
      <button onClick={() => { send(draft); setDraft(''); }}>Send</button>
    </div>
  );
}

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.

app/api/cstar-auth/route.ts
// app/api/cstar-auth/route.ts: signs who the signed-in customer is.
import { createHmac } from 'node:crypto';
import { getCurrentUser } from '@/lib/auth'; // your own session helper

export async function GET() {
  const user = await getCurrentUser();
  if (!user) return new Response('Unauthorized', { status: 401 });

  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', process.env.CSTAR_IDENTITY_SECRET!)
    .update(JSON.stringify(signed))
    .digest('hex');

  return Response.json({ customer, signature }, { headers: { 'Cache-Control': 'no-store' } });
}

Full-text article search with built-in debounce.

SearchBar.tsx
// components/SearchBar.tsx
import { useArticleSearch } from '@cstar.help/react';
import { useState } from 'react';

export function SearchBar() {
  const [query, setQuery] = useState('');
  const { results, totalCount, isLoading } = useArticleSearch(query);

  return (
    <div>
      <input
        value={query}
        onChange={e => setQuery(e.target.value)}
        placeholder="Search help articles..."
      />
      {isLoading ? <p>Searching...</p> : <p>{totalCount} results</p>}
      {results.map(a => (
        <a key={a.id} href={`/help/${a.slug}`}>{a.title}</a>
      ))}
    </div>
  );
}

What's Next?