SvelteKit

01 Prep

Prepare a SvelteKit app for Anvia server endpoints.

Use this path when Anvia runs behind SvelteKit endpoints, form actions, or server-only modules.

1. Create A SvelteKit Project

pnpm create svelte@latest anvia-sveltekit
cd anvia-sveltekit
pnpm install

Choose TypeScript. Anvia code belongs in server-only files, not client components.

2. Install Anvia

pnpm add @anvia/core @anvia/openai zod

Install other providers when needed:

pnpm add @anvia/anthropic @anvia/gemini @anvia/mistral

3. Add Environment Variables

OPENAI_API_KEY=sk_...

Read secrets from $env/static/private inside server modules:

import { OPENAI_API_KEY } from "$env/static/private";

if (!OPENAI_API_KEY) {
  throw new Error("OPENAI_API_KEY is required");
}

4. Choose File Boundaries

FilePurpose
src/lib/server/ai/support-agent.tsProvider client, model, tools, and reusable agent
src/routes/api/support/+server.tsJSON endpoint
src/routes/api/support/stream/+server.tsNDJSON stream endpoint
src/hooks.server.tsAuth and request-local locals

Next

Build the reusable agent in Setup Anvia. Read Runtime Boundaries before importing Anvia into Svelte components.