Key takeaways
- Keep API keys secret in environment variables, never in code or the browser.
- A request has a model, input messages and optional parameters such as temperature.
- Ask for structured JSON output when your code must parse the answer.
- Tool calling lets the model request functions that your code runs.
- Control cost by limiting tokens, caching and choosing the right model; check official pricing.
What is the OpenAI API?
What is the API and when is it useful?
A hosted model API lets your application send text (and in some models images or other data) and receive a generated reply, without you running the model yourself. Typical uses are summarising documents, answering questions over your data, extracting fields from text, classifying messages and writing drafts for human review. Model names, features, limits and prices change often, so always check the official documentation before building.
How do I get started safely?
Create an account and an API key in the provider's dashboard. Store the key in an environment variable or a secrets manager, never in source code, a public repository or front-end JavaScript. Call the API from a server you control. Set usage limits and alerts in the dashboard so that mistakes cannot create a large bill. Rotate a key immediately if it leaks.
What does a request look like?
The exact SDK and endpoint names change, so follow the current reference. The structure is stable: a model name, the input and optional settings.
POST https://api.openai.com/v1/<endpoint>
Authorization: Bearer $OPENAI_API_KEY
Content-Type: application/json
{
"model": "<model-name>",
"input": [
{"role": "system", "content": "You are a concise assistant for a training institute."},
{"role": "user", "content": "Summarise this text in three bullets: ..."}
],
"temperature": 0.2,
"max_output_tokens": 300
}
Messages have roles: a system (or developer) message sets behaviour, user messages carry the request and assistant messages hold earlier replies in a conversation. The API is stateless unless you use a feature that stores state, so you send the history you want the model to see.
Which parameters matter?
| Parameter | Effect |
|---|---|
| model | Which model answers; affects quality, speed and cost |
| temperature or similar | Lower values give steadier output; higher values give more variety |
| max output tokens | Caps the length of the reply and the cost |
| stop sequences | Ends output at a chosen string |
| response format | Asks for JSON or a schema |
Some newer models fix or hide certain settings, so read the model page.
How do I get structured output?
When code must read the answer, ask for JSON that matches a schema, using the structured output feature if the model supports it. Describe each field clearly and still validate the result in your code, because any model output can be wrong or incomplete. For classification, use a small fixed list of allowed labels.
What is tool calling?
You describe functions (name, purpose and parameters) in the request. The model may reply with a request to call a function with arguments. Your code runs the function, such as a database lookup or a calculator, and sends the result back so that the model can write the final answer. The model never runs code by itself; you control what each tool can do. Validate arguments and limit tools to safe, read-only actions unless a human approves.
How do I handle long inputs?
Models have a context limit measured in tokens. Split long documents into chunks, summarise each and then combine, or retrieve only the relevant chunks (retrieval-augmented generation). Count tokens before sending when possible and keep prompts short and clear.
How do I control cost and speed?
Charges depend on input and output tokens and the model. Reduce cost by shortening prompts, limiting output length, caching repeated requests, using smaller models for simple tasks, batching non-urgent jobs where supported and logging token use per feature. Streaming responses show the first words sooner and improves the user experience without changing total cost. Check the current pricing page, which changes.
How do I handle errors and rate limits?
Expect errors such as invalid requests, authentication failures, rate limits and timeouts. Retry only temporary errors with exponential backoff and a limit, log the request ID for support and show users a friendly message. Do not retry blindly in a loop, because it can increase cost and worsen limits.
What data and safety rules apply?
Do not send secrets or personal data unless you have a lawful reason and have checked the provider's data policy and your organisation's rules. Treat user input as untrusted: it can try to override your instructions (prompt injection). Keep system instructions separate from data, limit tool powers, filter outputs where needed and keep a human in the loop for important actions.
How do I test an API-based feature?
Create a set of sample inputs with expected results, run it after every change to a prompt or model, record accuracy, cost and time, and review failures by hand. Pin a model version where possible so behaviour does not change unexpectedly.
Download OpenAI API Guide PDF
OpenAI API Guide PDF
The PDF carries the same content as this page in a clean, print-ready layout, so you can read it offline and keep it for revision.
4 pages · 106 KB · PDF · Updated 5 Nov 2026