ChatGPT API: What It Is, Pricing, Setup, and Examples

The ChatGPT API is the common name developers use when they want to add GPT-powered conversations, text generation, summarization, classification, extraction, or agents to their own apps.
Technically, you are using the OpenAI API. ChatGPT is the familiar product people use in the browser, while the API is the programmable interface that lets your software send requests to OpenAI models and receive structured responses.
That distinction matters. ChatGPT is an app. The ChatGPT API is a building block. With it, you can build customer support chatbots, internal copilots, knowledge base assistants, document workflows, code helpers, content tools, and product features that use AI behind the scenes.
This guide explains what the ChatGPT API is, how it works, how pricing is calculated, how to make a first request, and what to watch before putting it into production.
How this guide was researched#
This article was written for the search intent behind chatgpt api: developers and operators who need a practical, current introduction before they build.
We used top-ranking educational pages, including Chatbase's ChatGPT API guide, to understand the structure searchers expect: definition, API key setup, model choice, pricing, code examples, rate limits, and production best practices.
For fast-changing API details, we cross-checked against official OpenAI documentation:
| Topic | Primary source used |
|---|---|
| First API request and SDK setup | OpenAI developer quickstart |
| Current model families and model choice | OpenAI model catalog |
| GPT-5.6 Luna capabilities, endpoints, and rate-limit examples | OpenAI GPT-5.6 Luna model page |
OpenAI models, pricing, and endpoint guidance change frequently. Treat this article as a practical guide, then verify final implementation details in the official docs before shipping production code.
What is the ChatGPT API?#
The ChatGPT API is a developer interface for sending prompts, instructions, files, images, conversation history, and tool definitions to OpenAI models from your own application.
Instead of opening ChatGPT and typing into a chat window, your backend sends an HTTPS request. The request includes the model you want to use, the user input, optional system or developer instructions, and any settings that control the response. The API returns a structured result that your app can display, store, stream, or pass into another workflow.
In simple terms:
- A user takes an action in your app.
- Your server sends a request to the OpenAI API.
- The model generates a response based on the input and instructions.
- Your app receives the response and shows it to the user or uses it in a workflow.
The API can power chat experiences, but it is not only for chat. The same model call can generate product descriptions, classify support tickets, extract fields from documents, write SQL, rewrite copy, answer questions from retrieved context, or call tools that your application exposes.
ChatGPT vs. ChatGPT API#
ChatGPT and the ChatGPT API use related model capabilities, but they solve different problems.
ChatGPT is a hosted product. It includes the interface, conversation history, account experience, file upload flows, and user-facing features.
The ChatGPT API is for developers. It gives your product controlled access to models through code. You decide the interface, the data flow, the guardrails, the storage layer, the retrieval system, the logs, the permissions, and the user experience.
Use ChatGPT when you need an AI assistant as an individual user. Use the API when you need AI inside your own website, SaaS product, support workflow, data pipeline, or internal tool.
How the ChatGPT API works#
Most ChatGPT API integrations have five parts.
1. API key#
An API key authenticates your app with OpenAI. It should live on your server, in a secret manager, or in environment variables. Never expose it in browser JavaScript, mobile app binaries, public repositories, or client-side logs.
2. Model#
The model controls the capability, latency, and cost of the response. OpenAI's current model docs describe the GPT-5.6 family as including higher-capability, balanced, and cost-sensitive options. For many production apps, the right model is not always the most expensive one. It is the model that reaches your quality target at the lowest acceptable latency and cost.
3. Instructions and input#
Your request tells the model what to do. A support assistant might receive a customer question plus instructions to answer only from approved help center content. A data extraction workflow might receive a PDF and instructions to return valid JSON. A writing assistant might receive a brand voice guide and a draft paragraph.
4. Tools and data#
The API can be connected to tools such as function calls, file search, web search, or application-specific APIs. For company-specific answers, many teams use retrieval augmented generation, or RAG, so the model can answer from approved documents instead of relying only on general training data.
5. Response handling#
Your application decides what happens after the model responds. It might display the answer, stream tokens into a chat UI, save a conversation, trigger a workflow, show citations, ask for human approval, or retry with a cheaper or stronger model.
Common ChatGPT API use cases#
The strongest API use cases usually have a clear task, a known user, and a measurable quality target.
Customer support chatbots#
A support chatbot can answer common questions, summarize conversations for agents, draft replies, route tickets, and escalate when confidence is low. For support use cases, the model should be grounded in your help center, product docs, policies, and account data rather than answering from memory.
Knowledge base assistants#
An AI knowledge base assistant helps users search long documentation, ask natural-language questions, and get direct answers with source references. This is especially useful when a traditional keyword search returns too many pages.
Content and marketing workflows#
Teams use the API to generate outlines, rewrite pages, adapt messaging for different audiences, summarize research, create variants for A/B tests, and turn product notes into drafts. Human review still matters because tone, accuracy, and brand judgment are hard to fully automate.
Data extraction and classification#
The API can turn unstructured text into structured data. For example, it can classify support tickets, extract invoice fields, summarize call transcripts, normalize messy CRM notes, or identify sentiment and urgency.
Internal copilots#
Internal tools can help employees search company policies, summarize meetings, draft customer emails, query analytics, or navigate complex procedures. These tools need strong access control because internal knowledge is often sensitive.
Developer tools#
Developers use model APIs to explain code, generate tests, write migration scripts, review pull requests, create documentation, and translate error logs into likely fixes. The API is most useful when paired with repository context and clear constraints.
ChatGPT API pricing#
ChatGPT API pricing is based mainly on tokens. A token is a small unit of text. Roughly, longer prompts and longer answers use more tokens. You are usually billed separately for input tokens and output tokens, and some models discount cached input.
OpenAI's model catalog changes over time, so always verify current pricing in the official OpenAI model and pricing pages before estimating production cost. As of the current OpenAI model catalog, examples from the GPT-5.6 family include:
| Model | Good fit | Input price | Output price |
|---|---|---|---|
| GPT-5.6 Sol | Complex reasoning and coding | $4 / 1M tokens | $20 / 1M tokens |
| GPT-5.6 Terra | Balanced intelligence and cost | $2 / 1M tokens | $12 / 1M tokens |
| GPT-5.6 Luna | Cost-sensitive, high-volume workloads | $0.20 / 1M tokens | $1.20 / 1M tokens |
The exact cost of your app depends on more than the model's headline price:
- How many requests users send
- How much context you include in every request
- How long the model's answers are
- Whether you use streaming, tools, file search, or web search
- Whether prompts can be cached
- How often you retry failed requests
- Whether you route simple tasks to cheaper models
For a production estimate, measure real token usage from logs instead of relying only on averages. A short support answer can be inexpensive. A workflow that sends long documents, long conversation history, and long outputs can become expensive quickly.
Here is a simple cost-estimation formula:
monthly cost =
(monthly input tokens / 1,000,000 * input price)
+ (monthly output tokens / 1,000,000 * output price)
+ tool, file search, web search, batch, or realtime costs if used
For example, if an app sends 50 million input tokens and receives 10 million output tokens in a month, you would multiply each token total by the selected model's input and output rates. This estimate should be checked against actual usage logs after launch.
How to get a ChatGPT API key#
To use the ChatGPT API, create an OpenAI account and generate an API key in the OpenAI platform dashboard.
The basic setup flow is:
- Create or log in to your OpenAI platform account.
- Add billing information or credits if required for your usage.
- Create an API key for your project.
- Store the key in an environment variable such as OPENAI_API_KEY.
- Use the key only from trusted server-side code.
For local development on macOS or Linux, you can export the key in your terminal:
export OPENAI_API_KEY="your_api_key_here"
In production, use your hosting provider's secret manager or environment variable system. Avoid saving keys in frontend code, screenshots, public issue trackers, analytics tools, or error messages.
Make your first ChatGPT API request#
The official OpenAI quickstart recommends using the OpenAI SDK and the Responses API for new text generation workflows.
Install the JavaScript SDK:
npm install openai
Then create a simple request:
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
input: "Write a two-sentence welcome message for a support chatbot.",
});
console.log(response.output_text);
For Python, install the SDK:
pip install openai
Then call the API:
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5",
input="Summarize this support ticket in one sentence."
)
print(response.output_text)
These examples are intentionally simple. A real application usually adds user identity, conversation state, safety checks, logging, error handling, rate limiting, and a way to evaluate answer quality.
A production-ready ChatGPT API architecture#
A demo can call the ChatGPT API from one server route. A production system needs a few more layers.
| Layer | What it does | Why it matters |
|---|---|---|
| Client UI | Chat window, form, editor, or workflow screen | Gives users a clear place to ask, review, and act |
| Backend API | Receives user requests and calls OpenAI | Keeps API keys private and enforces permissions |
| Prompt layer | Adds instructions, examples, context, and formatting rules | Makes outputs more consistent and easier to evaluate |
| Retrieval layer | Finds relevant documents, help articles, or database records | Grounds answers in approved business knowledge |
| Safety layer | Filters sensitive inputs, blocks unsafe actions, and controls scope | Reduces security and compliance risk |
| Observability layer | Logs token usage, latency, errors, feedback, and answer quality | Helps control cost and improve reliability |
This is why many teams can build a prototype quickly but need more time to ship a reliable support assistant, knowledge base chatbot, or internal copilot.
Responses API vs. Chat Completions API#
Many older tutorials teach the Chat Completions API because it has been widely used for chat-style apps. You will still see examples that send an array of messages with roles such as system, user, and assistant.
For new projects, the Responses API is often the better starting point because it is designed for text generation, multimodal input, tool use, streaming, and agent-style workflows in one interface.
The practical rule is simple:
- Use the Responses API for new OpenAI integrations unless you have a specific reason not to.
- Use Chat Completions when maintaining an existing integration that already depends on that format.
- Test before migrating because response shape, tool handling, and conversation state may differ.
Best practices for using the ChatGPT API#
Good ChatGPT API integrations are less about one clever prompt and more about the system around the model.
Keep secrets server-side#
Do not call the API directly from a public browser client. Route requests through your backend so you can protect API keys, validate inputs, enforce user permissions, and track usage.
Write specific instructions#
Tell the model its role, the task, the allowed sources, the response format, and what to do when it is unsure. Specific instructions reduce ambiguity and make behavior easier to evaluate.
Use structured outputs when you need data#
If your app expects JSON, define the shape you want instead of asking the model to "return JSON" in plain language. Structured outputs make downstream automation safer.
Ground answers in your data#
For product support, legal policies, medical workflows, finance, or any company-specific task, do not rely on the model's general knowledge alone. Retrieve relevant content from your approved sources and include it in the request.
Control cost with model routing#
Use cheaper models for simple classification, rewriting, or high-volume tasks. Use stronger models for complex reasoning, sensitive workflows, or tasks where accuracy has a bigger business impact.
Limit prompt size#
More context is not always better. Long prompts cost more, increase latency, and can bury the most important facts. Retrieve the smallest useful set of documents or conversation history.
Stream user-facing answers#
For chat interfaces, streaming improves perceived speed because users can see the answer appear as it is generated.
Add retries and fallbacks#
Production systems should handle timeouts, rate limits, transient errors, and malformed outputs. Use exponential backoff, idempotency where appropriate, and user-friendly fallback messages.
Log enough to improve quality#
Track prompts, retrieved sources, model choice, latency, token usage, answer quality signals, and user feedback. Keep privacy requirements in mind, especially for sensitive customer or employee data.
Rate limits and reliability#
The ChatGPT API has rate limits that depend on your account, model, usage tier, and endpoint. Rate limits can include requests per minute, tokens per minute, and batch queue limits. OpenAI's model pages show model-specific limit examples, but your actual limits may differ by account and usage tier.
Design your app as if limits and temporary failures will happen. Queue non-urgent jobs, stream long responses, retry transient errors, and show clear messages when the system is busy.
For high-volume use cases, estimate both average load and peak load. A marketing content workflow might be bursty during campaign planning. A support chatbot might spike during outages. Internal copilots can surge after a company-wide launch.
How to choose the right model#
Start with the job your app needs to do, not the model name.
For simple classification, extraction, rewriting, and high-volume support automation, a cost-sensitive model may be enough. For complex reasoning, code generation, long document synthesis, or workflows where mistakes are expensive, test a stronger model.
A practical model selection process:
- Write 20 to 50 realistic test cases.
- Define what a good answer looks like.
- Test at least two model options.
- Measure accuracy, latency, and token cost.
- Choose the cheapest model that meets the quality bar.
- Re-test when prompts, data, or model versions change.
Do not optimize only for cost. A cheaper model that creates more escalations, rework, or user frustration can be more expensive in practice.
How the ChatGPT API compares with a chatbot platform#
Using the ChatGPT API directly gives you flexibility. You can design the interface, choose the data pipeline, control storage, and build custom workflows. That flexibility is valuable when you have engineering resources and unique requirements.
A chatbot platform gives you speed. It usually includes a widget, crawling, document ingestion, knowledge base sync, analytics, conversation management, and integrations that would take time to build yourself.
Choose the API directly when:
- You have developers available to build and maintain the system.
- You need deep product-specific customization.
- You already have infrastructure for secrets, logs, queues, and monitoring.
- You want full control over the user interface and data pipeline.
Choose a platform when:
- You need a website chatbot or knowledge base assistant quickly.
- You do not want to build retrieval, ingestion, analytics, and chat UI from scratch.
- Non-technical team members need to manage content and behavior.
- You care more about time to value than low-level control.
For a deeper comparison, see our guide on ChatGPT API vs. chatbot platforms.
Common mistakes to avoid#
The fastest way to waste API budget is to ship without a clear operating model. Watch for these mistakes:
- Putting API keys in frontend code
- Sending the entire knowledge base on every request
- Using a premium model for every task by default
- Trusting model answers without retrieval or citations
- Ignoring token usage until the bill arrives
- Storing sensitive prompts without a privacy review
- Building a chatbot without human escalation
- Measuring only latency and cost, not answer quality
- Treating a demo prompt as production behavior
The API can be powerful, but production quality comes from evaluation, good data, security, and iteration.
ChatGPT API launch checklist#
Before shipping a ChatGPT API feature to real users, review this checklist:
- API keys are stored server-side and rotated when needed.
- User permissions are checked before private data is retrieved or sent.
- Prompts define role, task, allowed sources, format, and refusal behavior.
- The app logs model, latency, token usage, errors, and feedback.
- Cost estimates use real token measurements from test traffic.
- Rate-limit handling includes retries, backoff, and graceful fallbacks.
- Outputs are evaluated against realistic test cases.
- Sensitive prompts and responses follow your privacy policy.
- User-facing answers are grounded in retrieved sources when factual accuracy matters.
- A human escalation path exists for support, legal, finance, or high-risk workflows.
Frequently asked questions#
Is there an official ChatGPT API?#
People often say "ChatGPT API," but the official developer product is the OpenAI API. It gives developers access to GPT models and related tools that can power ChatGPT-like experiences in external apps.
Is the ChatGPT API free?#
OpenAI may offer free trials or credits depending on account status and current policy, but production API usage is generally paid. Check the OpenAI platform for current billing and pricing.
Can I use the ChatGPT API on my website?#
Yes, but you should call it from your backend, not directly from browser code. For a website chatbot, you also need a chat UI, conversation storage, retrieval from your content, safety rules, and analytics.
Can the ChatGPT API answer from my documents?#
Yes, but you need to provide the relevant document context or connect the model to a retrieval system. For reliable knowledge base answers, use RAG or file search rather than assuming the model already knows your private content.
Does the ChatGPT API know my private business data?#
No. The API does not automatically know your private documents, website, CRM, support tickets, or internal policies. You need to pass relevant context in the request, connect a retrieval layer, or use a platform that indexes approved sources for you.
How much does one ChatGPT API request cost?#
It depends on the model and the number of input and output tokens. Short requests to lower-cost models may cost a small fraction of a cent. Long requests with retrieved documents, conversation history, tools, and long answers cost more. The safest approach is to log token usage during testing and estimate from measured traffic.
Should I use the Responses API or Chat Completions API?#
For new projects, start with the Responses API unless your stack already depends on Chat Completions. The Responses API is designed for newer text generation, tool use, multimodal, streaming, and agent-style workflows.
Which model should I use?#
Use the model that meets your quality, latency, and cost requirements. Start with realistic test cases, compare model options, and route simple tasks to cheaper models when possible.
Start building with Denser#
Denser supports ChatGPT API workflows for teams that want AI chat, website assistants, and knowledge base experiences without building every layer from scratch.
If you want a faster path from content to production chatbot, start building free with Denser.