Getting started

Use Optimal API without already knowing APIs.

Follow the steps in order. Each picture is an illustration of the portal, drawn for this guide, so you can recognise the screen. The words on the real portal are in English.

Step 1

What an API key is

An API key is a password for a program. When a chat app or a command sends a request, it includes the key so Optimal API can see which account to bill.

Anyone who has the key can spend your balance. Treat it like a bank card number. The portal shows the full key once, when you create it. After that it shows only the beginning, so you can tell keys apart.

If you lose the key, create another one and revoke the old one. Revoking stops the old key immediately.

Illustration of the portal. The live app may show more fields.

Step 2

Create an account

Open the portal and choose Create account. Enter your name, email, and a password of at least 8 characters. Sign-up starts you at a zero balance. You cannot call a paid model until you add funds.

You will land on a dashboard that shows balance, held funds, and available funds. Held funds are reserved for calls that are still running. Available is what a new call can use.

Illustration of the portal. The live app may show more fields.

Step 3

Add balance

Open Top up. Enter an amount from 1.00 to 10000.00 with at most two decimal places, then choose a method. Every account can use Alipay, WeChat Pay, USDT, or USDC.

Alipay and WeChat Pay open the usual cashier when the operator has connected a live merchant account. USDT and USDC open a crypto invoice. The balance changes only after the payment is verified. Refresh the dashboard and the new available amount should be there.

If merchant keys are not connected yet, the portal uses a sandbox confirmation page instead of a live cashier. That page is labelled sandbox. Confirming it is what adds the test balance.

Illustration of the portal. The live app may show more fields.

Step 4

Create an API key

Open API keys. Give the key a name you will remember, such as “laptop” or the name of a chat app. Create it, then copy the full key before you leave the page.

Paste it into a password manager. The portal cannot show it again. You can create more than one key, and you can revoke one without affecting the others.

Illustration of the portal. The live app may show more fields.

Step 5

Send the first request

A request is a message plus a model name, sent to https://api.optimal-api.com/v1/chat/completions. The samples below use auto, which lets Optimal API choose the model. You can replace auto with a name from the models page, such as gpt-4o-mini.

If you use a terminal, copy one sample, set OPTIMAL_API_KEY to the key you just created, and run it. A normal answer is a short reply. An error is a JSON message with a code. The common ones are explained at the end of this guide.

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"}]}'
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"])
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);
Illustration of the portal. The live app may show more fields.

Step 6

Or use a chat app, with no code

Many desktop and web chat apps can talk to an OpenAI-compatible endpoint. Optimal API is not affiliated with those apps. The setting names differ, and the pattern is the same.

Apps that document this kind of setting include Chatbox, LobeChat, Open WebUI, NextChat, LibreChat, and AnythingLLM. Cursor and Continue can also point at a custom OpenAI base URL.

  1. Open the app’s model or provider settings.
  2. Choose OpenAI, or a custom OpenAI-compatible provider.
  3. Set the base URL to https://api.optimal-api.com/v1.
  4. Paste the API key from the portal.
  5. Set the model to auto or to a name from the models page.
  6. Send a short message such as “Hello”. A reply means the key, the balance, and the URL are working.
  7. If the app says it cannot find the address, it may be adding /v1 itself. Try the base URL without /v1. If it asks for a full chat URL, use https://api.optimal-api.com/v1/chat/completions.
Illustration of the portal. The live app may show more fields.

Step 7

Choose a model, or let auto choose

A fixed name such as gpt-4o-mini always asks that model, then its fallbacks if that provider fails. An auto name lets the gateway rank a short list and pick one that fits the task.

Use auto:cheap when you want a lower list price and the task is ordinary. Use auto or auto:balanced for everyday work. Use auto:best when you want the higher quality score and accept a higher hold. The response tells you which model actually answered.

Illustration of the portal. The live app may show more fields.

Step 8

Read usage, cost, and savings

Dashboard shows the balance, the amount on hold, and what is available. Usage groups recent calls by day and by key, with token counts and the amount charged.

Savings and performance shows charges the gateway did not take, for the controls your organisation has turned on. A comparison with another model is labelled as an estimate. If a figure says it is not measured yet, there is not enough traffic to publish a number. That empty state is intentional.

Models and pricing includes the ROI calculator. It multiplies your monthly token volume by the platform’s measured averages. It stays blank until those averages exist.

Illustration of the portal. The live app may show more fields.

Common errors and what to do

401 invalid_api_key

The key was rejected

The header is missing, the key was copied with a space or a missing character, or the key was revoked. Create a fresh key, copy the whole value, and paste it again. The word Bearer and a single space come before the key.

402 insufficient_balance

The balance is too low

The account cannot reserve the hold for this call. Open Top up and add funds. Auto routing reserves the most expensive model in its short list, so a cheap-looking request can still need a larger balance. The portal shows the available amount.

429 rate_limit_exceeded

Too many requests right now

The key has a request limit and a token limit. Wait a moment and send the message again. Sending faster will keep returning 429. An operator can assign a higher organisation ceiling. This limit is yours. It is separate from a provider being busy.

404 model_not_found

The model name is not in the catalog

Check the spelling against the models page. auto, auto:cheap, auto:balanced, and auto:best are valid names. A model an operator has disabled is also rejected.

Questions

Do I have to write code?

No. A chat app that accepts an OpenAI-compatible base URL is enough. The code samples are there when you want them.

Is this the OpenAI API?

It speaks the same chat request shape. The host is api.optimal-api.com. A key from OpenAI does not work here, and an Optimal API key does not work on OpenAI.

Which models can I name?

The models page lists the seeded catalog. The live portal list is the one your key can call, because an operator can enable or disable models.

What does a call cost?

Input tokens and output tokens are priced separately per million, then rounded up. The worksheet on the pricing page adds a list price from the seeded catalog. Measured savings are a different number and stay blank until they exist.

Can I keep using one model?

Yes. Send that model’s name. Fallbacks are used when that provider returns a retryable error. auto is optional.

What if a provider is down?

Retryable failures try the next model in the chain. If none answer, you are not charged for tokens, and the call returns an error you can retry.

Which payments are accepted?

Alipay, WeChat Pay, USDT, and USDC, on every account. VNPay and MoMo are not offered in the portal.

Can the portal show my key again?

No. Copy it when it is created. If you did not, create a new key and revoke the one you lost.

Are prompts stored?

A new organisation stores prompts by default, for 30 days. An owner can turn off “Store prompts and step outputs” on the Organisation page. With that off, finished runs do not keep the text.

Where do I see if the API is up?

Use the Status link in the header. It opens the public status page.