DEV Community

alice kelly
alice kelly

Posted on

OpenAI API Relay Setup: Environment Variables That Keep Your Project Clean

An OpenAI API relay is easiest to manage when your project treats it as configuration, not hardcoded code. The clean pattern is simple: keep the base URL, key, and model name in environment variables, then read them from your app.

This makes it easier to switch between direct API access, relay testing, and different models without touching source files.

The three variables I usually keep

AI_API_BASE_URL=https://api.wappkit.com/v1
AI_API_KEY=your_relay_key
AI_MODEL=gpt-5.5
Enter fullscreen mode Exit fullscreen mode

AI_API_BASE_URL points your SDK to the OpenAI-compatible endpoint.

AI_API_KEY is the key issued by the relay service.

AI_MODEL lets you switch models without editing your app code.

Before choosing a model, check the live model list. Do not rely on old examples copied from another project.

Python example

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["AI_API_KEY"],
    base_url=os.environ.get("AI_API_BASE_URL", "https://api.openai.com/v1"),
)

response = client.chat.completions.create(
    model=os.environ.get("AI_MODEL", "gpt-5.5"),
    messages=[{"role": "user", "content": "Write one sentence about API relays."}],
    max_tokens=80,
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

This keeps the code portable. Your local machine can use the relay. Production can use a different endpoint if needed.

Node.js example

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.AI_API_KEY,
  baseURL: process.env.AI_API_BASE_URL || "https://api.openai.com/v1",
});

const response = await client.chat.completions.create({
  model: process.env.AI_MODEL || "gpt-5.5",
  messages: [{ role: "user", content: "Write one sentence about API relays." }],
  max_tokens: 80,
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

Same idea: the app reads configuration, the environment decides the provider.

Why this helps

First, you avoid leaking keys into source control.

Second, you can test different models like gpt-5.5 or gpt-5.4 by changing one variable.

Third, teammates can use their own keys without editing shared files.

Fourth, rollback is easy. If the relay endpoint has an issue, you can change the base URL and restart.

Add a startup check

Before your app handles real work, validate the required variables:

required = ["AI_API_KEY", "AI_API_BASE_URL", "AI_MODEL"]
missing = [name for name in required if not os.environ.get(name)]

if missing:
    raise RuntimeError(f"Missing environment variables: {', '.join(missing)}")
Enter fullscreen mode Exit fullscreen mode

This catches configuration mistakes early.

Keep a tiny smoke test

Create a separate smoke test that sends one short request:

python smoke_test_ai.py
Enter fullscreen mode Exit fullscreen mode

Run it after changing the key, model, or base URL. If it fails, check the docs, billing page, and status page before rewriting application code.

Practical boundary

An OpenAI API relay is useful for development, prototypes, multi-model testing, and payment friction. It is not a reason to ignore security, cost controls, or production review.

Use environment variables, keep keys out of git, verify model names from the live list, and run a smoke test whenever configuration changes. That small bit of discipline prevents most relay setup bugs.

Top comments (0)