Skip to main content
OpenRouter Setup

OpenRouter Setup

OpenRouter configuration, model selection, API keys, and usage in Scriptonia.

OpenRouter Configuration

Scriptonia uses OpenRouter as an OpenAI-compatible API to run Idea, Intent, Platform, and Execution (all agents). The client is in lib/openai.ts: it uses fetch to OPENROUTER_BASE_URL (default https://openrouter.ai/api/v1/chat/completions), not the OpenAI SDK.

Base URL

OPENROUTER_BASE_URL=https://openrouter.ai/api/v1/chat/completions
Leave unset to use the default.

Required: at least one model key

You must set at least one of:
  • OPENROUTER_API_KEY_GROK4
  • OPENROUTER_API_KEY_QWEN3
  • OPENROUTER_API_KEY_GEMINI2_5
Legacy names (still supported):
  • GROK_API_KEY
  • QWEN_API_KEY
  • GEMINI_API_KEY
AI_MODELS in lib/openai.ts uses OPENROUTER_API_KEY_* first, then the legacy key for that model.
If the chosen model’s key is missing, the LLM call throws.

Model Selection

Configured models

KeyModel ID (env override)Env for API key
GROKGROK4_MODEL default x-ai/grok-4-fastOPENROUTER_API_KEY_GROK4 or GROK_API_KEY
QWENQWEN3_MODEL default qwen/qwen3-235b-a22b-2507OPENROUTER_API_KEY_QWEN3 or QWEN_API_KEY
GEMINIGEMINI2_5_MODEL default google/gemini-2.5-flashOPENROUTER_API_KEY_GEMINI2_5 or GEMINI_API_KEY

Default model

DEFAULT_MODEL=GROK
# or: QWEN, GEMINI
processIdea, refineIntent, selectPlatform, and executeX402Workflow (and the callOpenRouter / createChatCompletion path) use DEFAULT_MODEL unless a modelType argument overrides it. Execution and platform-templates typically use GROK for generation.

Overriding model IDs

GROK4_MODEL=x-ai/grok-4-fast
QWEN3_MODEL=qwen/qwen3-235b-a22b-2507
GEMINI2_5_MODEL=google/gemini-2.5-flash
Use IDs from OpenRouter models.

API Key Setup

1. OpenRouter account

  • Sign up at OpenRouter
  • Add a key in the dashboard
  • One key can access multiple models if your plan allows

2. Env vars

In .env (or .env.local), set at least the key for the model you use as default:
DEFAULT_MODEL=GROK
OPENROUTER_API_KEY_GROK4=
For multi-model:
OPENROUTER_API_KEY_GROK4=
OPENROUTER_API_KEY_QWEN3=
OPENROUTER_API_KEY_GEMINI2_5=

3. Optional: request headers

The client sends:
  • Authorization: Bearer <model-specific key>
  • HTTP-Referer: NEXT_PUBLIC_SITE_URL or http://localhost:3000
  • X-Title: Scriptonia Platform
Set NEXT_PUBLIC_SITE_URL in production so OpenRouter can attribute usage.

Usage and Costs

Where OpenRouter is used

  • Idea: processIdea → one chat completion (JSON)
  • Intent: refineIntent → one chat completion (JSON)
  • Platform: selectPlatform → one chat completion (JSON)
  • Execute: executeX402Workflow → many callOpenRouter / createChatCompletion calls (platform-templates: per-file and for review/dedup). Model is typically GROK

Billing

  • OpenRouter bills per token by model
  • Check OpenRouter pricing and your dashboard
  • Scriptonia does not set rate limits; to avoid surprise cost, use OpenRouter or app-level limits in production

Reducing cost

  • Use a cheaper model for Idea/Intent/Platform: set DEFAULT_MODEL=GEMINI or QWEN and ensure the corresponding key is set
  • Execution can still call callOpenRouter(…, ‘GROK’, …) explicitly
  • In lib/openai.ts, you can pass a modelType into processIdea, refineIntent, selectPlatform where the function accepts it
  • Execution’s generateFileContentWithAgent uses callOpenRouter(…, ‘GROK’, …); changing that would require a code edit

Verification

  • Set OPENROUTER_API_KEY_GROK4 (or the key for DEFAULT_MODEL) and DATABASE_URL
  • Run npm run dev and submit an Idea on the home page
  • You should get analysis and clarifying questions
  • If that works, Intent, Platform, and Execute are using the same client
  • Any failure will show in server logs as OpenRouter API error or a missing-key error