> ## Documentation Index
> Fetch the complete documentation index at: https://docs.scriptonia.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Openrouter setup

## Navigation

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

| Key    | Model ID (env override)                           | Env for API key                                      |
| :----- | :------------------------------------------------ | :--------------------------------------------------- |
| GROK   | GROK4\_MODEL default x-ai/grok-4-fast             | OPENROUTER\_API\_KEY\_GROK4 or GROK\_API\_KEY        |
| QWEN   | QWEN3\_MODEL default qwen/qwen3-235b-a22b-2507    | OPENROUTER\_API\_KEY\_QWEN3 or QWEN\_API\_KEY        |
| GEMINI | GEMINI2\_5\_MODEL default google/gemini-2.5-flash | OPENROUTER\_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](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
