From a6c4b0d0c20c0abf803324908bb43b14c7f9e8e8 Mon Sep 17 00:00:00 2001 From: guangyang1206 Date: Sun, 26 Apr 2026 20:15:53 +0800 Subject: [PATCH] feat(ai): support custom OpenRouter base URL via OPENROUTER_BASE_URL env var Closes #3048 When self-hosting Onlook or routing requests through a custom proxy, the OpenRouter endpoint is now configurable via the OPENROUTER_BASE_URL environment variable. Lookup order: 1. OPENROUTER_BASE_URL env var (explicit override) 2. Default OpenRouter endpoint (https://openrouter.ai/api/v1) Updated apps/web/client/.env.example to document the new optional var. --- apps/web/client/.env.example | 3 +++ packages/ai/src/chat/providers.ts | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/apps/web/client/.env.example b/apps/web/client/.env.example index e193b284a2..3d83d47683 100644 --- a/apps/web/client/.env.example +++ b/apps/web/client/.env.example @@ -8,6 +8,9 @@ SUPABASE_SERVICE_ROLE_KEY="" # OpenRouter - Enables AI chat. Other providers are optional below OPENROUTER_API_KEY="" +# Optional: override the OpenRouter API base URL (e.g. for self-hosted proxies or custom endpoints) +# Defaults to https://openrouter.ai/api/v1 when unset +# OPENROUTER_BASE_URL="https://your-proxy.example.com/api/v1" # Codesandbox - Used to host user apps. Other providers may be supported in the future. May be optional in the future. CSB_API_KEY="" diff --git a/packages/ai/src/chat/providers.ts b/packages/ai/src/chat/providers.ts index 3dd4eb6582..50bbc76f17 100644 --- a/packages/ai/src/chat/providers.ts +++ b/packages/ai/src/chat/providers.ts @@ -45,10 +45,28 @@ export function initModel({ }; } +/** + * Returns the base URL for the OpenRouter provider. + * + * The lookup order is: + * 1. `OPENROUTER_BASE_URL` environment variable (explicit override) + * 2. The default OpenRouter API endpoint + * + * This allows self-hosted deployments and proxy setups to point Onlook at a + * custom endpoint without modifying source code. + */ +function getOpenRouterBaseUrl(): string | undefined { + return process.env.OPENROUTER_BASE_URL || undefined; +} + function getOpenRouterProvider(model: OPENROUTER_MODELS): LanguageModel { if (!process.env.OPENROUTER_API_KEY) { throw new Error('OPENROUTER_API_KEY must be set'); } - const openrouter = createOpenRouter({ apiKey: process.env.OPENROUTER_API_KEY }); + const baseURL = getOpenRouterBaseUrl(); + const openrouter = createOpenRouter({ + apiKey: process.env.OPENROUTER_API_KEY, + ...(baseURL && { baseURL }), + }); return openrouter(model); }