123DevOps··9 min read

How to use the OpenAI API: API key, Responses API, first call, and best practices

A step-by-step guide to the OpenAI API: dashboard and docs, projects, the Playground, storing your API key safely, your first Responses API call with curl and Python, tools, the Agents SDK, what is being retired, and how to handle cost, data, and security.

AI

Using ChatGPT in the browser covers a lot. But if you want your own system to summarize tickets or classify emails on its own, you need the OpenAI API. This guide goes from creating an account to the first answer in your terminal and covers what to take care of before production. For the full map of products, see the overview of OpenAI’s AI.

A heads-up: OpenAI renamed, moved, and retired a lot in 2025 and 2026. If a screen or an old tutorial doesn’t match what you see, check the official pages.

Dashboard and docs: two addresses

  • platform.openai.com is the dashboard: keys, usage, limits, projects, and organization settings.
  • developers.openai.com/api/docs is the documentation: quickstart, guides, reference, and the list of models. Old platform.openai.com/docs links redirect there.

The API itself is an ordinary REST API: you send JSON over HTTPS and get JSON back.

Step by step: from account to API key

1. Create an account and a project

Create an account at platform.openai.com. It is separate from your ChatGPT subscription: with the API you pay for usage, measured in tokens (chunks of text).

Next, create projects. Each one isolates an environment, with its own access, request limits (rate limits), and spend limits. Keeping staging apart from production stops a runaway test from eating the production budget.

2. Test in the Playground

Before writing code, test your prompt in the dashboard’s Playground: developer instructions (the general rules), the user message, the model, and run. Once the answer looks right, the option to view the code shows the same call ready to use. Button names may change.

3. Create the key under API keys

On the dashboard’s API keys page, click “Create new secret key” (the label may vary), pick the project, and give it a name that says where it will be used, such as support-staging. Copy it right away: the full key is usually shown only once. Lost it? Create a new one and revoke the old one.

Animated illustration: from the OpenAI Playground to creating an API key Animated illustration (not a real screenshot): in the Playground, developer instructions and a user message, picking the model, Run, and the answer; then “Code” with the curl call to /v1/responses (Authorization: Bearer $OPENAI_API_KEY), API keys › “Create new secret key”, and the key copied once into an environment variable. The real interface may use different names and positions.

The dashboard paths you will use most:

Where in the dashboard What it’s for
/api-keys Create and revoke keys
/usage Track consumption
/settings/organization/limits See rate limits and usage limits
/settings/organization/team Manage who has access
/tokenizer See how text turns into tokens

Keep the key out of your code

The key is a password: whoever has it spends on your behalf. Never put it in code or in Git. On your machine, use an environment variable; on servers and pipelines, a secrets manager. The reasoning is in environment variables and the 12-factor app.

The official quickstart uses export OPENAI_API_KEY=.... A variation that keeps the key out of your shell history:

# Read the key without echoing it and without saving it to shell history
read -rs OPENAI_API_KEY && export OPENAI_API_KEY

# If you use a .env file, it never goes into Git
echo ".env" >> .gitignore

First call with curl

The recommended API for new projects is the Responses API, at the /v1/responses endpoint. Replace <model> with the model id (I explain where to find it below):

curl https://api.openai.com/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{"model": "<model>", "instructions": "Answer in English, concisely.", "input": "Explain in 3 sentences what a REST API is."}'

The Authorization header carries the key. In the body, instructions holds the general rules (role, tone, format) and input is the request. The response is JSON with an output list, which holds the model’s message and sometimes other items, such as tool calls.

Reading just the text with jq

To pull out only the answer text, filter for message items and, inside them, the text parts:

curl https://api.openai.com/v1/responses \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{"model": "<model>", "instructions": "Answer in English, concisely.", "input": "Explain in 3 sentences what a REST API is."}' \
  | jq -r '.output[] | select(.type=="message") | .content[] | select(.type=="output_text") | .text'

With the official SDKs you don’t need this filter: they offer output_text as a shortcut.

In Python, with the official SDK

Install the SDK and also set the model through an environment variable, so you can switch it without touching the code:

pip install openai
export OPENAI_MODEL="<model id>"
import os
from openai import OpenAI

client = OpenAI()  # reads OPENAI_API_KEY from the environment

response = client.responses.create(
    model=os.environ["OPENAI_MODEL"],
    input="List 3 precautions for storing an API key.",
)

print(response.output_text)

In JavaScript the shape is the same: client.responses.create(...) and then response.output_text.

Responses or Chat Completions?

The docs are clear: Chat Completions is still supported, but Responses is recommended for all new projects. Existing code keeps working; migrate at your own pace, using the official guide.

The Assistants API, however, was shut down on August 26, 2026. The migration path is Responses plus Conversations: what used to be a thread becomes a conversation, and what used to be a run becomes a response.

Tools in the Responses API

In the Responses API, the model can use tools while it builds its answer. The main ones:

Tool What it’s for
web_search Look up current information on the web
file_search Search files you uploaded
code_interpreter Run code for calculations and data analysis
Image generation Generate images within the response
Remote MCP (mcp) Call remote MCP servers
Computer use Operate a graphical interface, step by step
Function calling The model asks to call functions in your code

The list keeps growing (shell and skills also appear in the tools guide); the parameters are in the docs.

Agents SDK, Realtime, and Batch

The Agents SDK is an open-source library (MIT license) for building agents with tools, in Python and in JavaScript/TypeScript:

pip install openai-agents

The Realtime API handles voice and real-time conversations, over WebRTC or WebSocket. Batch processes requests in bulk, asynchronously, for work that doesn’t need an immediate answer; among other endpoints, it accepts /v1/responses itself.

What is being retired

According to the deprecations page in the docs (check it before you decide):

Item Status Suggested path
Assistants API Shut down on 08/26/2026 Responses + Conversations
Agent Builder (AgentKit) Scheduled to shut down on 11/30/2026 Agents SDK or ChatGPT Workspace Agents
Evals (dashboard, API, and graders) Scheduled to shut down on 11/30/2026 OpenAI points to Promptfoo
Prompts API (/v1/prompts) Scheduled to shut down on 11/30/2026 See the deprecations page
Fine-tuning Winding down, closed to new users See the deprecations page

For fine-tuning, the docs say self-serve job creation is restricted starting 12/15/2026. If you depend on it, read the official page now.

How to choose a model

The latest GPT models come in several tiers, from most capable to most efficient, plus models for images and voice. Start with the model from the official quickstart, test with real cases, and try a faster tier for simple tasks, such as classifying text.

Model ids change with every release. Copy the current one from the Models page, at developers.openai.com/api/docs/models, and store it in OPENAI_MODEL: switching models becomes a config change, not a code change.

Costs and limits without surprises

Prices change and depend on the model; check the official page. The habits that help most:

  • A spend limit per project, so a bug doesn’t turn into a bill.
  • Rate limits: see yours at /settings/organization/limits; on a 429 error, retry with increasing waits (backoff).
  • Batch for work that can wait.
  • Short answers: ask in the prompt itself for only the length you need.
  • Check /usage often, broken down by project.

Your data: training, retention, and privacy laws

According to OpenAI’s enterprise privacy page, data sent through the API is not used to train models by default, unless you opt in. Abuse-monitoring logs are kept for up to 30 days. Approved customers can use Zero Data Retention (ZDR) or modified abuse monitoring. Response state is kept for 30 days by default, and conversations until you delete them.

Privacy laws such as Brazil’s LGPD or the GDPR still apply: personal data needs a legal basis, minimization, and records. Never paste secrets, customer data, or production credentials into prompts. This guide is not legal advice; involve your company’s legal team and data protection officer.

Security: think OWASP for LLMs

LLM applications have their own risks, such as prompt injection (text that tries to change the model’s instructions) and agents with too much permission. The OWASP for LLMs security guide covers each one. The basics: validate the model’s output before executing or storing it, give tools the least permission they need, use one key per application, and revoke any exposed key immediately.

Checklist

  • I know where the dashboard (platform.openai.com) and the docs (developers.openai.com/api/docs) are
  • I created separate projects per environment, each with a spend limit
  • The key lives in an environment variable or secrets manager, never in Git
  • My first Responses API call worked, with the model coming from OPENAI_MODEL
  • I checked the deprecations page for Assistants, Agent Builder, Evals, Prompts, and fine-tuning
  • I handle 429 errors and use Batch for work that can wait
  • I reviewed data retention, privacy laws, and the OWASP risks for LLMs

The first call takes five minutes. What separates a test from a product is everything else: a well-kept key, projects with limits, minimal data, and output that is always validated.

Get updates on new posts and stay in the loop!