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

<!-- 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
pip install python-dotenv openai
```

## 3. Connect

Django reads no env file by itself: `settings.py` loads it with `python-dotenv`, below `BASE_DIR`.

**`yourproject/settings.py`**

```python
import os

from dotenv import load_dotenv

# ExtraOrbital writes .env.local when the project has one, else .env.
# load_dotenv never overrides a variable already set, so the first file wins.
load_dotenv(BASE_DIR / ".env.local")
load_dotenv(BASE_DIR / ".env")

OPENROUTER_API_KEY = os.environ["OPENROUTER_API_KEY"]
OPENROUTER_BASE_URL = os.environ["OPENROUTER_BASE_URL"]
```

**`yourapp/ai.py`**

```python
from openai import OpenAI

from django.conf import settings

ai = OpenAI(api_key=settings.OPENROUTER_API_KEY, base_url=settings.OPENROUTER_BASE_URL)
# Model ids carry the author prefix: openai/…, anthropic/…, google/…
AI_MODEL = "~openai/gpt-mini-latest"
```

## 4. Use it

**`yourapp/views.py`**

```python
from django.http import JsonResponse

from yourapp.ai import AI_MODEL, ai

# yourapp/urls.py: path("api/ai-gateway/", views.check_ai_gateway)


def check_ai_gateway(request):
    completion = ai.chat.completions.create(
        model=AI_MODEL, messages=[{"role": "user", "content": "Say hello in five words."}]
    )
    return JsonResponse({"text": completion.choices[0].message.content})
```

## 5. Verify

```bash
npx extraorbital list        # the resource is listed as active
python manage.py runserver
curl http://localhost:8000/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.
- `yourapp` and `yourproject` stand for the project's own app and settings package.
- Read credentials once in `settings.py` and import `django.conf.settings` elsewhere; `os.environ[...]` fails at startup when one is missing, which is the point.
- 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
