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

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

Next.js loads both `.env.local` and `.env`, so either file ExtraOrbital writes is read on the next `next dev`.

**`lib/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

**`app/api/ai-gateway/route.ts`**

```ts
import { ai, AI_MODEL } from "@/lib/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 Response.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: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.
- Import the module only from server code (route handlers, server components, server actions). Only `NEXT_PUBLIC_*` variables reach the browser, and nothing else should.
- With a `src/` directory the module goes in `src/lib/`; without the `@/*` path alias, import it relatively.
- Restart `next dev` after the env file changes: it is read at startup.
- 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
