---
name: ai-gateway-nuxt
description: "Provisions one OpenAI-compatible API key that reaches every major model (OpenAI, Anthropic, Google, Meta, Mistral, DeepSeek…) for a Nuxt app with `npx extraorbital add ai` (no signup, no card) and wires it into Nuxt with `openai`. Use when a Nuxt 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 Nuxt

<!-- 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

`nuxi dev` loads `.env` only. If ExtraOrbital wrote `.env.local`, start with `nuxi dev --dotenv .env.local`, or provision with `--env-file .env`.

**`server/utils/ai.ts`**

```ts
import OpenAI from "openai";

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

## 4. Use it

**`server/api/ai-gateway.get.ts`**

```ts
// `ai`, `AI_MODEL` are auto-imported from server/utils/ai.ts.
export default defineEventHandler(async () => {
  const completion = await ai.chat.completions.create({
    model: AI_MODEL,
    messages: [{ role: "user", content: "Say hello in five words." }],
  });
  return { text: completion.choices[0]?.message.content };
});
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
npm run dev
curl http://localhost:3000/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.
- Keep the module in `server/utils/`: Nitro auto-imports its exports into server routes and never ships them to the browser. Export names are global across `server/utils`, which is why they are specific.
- A production build does not read `.env`: set the variables on the host (`npx extraorbital push` for Vercel).
- Only `runtimeConfig.public` reaches the browser; never put a credential there.
- 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
