Setting OpenAI Base URL

How to Set the OpenAI Base URL in Python, Node.js, and Environment Variables

The base_url (or baseURL) parameter tells the OpenAI SDK where to send API requests. By default, it points to https://api.openai.com/v1, but you can change it to use a relay service, self-hosted proxy, or any OpenAI-compatible endpoint. This is one of the most important configuration options for teams using API gateways or working in regions with restricted access.

Why Change the Base URL?

Python SDK Configuration

The OpenAI Python SDK accepts base_url as a constructor parameter:

from openai import OpenAI

# Method 1: Direct parameter
client = OpenAI(
    api_key="sk-your-key",
    base_url="https://claude4u.com/v1"
)

# Method 2: Using environment variable (recommended)
import os
os.environ["OPENAI_BASE_URL"] = "https://claude4u.com/v1"

client = OpenAI()  # Automatically reads OPENAI_BASE_URL

# Make a request as usual
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)

Node.js SDK Configuration

In the Node.js SDK, use the baseURL property (camelCase):

import OpenAI from 'openai';

// Method 1: Direct parameter
const client = new OpenAI({
    apiKey: 'sk-your-key',
    baseURL: 'https://claude4u.com/v1'
});

// Method 2: Environment variable
// Set OPENAI_BASE_URL=https://claude4u.com/v1 in your environment
const client2 = new OpenAI(); // Reads OPENAI_BASE_URL automatically

const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }]
});

Environment Variable Configuration

Setting the base URL via environment variables is the cleanest approach because it requires no code changes:

# Linux / macOS
export OPENAI_API_KEY="sk-your-key"
export OPENAI_BASE_URL="https://claude4u.com/v1"

# Windows Command Prompt
set OPENAI_API_KEY=sk-your-key
set OPENAI_BASE_URL=https://claude4u.com/v1

# Windows PowerShell
$env:OPENAI_API_KEY = "sk-your-key"
$env:OPENAI_BASE_URL = "https://claude4u.com/v1"

Using a .env File

For local development, store configuration in a .env file:

# .env
OPENAI_API_KEY=sk-your-key
OPENAI_BASE_URL=https://claude4u.com/v1
# Python - load with python-dotenv
from dotenv import load_dotenv
load_dotenv()

from openai import OpenAI
client = OpenAI()  # Reads from environment
// Node.js - load with dotenv
import 'dotenv/config';
import OpenAI from 'openai';

const client = new OpenAI(); // Reads from environment
Warning: Always add .env to your .gitignore file to prevent accidentally committing API keys to version control.

cURL Configuration

When testing with cURL, simply change the URL in your request:

curl https://claude4u.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-key" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'

Framework-Specific Configuration

Many AI frameworks and tools support the OPENAI_BASE_URL environment variable natively:

Verifying Your Configuration

Run this quick test to confirm your base URL is working:

from openai import OpenAI

client = OpenAI(base_url="https://claude4u.com/v1")

models = client.models.list()
for model in models.data[:5]:
    print(model.id)
Tip: Using claude4u.com as your base URL gives you automatic failover, load balancing across multiple upstream providers, and a single API key that works with OpenAI, Claude, Gemini, and more. Set it once and access all major AI models through the same OpenAI-compatible interface.

Troubleshooting

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free