Skip to content

Services · by OpenAI

OpenAI API

Connect your app to OpenAI models to build features such as turning a customer message into a draft reply.

Say you run a shop and want help answering common questions. Your app can send a customer’s question and your returns policy to an OpenAI model, then display its proposed answer for someone at the shop to review.

Try two sample questions. Ask one that the policy answers and one it leaves open. Reading both drafts helps you decide when the feature can save time and when it should ask a person for help.

When to use it
When your app needs to write, summarize, answer questions or read images with OpenAI’s models.
When to skip it
If you only want an AI tool to write your code. You need it only when your finished app uses AI itself.
What it costs
Pay per use with prepaid credits, from $5. Credits expire after a year. On November 9, 2026, GPT-6 Luna cost $0.10 per million input tokens and $0.50 per million output tokens.

Add AI features with the OpenAI API

Add one AI feature from your plan, like a summary or a helper chat, using the OpenAI API. Your app calls OpenAI from its server, so your key stays secret.

You’ll need

  • A website or app you started, open in your AI tool.
  • PLAN.md from the Plan your idea guide.
  • A payment card, to buy credits.
  • OpenAI account. It holds the credits your app spends.

Your part

  • Create the OpenAI account and set a spending limit.
  • Create the API key and paste it into your .env file.
  • Try the feature and check what it cost.

Copy the prompt for these steps

All 5 steps, in one prompt
I’m following “Add AI features with the OpenAI API” on BuildLadder.
What the guide says I’ll end up with: One AI feature from your plan, working in your app, with a spending limit set and your key kept out of your code.
This comes from https://buildladder.com/tools/openai-api#ai-features. If you can open web pages, read https://buildladder.com/tools/openai-api.md for the current version. BuildLadder guide, tool, concept, answer and setup pages all work this way: add .md to the address.

How to work with me:
- Go through the steps below in order, one at a time.
- Do what you can yourself in this folder: write code, run commands, check settings. If a service has an official command-line tool, like the Vercel CLI, you can offer to use it. Ask me before you install anything, sign in to anything or spend money.
- When a step needs me, like making an account or clicking in a dashboard, tell me exactly what to do in small steps. Then wait until I say it’s done.
- The actions, checks and fixes in each step were written for me, the reader. Where one says “your AI tool”, that means you, and “Say:” means something I would say to you.
- Never ask me to paste passwords or secret keys into this chat. I type those into the service’s own settings.
- After each step, tell me how to check it worked. Once Git is set up, save a version each time a step works. Then ask if I’m ready for the next one.
- Use plain words. I may never have coded before.

## Step 1 of 5: Create an account and add credits

This step is mine to do. Walk me through it, one action at a time:
- Go to platform.openai.com and sign up.
- Open the billing overview in Settings. Select Add payment details and enter your card.
- Choose your first credit amount. The form suggests $10, and you can lower it to $5.
- Before you confirm, find Use auto-reload. It starts turned on. Turn it off, so you’re never charged more without asking.
- Open Project settings and select Limits. In Spend, select Edit spend limit and enter a Monthly spend limit.
- Turn on Enforce a hard limit, then select Save.

It worked when: Your OpenAI account has credits, auto-reload is off, and a monthly spending limit is set.

Before we move on, check with me:
- Does the Limits page show your monthly limit with Enforce a hard limit turned on?

If something goes wrong:
- Your balance looks wrong after you paid. Wait a few minutes. It can take a little while to update.
- You see Buy credits or Add to credit balance instead of Add payment details. Your card is already saved. Click whichever one appears to add credits.

## Step 2 of 5: Create a key and save it in .env

This step is mine to do. Walk me through it, one action at a time:
- Go to platform.openai.com/api-keys and create a new secret key. Name it after your app.
- Copy the key now. OpenAI shows the full key only once. Paste it only into your .env file, never into the chat.
- In your product’s folder, open the file named .env. If there isn’t one, ask your AI tool to create an empty one, then open it yourself.
- Add the line OPENAI_API_KEY=your-key, with your real key and no spaces. Save the file.
- Don’t save a version yet. “Add one AI feature” first checks that Git leaves your .env file out.

It worked when: Your .env file holds your key, and it is saved only on your computer.

Before we move on, check with me:
- Does the line start with OPENAI_API_KEY= and have no spaces?

If something goes wrong:
- You closed the window before you copied the key. You can’t see it again. Create a new key and copy it this time.
- You lost the key or pasted it somewhere public. Delete it with the trash can icon on the API keys page. Create a new one, then update the line in your .env file.

## Step 3 of 5: Add one AI feature

Read PLAN.md in this folder. It has my plan and the tools I chose.

I want to add one AI feature that uses the OpenAI API. Pick the one feature from my plan that fits best, like a summary or a helper chat. Tell me which one and wait for my OK.

Before you write code:
- Check that .env is listed in .gitignore. If it isn’t, add it. Don’t open or print .env.
- Create .env.example with only the variable name OPENAI_API_KEY and no value.
- Check OpenAI’s current official docs for the right library and a current, low-cost model.

Then build the feature:
- Call OpenAI only from server code, like an API route or a server function. Never call it from the browser or the phone app.
- Read the key from the OPENAI_API_KEY environment variable. Never put the key in code, logs or Git.
- Never use a variable name the browser or app can read, such as one starting with NEXT_PUBLIC_ or EXPO_PUBLIC_.
- If my app has no server part yet, suggest the simplest one for my setup and ask me first.
- Cap the answer length with the API’s maximum output setting, so each answer stays short and cheap.

Keep everything that already works.

It worked when: Your app shows an answer from OpenAI when you try the feature on your computer.

Before we move on, check with me:
- Does your AI tool say the file that calls the API is server code, not a page or screen?

If something goes wrong:
- It says the API key is missing or invalid. Check that the line in your .env file starts with OPENAI_API_KEY= and has no spaces. Then restart your app so it reads the file again.
- It says your credit balance is used up. Buy more credits on the billing overview. Wait a few minutes for the balance to update, then try again.
- It says you reached your spend limit. Your limit worked. Raise it on the Limits page if you want to keep going this month.

## Step 4 of 5: Handle waiting, errors and limits

My AI feature works. Now make it safe and friendly to use.

Check OpenAI’s current official docs for the errors the API can return, including credit_balance_exhausted and project_spend_limit_exceeded. Then:
- Show a clear loading message while the answer is on its way, and stop people from sending the same request twice.
- Show a short, friendly message when something fails. Keep the real error in the server logs only, and never log the key.
- Give these cases their own plain message: too many requests (a rate limit), out of credits or over my spending limit, and the service being busy.
- Limit how long a person’s input can be, and keep the answer length capped.
- Add a simple limit on how many times one person can use the feature in a short time, so nobody can run up my bill.

Keep everything that already works. Make only these changes. Then give me a short list of things to try, and what I should see for each one.

It worked when: Your feature shows a loading message and friendly errors, and stops one person from using it too often.

Before we move on, check with me:
- When you send an empty box, is the message helpful?
- When you paste in a very long text, does it stop you politely?

If something goes wrong:
- You can’t make an error happen to test it. Ask your AI tool to show you how to fake each error for a moment on your computer, then undo it.

## Step 5 of 5: Try it and check the cost

This step is mine to do. Walk me through it, one action at a time:
- Use the feature five times with real examples from your plan.
- Make a mistake on purpose, like sending an empty box. Check the message makes sense.
- Ask your AI tool to build the app and check that your key is in no file a visitor’s browser or phone downloads.
- Open the Usage page in your OpenAI settings and see what those tries cost.
- Write the cost of one use into PLAN.md. If it’s too high, ask your AI tool for a cheaper model or shorter answers.
- When you put your app online, add the same key in your host’s settings. The .env file doesn’t go online.

It worked when: The feature works with real examples, your key stays hidden, and you know roughly what one use costs.

If something goes wrong:
- Your credit balance shows a number below zero. Requests can run for a short time after credits run out. The amount comes off your next purchase. Keep your spend limit on, since the balance isn’t an instant cutoff.
- The feature works on your computer but not online. Your host needs the key too. Add OPENAI_API_KEY in its settings, then put your app online again.

The steps

  • Steps marked AI tool are done by your AI tool when you paste the prompt. The rest are yours.
  • Save a version after every step that works.
  • If a screen looks different from a step, ask your AI tool where the setting moved.
  • Never paste a password or secret key into the chat.
  1. Your account and key are ready

    Create an account and add credits

    You

    The OpenAI API is how your app talks to OpenAI’s models. You buy credits first, then set a spending limit, so a mistake can’t run up a big bill.

    1. Go to platform.openai.com and sign up.
    2. Open the billing overview in Settings. Select Add payment details and enter your card.
    3. Choose your first credit amount. The form suggests $10, and you can lower it to $5.
    4. Before you confirm, find Use auto-reload. It starts turned on. Turn it off, so you’re never charged more without asking.
    5. Open Project settings and select Limits. In Spend, select Edit spend limit and enter a Monthly spend limit.
    6. Turn on Enforce a hard limit, then select Save.

    It worked when: Your OpenAI account has credits, auto-reload is off, and a monthly spending limit is set.

    Check this step
    • Does the Limits page show your monthly limit with Enforce a hard limit turned on?
    Your balance looks wrong after you paid.

    Wait a few minutes. It can take a little while to update.

    You see Buy credits or Add to credit balance instead of Add payment details.

    Your card is already saved. Click whichever one appears to add credits.

  2. Create a key and save it in .env

    You

    An API key is a secret code that lets your app use your account. It goes in your .env file, which stays on your computer and out of your shared code.

    1. Go to platform.openai.com/api-keys and create a new secret key. Name it after your app.
    2. Copy the key now. OpenAI shows the full key only once. Paste it only into your .env file, never into the chat.
    3. In your product’s folder, open the file named .env. If there isn’t one, ask your AI tool to create an empty one, then open it yourself.
    4. Add the line OPENAI_API_KEY=your-key, with your real key and no spaces. Save the file.
    5. Don’t save a version yet. “Add one AI feature” first checks that Git leaves your .env file out.

    It worked when: Your .env file holds your key, and it is saved only on your computer.

    Check this step
    • Does the line start with OPENAI_API_KEY= and have no spaces?
    You closed the window before you copied the key.

    You can’t see it again. Create a new key and copy it this time.

    You lost the key or pasted it somewhere public.

    Delete it with the trash can icon on the API keys page. Create a new one, then update the line in your .env file.

  3. Your AI feature works

    Add one AI feature

    AI tool

    Add one AI feature from your plan. Your app’s server calls OpenAI, so the key never reaches a visitor’s browser or phone.

    It worked when: Your app shows an answer from OpenAI when you try the feature on your computer.

    Check this step
    • Does your AI tool say the file that calls the API is server code, not a page or screen?
    It says the API key is missing or invalid.

    Check that the line in your .env file starts with OPENAI_API_KEY= and has no spaces. Then restart your app so it reads the file again.

    It says your credit balance is used up.

    Buy more credits on the billing overview. Wait a few minutes for the balance to update, then try again.

    It says you reached your spend limit.

    Your limit worked. Raise it on the Limits page if you want to keep going this month.

  4. Handle waiting, errors and limits

    AI tool

    AI answers take a few seconds, and sometimes fail. Without limits, one person could also use up your credits.

    It worked when: Your feature shows a loading message and friendly errors, and stops one person from using it too often.

    Check this step
    • When you send an empty box, is the message helpful?
    • When you paste in a very long text, does it stop you politely?
    You can’t make an error happen to test it.

    Ask your AI tool to show you how to fake each error for a moment on your computer, then undo it.

  5. Try it and check the cost

    You

    Use the feature the way a visitor would. Then look at your usage with OpenAI, so you know roughly what each use costs.

    1. Use the feature five times with real examples from your plan.
    2. Make a mistake on purpose, like sending an empty box. Check the message makes sense.
    3. Ask your AI tool to build the app and check that your key is in no file a visitor’s browser or phone downloads.
    4. Open the Usage page in your OpenAI settings and see what those tries cost.
    5. Write the cost of one use into PLAN.md. If it’s too high, ask your AI tool for a cheaper model or shorter answers.
    6. When you put your app online, add the same key in your host’s settings. The .env file doesn’t go online.

    It worked when: The feature works with real examples, your key stays hidden, and you know roughly what one use costs.

    Your credit balance shows a number below zero.

    Requests can run for a short time after credits run out. The amount comes off your next purchase. Keep your spend limit on, since the balance isn’t an instant cutoff.

    The feature works on your computer but not online.

    Your host needs the key too. Add OPENAI_API_KEY in its settings, then put your app online again.

Compare OpenAI API with the other options

Stuck? Try these, in order
  1. Stop asking it to “fix it”. Say exactly what’s wrong.

    Paste this into the same chat. It asks your AI tool to find the problem with you.

    Troubleshooting prompt
    I’m following “OpenAI API” on BuildLadder and something isn’t working.
    This comes from https://buildladder.com/tools/openai-api. If you can open web pages, read https://buildladder.com/tools/openai-api.md for the current version. BuildLadder guide, tool, concept, answer and setup pages all work this way: add .md to the address.
    
    Ask me which step I’m on, what I tried and what happened, one question at a time. Look at the files in this folder if you can. Then explain the problem in plain words and suggest the smallest fix. Don’t change anything that already works.

  2. Check the service isn’t down.

    If you use one of these, open its status page before changing any code: Supabase, Vercel, Netlify, Cloudflare, Stripe, Clerk, Firebase, Resend, GitHub, Expo, Claude, OpenAI.

  3. Go back to your last saved version.
    Troubleshooting prompt
    Take my product back to the last saved version that worked. First show me which version that is and what will change. Don’t delete anything else.

  4. Make the change smaller.
    Troubleshooting prompt
    Let’s make a smaller change. Undo what you just tried. Then do only the first part of it, and tell me how to check that part works before we go on.

  5. Ask a person.

    Copy this, fill in each line, and post it in a community like r/vibecoding. Take out any keys or passwords first.

    Problem report
    What I'm building: (one sentence)
    What I asked my AI tool to do: (the last request)
    What I expected: (what should happen)
    What happened instead: (what you see)
    The exact error, if any: (copy it here)
    What I've tried: (each attempt, in order)
    My tools: (AI tool, website or app, hosting)

Look things up

Questions people ask

Words used on this page

API
An API is a set of rules that lets one program ask another for data or actions. Your app uses APIs to talk to outside services, such as maps, payments, or weather.
Spending limit
A spending limit is the most you let a service charge you in a month. When you reach it, the service stops instead of billing more. Some services call it a spend cap.
API key
An API key is a long code that tells a service your app is allowed to use it. Many keys are secret, so treat them like passwords and keep them out of shared code.
Environment variable
An environment variable is a named setting stored outside your code, such as an API key or a database address. Your app reads it when it runs, so secrets stay out of your shared files.
Backend
The backend is the behind-the-scenes part of an app that visitors never see. It runs on a server, where it saves data and checks who is signed in.
Token
A token is a small chunk of text that an AI model reads and writes, often part of a word. AI tools count tokens to set usage limits, prices, and how much fits in the context window.
Rate limit
A rate limit is the most requests a service lets you make in a set time, like a minute or a day. Go over it and the service turns requests away for a while, often with a 429 error.
Error message
An error message is text a program shows when something goes wrong. It usually says what failed and where, which helps you or your AI tool find the fix.
Production
Production is the live version of your website or app that real people use. Changes there affect everyone, so you test them in a preview first.
Hosting
Hosting is a service that keeps your website on computers that stay online, so anyone can visit anytime. Many hosts have a free plan for small sites.

Official pages

Updated · OpenAI