Quickstart

The base URL is https://api.optimal-api.com/v1.

Send the OpenAI chat shape. Put the API key in Authorization: Bearer. Create the key in the portal first, and add balance before the first paid call.

curl
curl https://api.optimal-api.com/v1/chat/completions \
  -H "Authorization: Bearer $OPTIMAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"auto","messages":[{"role":"user","content":"Hello"}]}'
curl, fixed model
curl https://api.optimal-api.com/v1/chat/completions \
  -H "Authorization: Bearer $OPTIMAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Hello"}]}'
curl, list models
curl https://api.optimal-api.com/v1/models \
  -H "Authorization: Bearer $OPTIMAL_API_KEY"
Python
import json
import os
import urllib.request

request = urllib.request.Request(
    "https://api.optimal-api.com/v1/chat/completions",
    data=json.dumps({
        "model": "auto",
        "messages": [{"role": "user", "content": "Hello"}],
    }).encode(),
    headers={
        "Authorization": "Bearer " + os.environ["OPTIMAL_API_KEY"],
        "Content-Type": "application/json",
    },
)
with urllib.request.urlopen(request) as response:
    body = json.load(response)
print(body["choices"][0]["message"]["content"])
Python, OpenAI SDK
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.optimal-api.com/v1",
)
result = client.chat.completions.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello"}],
)
print(result.choices[0].message.content)
Node.js
const response = await fetch("https://api.optimal-api.com/v1/chat/completions", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.OPTIMAL_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    model: "auto",
    messages: [{ role: "user", content: "Hello" }],
  }),
});
const body = await response.json();
if (!response.ok) {
  console.error(body);
  process.exit(1);
}
console.log(body.choices[0].message.content);
Node.js, OpenAI SDK
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.OPTIMAL_API_KEY,
  baseURL: "https://api.optimal-api.com/v1",
});
const result = await client.chat.completions.create({
  model: "auto",
  messages: [{ role: "user", content: "Hello" }],
});
console.log(result.choices[0].message.content);
  • Python’s standard library sample needs no extra package. The SDK sample expects the openai package and a real key in place of YOUR_API_KEY.
  • The Node sample uses the fetch built into current Node. The SDK sample expects the openai package.
  • model can be auto, auto:cheap, auto:balanced, auto:best, or a name from the models page.
  • A non-stream auto response includes x-routed-model. x-estimated-savings-micros compares the served price with an estimated baseline.

Prefer a click-by-click guide? Open the getting-started tutorial

API status