The Right Write Truth
OpenAI API, step by step

How to Get a ChatGPT API Key (and Not Leak It)

Getting a ChatGPT API key takes about five minutes. Keeping it safe and understanding what it costs you is the part most tutorials skip, so this guide covers both.

How to get a ChatGPT API key: the short answer

A ChatGPT API key comes from OpenAI's developer platform, not from the ChatGPT app. You sign in at platform.openai.com, open the API keys page, create a new secret key, and copy it once. That key lets your own code send requests to the same family of models that power ChatGPT.

One thing trips up almost everyone: a ChatGPT Plus or other ChatGPT subscription does not include API usage. The API has its own billing, charged by how much you use. You can have one without the other.

Step by step: create your OpenAI API key

  1. Sign in to the developer platform. Go to platform.openai.com and log in with your OpenAI account, or create one. The same login works for ChatGPT.
  2. Set up billing. Open the Billing section of your account settings and add a payment method. New accounts usually need this before API calls will work. Many accounts use prepaid credits, so you top up a balance and usage draws it down.
  3. Pick or create a project. The platform groups keys and usage into projects. A separate project per app makes it easy to see what each one costs and to shut one down without touching the others.
  4. Create the key. Open the API keys page, choose Create new secret key, give it a name you will recognize later (for example "blog-summarizer-dev"), and confirm.
  5. Copy it right away. The full key is shown only once. Paste it straight into a password manager or your environment settings. If you lose it, delete it and make a new one.
  6. Set a spending limit. In the limits or billing settings, set a monthly budget or alert so a bug in your code can't quietly run up a bill.

Keep your API key secret

Treat an API key like a password tied to your credit card. Anyone who has it can make requests that you pay for. Leaked keys get found and abused quickly, especially ones pushed to public code repositories.

  • Store it in an environment variable. Set OPENAI_API_KEY on your machine or server instead of typing the key into your code. The official OpenAI libraries read that variable automatically.
  • Never commit it. If you use a .env file, add it to .gitignore before your first commit.
  • Never put it in front-end code. Anything in a web page or mobile app can be read by users. Send requests from your own server and have the browser talk to that server.
  • Don't paste it into chats, forums or screenshots. That includes asking an AI assistant to debug code with the key still in it.
  • Use separate keys. One key per app or environment means you can revoke a single leaked key without breaking everything else.
  • Rotate when in doubt. If you think a key has leaked, delete it on the API keys page and create a new one. Deleting it stops it working.

Billing basics without the guesswork

The API charges per token, which is a chunk of text roughly the size of a short word or part of a longer one. You pay for the tokens you send (your prompt plus any instructions and context) and the tokens the model sends back. Bigger, more capable models cost more per token than smaller ones, and prices change, so check OpenAI's current pricing page before you build a budget around a number.

A few habits keep costs predictable:

  • Start with a smaller model and move up only if the output isn't good enough.
  • Keep prompts lean. Pasting a whole book as context on every call adds up fast.
  • Set a maximum output length for each request.
  • Check the usage dashboard after your first day of testing so you know what normal looks like.

Your first ChatGPT API request

Once your key is saved as OPENAI_API_KEY, you can test it from a terminal with curl. Replace MODEL_NAME with a model listed in your platform dashboard or OpenAI's model documentation.

curl https://api.openai.com/v1/responses \

-H "Content-Type: application/json" \

-H "Authorization: Bearer $OPENAI_API_KEY" \

-d '{

"model": "MODEL_NAME",

"input": "Write one sentence about why API keys must stay secret."

}'

In Python, install the official library with pip install openai, then run:

from openai import OpenAI

 

client = OpenAI() # reads OPENAI_API_KEY from your environment

 

response = client.responses.create(

model="MODEL_NAME",

input="Write one sentence about why API keys must stay secret.",

)

print(response.output_text)

If you see a sentence printed back, your key, billing and model name are all working. From there you can build anything from a summarizer to a writing assistant. If you'd rather not build at all, our free summarizer and paraphrasing tool already do two of the most common jobs.

Common ChatGPT API errors and fixes

ErrorWhat it usually meansFix
401 Unauthorized / invalid API keyThe key is wrong, deleted or not being readCheck the environment variable is set in the same terminal or server that runs your code, with no extra spaces or quotes
429 insufficient_quotaNo credit or billing set up, or your budget limit was reachedAdd a payment method or credits, or raise your limit
429 rate limitToo many requests or tokens in a short windowSlow down, retry with a short wait that grows each time, or batch work
404 model not foundThe model name is misspelled, retired or not available to your accountCopy the exact model name from your dashboard or the docs
400 bad requestThe request body is malformedCheck your JSON, quotes and parameter names against the current docs

Most beginner problems are the first two. If the key works in curl but not in your app, the app is almost always not seeing the environment variable.

Using your key in other apps and plugins

Plenty of writing apps, WordPress plugins, spreadsheet add-ons and note-taking tools ask you to paste in your own OpenAI API key. This "bring your own key" setup can be handy, because you pay OpenAI directly for what you use. It also means you are trusting that app with a key tied to your billing.

  • Make a dedicated key for each app. Name it after the app so you know which one to revoke if something looks off.
  • Put that key in its own project with a low budget. If the app misbehaves or gets breached, the damage stops at the limit.
  • Check the usage page after a week. If an app you barely use shows heavy traffic, delete the key.
  • Only use apps from developers you can identify. A browser extension or plugin with no clear owner and a request for your key is a bad trade.

If the app also offers a plan where it pays for the AI and you pay the app, compare both. Sometimes the simpler bill is worth more than the savings.

API or ChatGPT: which do you need?

If you want to chat, write, brainstorm or study, the ChatGPT app is simpler and you don't need a key at all. Our beginner's guide to ChatGPT covers that. The API makes sense when you want to automate something, connect AI to your own app or website, or process lots of text the same way. It's a developer tool first, and it rewards a little care with keys and budgets.

Questions people ask

Is the ChatGPT API key free?

Creating a key costs nothing, but using it is billed by usage. OpenAI's free trial offers have changed over time, so check your billing page to see whether your account has any credit.

Does ChatGPT Plus include API access?

No. A ChatGPT subscription and API usage are billed separately. You need to set up billing on the developer platform to use the chatgpt api.

Where do I find my existing OpenAI API key?

You can't view a full key again after creating it. The API keys page shows only part of each key, so if you lost it, delete it and create a new one.

Is it safe to put my API key in JavaScript on my website?

No. Anyone can read front-end code and use your key. Keep the key on your server and have your site send requests through it.

What does 'insufficient_quota' mean?

Your account has no usable credit or has hit its spending limit. Add a payment method or credits, or raise your budget in the billing settings.

Can I use the chat gpt api key for an AI detector or plagiarism tool?

You can build tools on the API, but a language model alone is not a reliable AI detector. Our free AI checker and plagiarism checker are ready to use if you just need a result.

Skip the setup for everyday writing jobs

If you only need to check, summarize or rewrite text, our free tools do it in the browser with no key. Options for longer documents are coming soon.

See the free tools