Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/supabase-chat/.env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
OPENROUTER_API_KEY=sk-or-v1-...
# Optional: defaults to openai/gpt-5.5
# OPENROUTER_MODEL=openai/gpt-5.5

# OrcaRouter — Anthropic-compatible AI gateway (https://www.orcarouter.ai)
# Setting ORCAROUTER_API_KEY routes the example through OrcaRouter instead of
# OpenRouter (base URL https://api.orcarouter.ai/v1 is applied automatically):
# ORCAROUTER_API_KEY=sk-orca-...
# ORCAROUTER_MODEL=anthropic/claude-haiku-4.5
4 changes: 4 additions & 0 deletions examples/supabase-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ cp .env.local.example .env.local
| `NEXT_PUBLIC_SUPABASE_ANON_KEY` | Supabase dashboard → Settings → API → anon/public key |
| `OPENROUTER_API_KEY` | [openrouter.ai/keys](https://openrouter.ai/keys) |
| `OPENROUTER_MODEL` _(optional)_ | Defaults to `openai/gpt-5.5` |
| `ORCAROUTER_API_KEY` _(optional)_ | Setting this routes the example through [OrcaRouter](https://www.orcarouter.ai) instead of OpenRouter |
| `ORCAROUTER_MODEL` _(optional)_ | Defaults to `anthropic/claude-haiku-4.5` |

> Using [OrcaRouter](https://www.orcarouter.ai)? Set `ORCAROUTER_API_KEY` (and optionally `ORCAROUTER_MODEL`) and the example streams Claude models through the same OpenAI-compatible client — no code changes needed.

### 5. Install and run

Expand Down
18 changes: 15 additions & 3 deletions examples/supabase-chat/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import { NextRequest } from "next/server";
import OpenAI from "openai";
import type { ChatCompletionMessageParam } from "openai/resources/chat/completions.mjs";

const MODEL = process.env.OPENROUTER_MODEL ?? "openai/gpt-5.5";
// Gateway selection. OpenRouter (https://openrouter.ai/api/v1) is the default;
// setting ORCAROUTER_API_KEY routes the example through OrcaRouter
// (https://api.orcarouter.ai/v1) — an Anthropic-compatible AI gateway — using
// the same OpenAI-compatible client. See .env.local.example for both options.
const ORCAROUTER_ENABLED = Boolean(process.env.ORCAROUTER_API_KEY);

const MODEL = ORCAROUTER_ENABLED
? process.env.ORCAROUTER_MODEL ?? "anthropic/claude-haiku-4.5"
: process.env.OPENROUTER_MODEL ?? "openai/gpt-5.5";

/**
* POST /api/chat
Expand All @@ -24,8 +32,12 @@ export async function POST(req: NextRequest) {
const supabase = await createSupabaseServer();

const client = new OpenAI({
apiKey: process.env.OPENROUTER_API_KEY,
baseURL: "https://openrouter.ai/api/v1",
apiKey: ORCAROUTER_ENABLED
? process.env.ORCAROUTER_API_KEY
: process.env.OPENROUTER_API_KEY,
baseURL: ORCAROUTER_ENABLED
? "https://api.orcarouter.ai/v1"
: "https://openrouter.ai/api/v1",
});

const stream = await client.chat.completions.create({
Expand Down