# OpenAI API

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

Web page: https://buildladder.com/tools/openai-api

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.

- Made by: OpenAI
- Official site: https://developers.openai.com/api/docs/quickstart
- Cost: 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.
- What you need first: An OpenAI API platform account, some credits, and an API key. The key is a secret code your app’s server uses to call OpenAI.
- Who it helps: 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.
- Updated: 2026-11-09

## 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.

What you’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.

### 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 (https://platform.openai.com): 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.

### 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.

Compare OpenAI API with the other options: https://buildladder.com/concepts/ai-features.md
