Generate Text

Code Playground
Svelte AI SDK Text Generation

Learn how to generate text using Vercel AI SDK + OpenRouter in Sveltekit.

Demo

Step 1: Create Prompt UI

Let's start by building a simple Svelte component that allows users to input a prompt and submit it. When the user submits, we'll send the prompt to our SvelteKit API route to get the generated response.

+page.svelte

Step 2: AI Config Setup

Set up a shared AI configuration first so your provider and model settings live in one place. That keeps your SvelteKit project easier to maintain and makes it simple to switch models or providers later.

src/lib/config/ai.ts
Note

Make sure to add your OPENROUTER_API_KEY to your .env file.

Step 3: Create API Route

Create a SvelteKit API route to handle the request and generate text with the AI SDK. This route receives the submitted messages, runs the model, and returns the generated response back to the page.

src/routes/api/cookbook/generate-text/+server.ts