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?
- API Relay/Proxy — Route requests through a service like claude4u.com for reliability, load balancing, and unified billing
- Regional Access — Access OpenAI from regions where direct connections are unreliable or blocked
- Self-hosted Gateway — Use your own API gateway for logging, rate limiting, or compliance
- Compatible APIs — Connect to any service that implements the OpenAI API interface (Azure OpenAI, vLLM, Ollama, etc.)
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
.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:
- LangChain — Reads
OPENAI_BASE_URLautomatically when usingChatOpenAI - LlamaIndex — Set
api_baseparameter in the OpenAI LLM constructor - AutoGen — Configure
base_urlin the model config list - Cursor IDE — Set the base URL in Settings → Models → OpenAI
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)
Troubleshooting
- Connection refused — Verify the URL is correct and includes the
/v1path - SSL certificate error — Ensure the relay service uses valid HTTPS
- 404 errors — Check that the path suffix is correct (some services omit
/v1) - Environment variable not read — Restart your terminal or application after setting it
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI