How to Use the OpenAI API: Setup, Keys and Apps
Learn how to get an OpenAI API key, make your first API call, build apps and agents, and handle errors, costs, and rate limits safely.
What the OpenAI API can do
The OpenAI API lets your software send input to OpenAI models and use their results. You can build chat features, summarize files, analyze images, create speech, or connect an agent to tools. To use it, create an API key, keep it on a server, then send requests from your app.
The API is not the same as ChatGPT. ChatGPT is a ready-to-use product. The API is a set of building blocks for software you control. You choose the model, shape the input, and decide what your app does with each reply.
A common flow has three parts. Your app sends a request, the model returns a response, and your code shows or uses that result. More advanced apps can pass tool calls to your own code. That lets a model look up an order, search a help center, or start a task.
Start with one narrow task. A support reply draft is easier to test than a broad virtual assistant. The smaller goal makes it easier to check quality, cost, and risk.
Get an API key and make your first call
Use an OpenAI account to create an API key in the developer platform. The exact labels may change, but the key lives in your account’s API settings. OpenAI’s API key instructions explain where to find it.
- Sign in. Open the OpenAI developer platform and sign in to your account.
- Choose a project. Create or select a project for the app you are building.
- Create a secret key. Make a key for that project and copy it once. Store it in a password vault or your host’s secret store.
- Set a usage limit. Add a budget alert or project limit where available. Check usage as you test.
- Call the API from your server. Read the key from an environment variable, then send a request using an OpenAI SDK or HTTP client.
Here is a small Python example using the current SDK style. First, install the package with pip install openai. Set the OPENAI_API_KEY environment variable before running your script.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-4.1-mini",
input="Write a two-sentence welcome note."
)
print(response.output_text)
Never put a secret key in browser code, a mobile app, or a public code repository. Users can inspect those files and steal the key. If you suspect a leak, revoke that key, make a new one, and check recent usage.

Build different kinds of AI applications
The same API can support many app types, but the best design depends on the job. For a chat tool, send the user’s message and relevant context. For a document helper, extract or upload text, then ask a focused question about it.
A store could use the API to draft product descriptions from approved facts. A help desk could suggest answers from its own support material. A learning app might explain a topic at several reading levels. In each case, your code should check the result before taking an action that affects a user.
Models can also work with images and audio. For example, a field service app could let a worker send a photo of a broken part. A voice app could turn spoken requests into text, then read a reply aloud. Test these paths with real-world inputs, such as poor lighting or background noise.
For data-backed answers, give the model access to relevant sources rather than asking it to guess. Your app can retrieve a few matching help pages, then include them in the request. This approach can make answers more useful, but your app should still show sources or ask a person to review uncertain cases.
Keep private data to the minimum needed for the task. Do not send passwords, full payment details, or sensitive records without a clear need and suitable safeguards. Define what your app stores, and how long it keeps that data.

Use Agent Builder and other OpenAI tools
Agent Builder helps teams design workflows that use models and tools. Rather than sending one prompt and stopping, an agent can follow steps, call an approved tool, and use the result to decide what comes next. A tool might search a set of documents or ask your server for a delivery status.
To try Agent Builder, start with a task and map its steps. Add the model and instructions, then connect only the tools the task needs. Set clear limits for actions, test sample cases, and check where a person must approve the next step.
Think of a returns agent as a simple example. It could read a customer’s request, check the order through a tool, and suggest a return option. Your system should still confirm the policy and ask for approval before issuing a refund.
Agent tools can save time, but they do not make every action safe by default. Limit which data each tool can access. Log important actions and add checks before a tool can send messages, change records, or spend money.
For direct API work, use the OpenAI docs and SDKs to send model requests and connect tools in your code. Choose a visual builder when it helps you plan or test a flow. Choose code when you need tight control over your app’s rules and data.
What companies use OpenAI?
Public examples show how different firms apply OpenAI technology. Shopify has described using OpenAI models in its product work. Morgan Stanley has shared how it uses AI to help financial advisers find information in its knowledge base. Klarna has reported using an OpenAI-powered assistant for customer service.
These examples do not mean every firm uses the same product or setup. Some use ChatGPT Enterprise, while others build features with the API or combine both. The right choice depends on the task, the data involved, and how much control the company needs.
When you look at company examples, focus on the workflow, not just the brand name. Ask what task changed, how staff review results, and what the system can access. Those details help you judge whether a similar use fits your own team.
Follow sound API practices
Good API integration starts with safe key handling and clear limits. Keep secrets on a server, use separate keys for separate projects, and rotate a key if it may be exposed. Give each app only the access it needs, and watch usage for unusual spikes.
Plan for errors before launch. A request can fail because of a bad input, a network issue, a busy service, or a rate limit. Show users a calm message, record enough detail to diagnose the issue, and avoid logging private prompts or secret keys.
For temporary failures, retry with a short delay that grows after each attempt. Add a random pause to avoid many clients retrying at once. Do not retry every error: fix invalid requests instead, and cap the number of attempts.
Rate limits set how many requests or tokens your project can send in a given time. If you reach one, slow the request flow and retry after a delay. For busy apps, queue work, limit parallel calls, and cache results that do not change often.
Measure quality and cost with real examples. Track response time, failed requests, and the share of answers that need human edits. Set a budget for each feature, and test a smaller model when it can meet the same quality bar.
- Keep API keys out of client-side code.
- Set request timeouts and bounded retries.
- Handle rate limits with backoff and queues.
- Review outputs before high-impact actions.
- Track usage, cost, errors, and answer quality.
Ship one small feature first. Test it with expected inputs and awkward edge cases, then add limits and monitoring before expanding access. That gives your team a clear way to learn how the OpenAI API fits its work.
Step-by-step
- 01 Sign in and choose a project
Open the OpenAI developer platform and select or create a project for your app.
- 02 Create a secret key
Make a key for the project and store it in a server-side secret store. Do not add it to browser or mobile code.
- 03 Set up the SDK
Install an OpenAI SDK for your language and set the API key as an environment variable.
- 04 Send a small test request
Call a model with a simple prompt, then check the returned text and usage.
- 05 Add safeguards
Set timeouts, handle errors and rate limits, and review outputs before sensitive actions.
Frequently asked questions
- How do I get an OpenAI API key?
- Sign in to the OpenAI developer platform, select a project, and create a secret key. Store it in a secure server-side secret store.
- Can I use my OpenAI API key in a website?
- Do not place the key in browser code. Send requests from your server so visitors cannot view or copy the secret.
- What can I build with the OpenAI API?
- You can build chat features, document helpers, image or audio workflows, and agents that call approved tools. Pick one task and test it before adding more features.
- What is OpenAI Agent Builder used for?
- Agent Builder helps you design and test workflows where a model can follow steps and use connected tools. Set tool limits and add approval checks for sensitive actions.
- How should I handle OpenAI API rate limits?
- Slow requests, use a queue, and retry temporary failures with growing delays. Cap retries and fix invalid requests rather than sending them again.
- Which companies use OpenAI technology?
- Public examples include Shopify, Morgan Stanley, and Klarna. Their uses differ, so check each company’s own description for the specific product or workflow.