AIDiscount优汇智
Home / Blog

What Is an OpenAI-Compatible API? How to Switch the Base URL

An OpenAI-compatible API accepts the same requests as the OpenAI API. Here is how to point the OpenAI SDK, curl and common tools at a different provider by changing two settings.

2026-11-25 · 2 min read · AIDiscount team

OpenAI-compatibleAPIPythonNode.js

Most AI tools and SDKs were written against the OpenAI API. Many other providers accept the same request format, so the same code can talk to them. These providers are called OpenAI-compatible. Moving between them usually means changing two settings: the base URL and the API key.

What "OpenAI-compatible" means

A provider is OpenAI-compatible when it serves the same paths with the same JSON shapes. The most common ones are:

EndpointUsed for
POST /v1/chat/completionsChat and most tools. The most widely supported format.
POST /v1/responsesOpenAI's newer interface, used by Codex CLI and the Agents SDK.
GET /v1/modelsLists the model IDs your key can use.
POST /v1/images/generationsImage generation.
POST /v1/embeddingsText embeddings.

Compatibility covers the request and response format. Each provider still decides which models it offers, so the model value must be a model ID that the provider lists.

The two settings you change

  • Base URL. The address that comes before /chat/completions. For OpenAI it is https://api.openai.com/v1. For AIDiscount it is https://aidiscount.ai/v1.
  • API key. The provider's own key, sent as Authorization: Bearer sk-.... A key from one provider does not work at another.

Keep the key in an environment variable instead of in code:

# macOS / Linux
export AIDISCOUNT_API_KEY="sk-..."

# Windows PowerShell
$env:AIDISCOUNT_API_KEY="sk-..."

curl

curl https://aidiscount.ai/v1/chat/completions \
  -H "Authorization: Bearer $AIDISCOUNT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4.1-flash",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

Python (official OpenAI SDK)

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["AIDISCOUNT_API_KEY"],
    base_url="https://aidiscount.ai/v1",
)

resp = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Hello"}],
)
print(resp.choices[0].message.content)

The SDK also reads OPENAI_BASE_URL and OPENAI_API_KEY from the environment. If you set both, you can switch providers without touching the code at all.

Node.js (official OpenAI SDK)

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.AIDISCOUNT_API_KEY,
  baseURL: "https://aidiscount.ai/v1",
});

const resp = await client.chat.completions.create({
  model: "deepseek-v4.1-flash",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(resp.choices[0].message.content);

Streaming works the same way

Add stream=True (Python) or stream: true (Node.js). The provider sends server-sent events in the same format as OpenAI.

stream = client.chat.completions.create(
    model="deepseek-v4.1-flash",
    messages=[{"role": "user", "content": "Write a haiku"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Find the model IDs you can use

curl https://aidiscount.ai/v1/models \
  -H "Authorization: Bearer $AIDISCOUNT_API_KEY"

The model marketplace shows the same IDs with their current prices.

Common mistakes

  • 404 Not Found. The base URL is missing /v1, or the tool adds /v1 itself and you added it again. Try the other form.
  • 401 Unauthorized. The key belongs to a different provider, has a typo, or was deleted.
  • Model not found. The model name is one the provider does not offer. Copy the exact ID from /v1/models.

More fixes are in How to fix common AI API errors.

What about the Anthropic format?

Some tools speak the Anthropic Messages API instead (POST /v1/messages with an x-api-key header). AIDiscount accepts that format too. For the Anthropic SDK, use the base URL https://aidiscount.ai without /v1, because the SDK adds /v1/messages itself. The API docs have full examples.

Try it with AIDiscount. One API key works with OpenAI- and Anthropic-compatible tools. Pay as you go, no monthly fee.
Create a free accountRead the API docs

Related guides

Languages

English · 简体中文 · 繁體中文 · 日本語 · 한국어 · Español · Français · Deutsch · Português · Русский