---
name: ai-gateway-sveltekit
description: "Provisions one OpenAI-compatible API key that reaches every major model (OpenAI, Anthropic, Google, Meta, Mistral, DeepSeek…) for a SvelteKit app with `npx extraorbital add ai` (no signup, no card) and wires it into SvelteKit with `openai`. Use when a SvelteKit project needs AI Gateway, an LLM, an OpenAI API key, an Anthropic API key, a Gemini key, OPENAI_API_KEY, ANTHROPIC_API_KEY or OpenRouter, or when OPENROUTER_API_KEY is missing or unset in .env."
---

# AI Gateway in SvelteKit

<!-- Generated from ExtraOrbital's service registry by `pnpm skills:build` in platform/app. Edit the generator, not this file. -->

ExtraOrbital provisions one OpenAI-compatible API key that reaches every major model (OpenAI, Anthropic, Google, Meta, Mistral, DeepSeek…) in one command and writes the credentials into the project's env file. For anything this recipe does not cover, use the `extraorbital` skill or https://extraorbital.dev/llms.txt.

## 1. Provision

```bash
npx extraorbital add ai
```

Run it from the project root. It is idempotent (a second run returns the same resource) and writes to `.env.local` if the project has one, otherwise `.env`, keeping that file out of git. Add `--json` to parse the result; credential values are redacted there, and you should never print them either.

| Variable | |
| --- | --- |
| `OPENROUTER_BASE_URL` | OpenAI-compatible base URL |
| `OPENROUTER_API_KEY` | The key; spends from the team's budget |

Free on the Prototype plan: First $5.00 of small models.

## 2. Install the client

```bash
npm install openai
```

## 3. Connect

SvelteKit reads `.env` and `.env.local` into `$env/dynamic/private` in dev; in production the host's environment fills it.

**`src/lib/server/ai.ts`**

```ts
import OpenAI from "openai";
import { env } from "$env/dynamic/private";

export const ai = new OpenAI({
  apiKey: env.OPENROUTER_API_KEY!,
  baseURL: env.OPENROUTER_BASE_URL!,
});
// Model ids carry the author prefix: openai/…, anthropic/…, google/…
export const AI_MODEL = "~openai/gpt-mini-latest";
```

## 4. Use it

**`src/routes/api/ai-gateway/+server.ts`**

```ts
import { json } from "@sveltejs/kit";
import { ai, AI_MODEL } from "$lib/server/ai";

export async function GET() {
  const completion = await ai.chat.completions.create({
    model: AI_MODEL,
    messages: [{ role: "user", content: "Say hello in five words." }],
  });
  return json({ text: completion.choices[0]?.message.content });
}
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
npm run dev
curl http://localhost:5173/api/ai-gateway
```

A JSON answer means the credentials, the client and the route all work. Delete the route afterwards if the app does not need it.

## Pitfalls

- No vendor's own key can be provisioned. `npx extraorbital add openai` (or an `.env.example` declaring `OPENAI_API_KEY`) provisions this gateway, points `OPENAI_BASE_URL`/`OPENAI_API_KEY` at it so existing code runs unchanged, and exits `8`: not a failure.
- `gpt-5.2` is not an id here; `openai/gpt-5.2` is. On the free Prototype plan a key reaches small models only; the list with prices is at https://extraorbital.dev/api/gateway/models.
- It also speaks the Anthropic Messages API and works with the Vercel AI SDK and LangChain: https://extraorbital.dev/docs/resources/ai-gateway/sdks.md
- Keep the key on the server: never in a `NEXT_PUBLIC_`/`PUBLIC_` variable or a client component.
- Read credentials through `$env/dynamic/private`, not `process.env`: Vite does not put `.env` into `process.env` in dev, and `$lib/server/` keeps the module out of the browser bundle.
- `$env/dynamic/private` is empty while a page is prerendered: do not call the client from a prerendered route.
- Only `PUBLIC_*` variables reach the browser. Pass anything public a page needs from a `+page.server.ts` load function instead.
- Exit code 3 means the account is past its free allowance: show the human the link the command printed and wait. Do not retry or work around it.

Docs: https://extraorbital.dev/docs/resources/ai-gateway.md
