Switching from OpenAI
Migrate OpenAI-compatible SDK code to NexGate by changing the base URL and API key.
The migration
Switching to NexGate usually requires two configuration changes:
- Set the base URL to
https://api.nexgate.app/v1. - Use a NexGate API key that starts with
ng-.
Note
Request and response shapes remain OpenAI-compatible for chat completions. NexGate does not currently expose embeddings or image generation.
Before and after
# Before: OpenAI
from openai import OpenAI
client = OpenAI(api_key="sk-proj-abc123")
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
)# After: NexGate
from openai import OpenAI
client = OpenAI(
api_key="ng-your-key",
base_url="https://api.nexgate.app/v1",
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}],
)// Before: OpenAI
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-proj-abc123",
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello" }],
});// After: NexGate
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "ng-your-key",
baseURL: "https://api.nexgate.app/v1",
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello" }],
});Environment variables
OPENAI_API_KEY=sk-proj-abc123OPENAI_API_KEY=ng-your-key
OPENAI_BASE_URL=https://api.nexgate.app/v1Then create the SDK client from environment variables if your SDK supports it.
from openai import OpenAI
client = OpenAI()Warning
Keep API keys server-side. Do not ship ng- keys in public frontend bundles.
Compatible features
Streaming
Use stream=True or stream: true for Server-Sent Events.
Function calling
Send tools and tool_choice with OpenAI-compatible tool schemas.
JSON output
Use response_format with json_object or json_schema.
Sampling controls
Use temperature, top_p, penalties, stop, and seed.
Parameter differences to know
| Area | OpenAI-compatible behavior in NexGate |
|---|---|
| Base URL | Must be https://api.nexgate.app/v1 |
| API key | Must be a NexGate key in Authorization: Bearer ng-... |
| Request body | Capped at 64KB |
| Messages | 1 to 100 messages |
| Output limit | max_tokens or max_completion_tokens, depending on model family |
| Timeout | Provider calls time out after 120 seconds |
| Billing | Credits are reserved before the provider call and finalized after actual usage is known |
Tip
For GPT-5 and o-series style models, NexGate forwards the output ceiling using max_completion_tokens; older chat and compatible third-party models use max_tokens.
LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-5.5",
openai_api_key="ng-your-key",
openai_api_base="https://api.nexgate.app/v1",
)
response = llm.invoke("What is NexGate?")import { ChatOpenAI } from "@langchain/openai";
const llm = new ChatOpenAI({
modelName: "gpt-5.5",
openAIApiKey: "ng-your-key",
configuration: {
baseURL: "https://api.nexgate.app/v1",
},
});
const response = await llm.invoke("What is NexGate?");Common migration errors
| Error | Fix |
|---|---|
401 authentication_error | Use Authorization: Bearer ng-your-key and confirm the key was generated in the dashboard |
400 invalid_request_error | Check JSON shape, message limits, model ID, and request body size |
402 insufficient_credits | Add credits before retrying |
429 rate_limit_error | Increase the hourly spend safety limit or wait for the window to clear |
502 provider_error | Retry later or switch to another enabled model |
Why switch?
More model choices
Use multiple model families through one API.
Prepaid billing
Keep spend bounded with credits and safety limits.
Usage exports
Download request-level usage history as CSV.
No custom SDK
Keep OpenAI-compatible client libraries.