OpenAI API
Access GPT models for chat, text, embeddings, images and audio.
What it does
The OpenAI API exposes GPT models for chat and text generation, structured output and function/tool calling, text embeddings, image generation, speech-to-text (Whisper) and text-to-speech. Everything is JSON over HTTPS; the Chat Completions and newer Responses endpoints cover most use cases.
Base URL
Authentication
Create an account, add a payment method, then generate a secret key (sk-…) in the dashboard. Send it as Authorization: Bearer YOUR_KEY. Keys are per-project; set a monthly spend limit so a runaway loop can’t drain your balance. There is no perpetual free tier — you pay per token used.
Code examples
curl https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"Say hi"}]}'from openai import OpenAI
client = OpenAI() # reads OPENAI_API_KEY from the environment
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hi in one word"}],
)
print(resp.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Say hi in one word" }],
});
console.log(resp.choices[0].message.content);Rate limits & quotas
Rate limits are set per usage tier and rise as your lifetime spend grows — a new paid account starts around 500 requests/min and a few hundred thousand tokens/min on the small models, far higher on higher tiers. Limits are enforced on both requests-per-minute and tokens-per-minute; the response headers (x-ratelimit-remaining-*) tell you where you stand. Over the limit returns HTTP 429 — back off and retry.
Key response fields
| choices[0].message.content | The model’s reply text |
| choices[0].finish_reason | stop, length, tool_calls or content_filter |
| usage.prompt_tokens | Tokens in your input — you are billed for these |
| usage.completion_tokens | Tokens the model generated |
| model | Exact model snapshot that served the request |
Common errors
| 401 | Bad or revoked key, or wrong Authorization scheme (must be Bearer). |
| 429 | Rate limit hit or you are out of credit — the JSON error.type tells you which. |
| 400 context_length_exceeded | Prompt plus max_tokens is larger than the model window; trim the prompt. |
| 500 / 503 | Transient server error or overload — retry with exponential backoff. |
When to use it (and alternatives)
The default choice when you want a single vendor covering chat, embeddings, images and audio with mature SDKs. For long-context reasoning and tool use many teams also try the Anthropic Claude API; for open-weight models on your own terms see Hugging Face or Replicate.
FAQ
Is the OpenAI API free?
No. New accounts sometimes get a small time-limited credit, but normal use is pay-as-you-go per token. The small models cost a fraction of a cent per short request; set a hard monthly limit in the billing settings.
What is the difference between Chat Completions and the Responses API?
Chat Completions is the long-standing stateless endpoint. The Responses API is the newer interface that adds built-in tools, state and simpler multi-turn handling. Both are supported; pick Responses for new projects that need tools.
How do I stop the API from cutting off long answers?
Raise max_tokens (or max_output_tokens) and check finish_reason. If it is length the model ran into your cap, not the context window.
New to APIs?
Short guides for working with this API — and any other.
🛠️ Handy LarzOS tools
Free tools to help you work with this API.
Prompt ImproverLearn Prompt Engineering