• Docs
  • Talk to an expert
Blog
Blog
BlogProductCase studiesNewsInsights
Blog

What is an API key? Where to store it and how keys leak

APIsecurityAI Agents
Share

TL;DR

  • An API key is a secret code that lets an app or tool use a service, such as an AI model, a payment provider, or a maps service. The service bills and limits usage against your account through the key.
  • Treat it like a password. Anyone who has your key can use the service as you, and you pay for it.
  • Store keys in environment variables or a secrets manager, never in your code, and keep .env files out of Git.
  • Front-end variables are public. In frameworks such as Next.js and Vite, variables starting with NEXT_PUBLIC_ or VITE_ are built into code every visitor can read.
  • If a key leaks, revoke it and create a new one first. Deleting it from your code isn't enough, because copies already exist.
  • On Upsun Cloud, store keys as sensitive variables, set per project or per environment, so they never appear in your repository.

An API key is a long, unique code that identifies you, or your app, to an online service. When your app asks the service for something, such as a reply from an AI model or a payment, it sends the key along, and the service checks it, does the work, and records the usage against your account.

If you've set up an AI coding tool, connected an app to an AI model, or added payments or maps to a website, you've been asked for one. This guide explains what API keys do, where to keep them, the common ways they leak, and what to do if yours does.

What an API key does

Key takeaway: an API key tells a service who's asking, so it can allow the request and charge the right account.

JobWhat it means
Identifies youThe service knows which account each request comes from
Grants accessOnly requests with a valid key are accepted
Tracks usageRequests are counted against your account
Bills youPaid services charge your account for what the key uses
Limits usageServices can cap how many requests a key can make

Because a key is tied to your account and your bill, a stolen key can cost real money. A leaked AI model key, for example, can be used to run large workloads at your expense.

API keys are one of several ways to prove who's asking. Others include login tokens that expire, and OAuth, which lets you approve an app without sharing a password. API keys are popular because they're simple, which is also why they're easy to leak.

Where to store an API key

Key takeaway: keep keys out of your code, out of Git, and out of anything sent to the browser.

PlaceSafe?Why
Written in your codeNoAnyone who can see the code can see the key, including everyone with access to the repository
A .env file committed to GitNoGit keeps the key in the history, even after you delete it
A .env file kept out of GitFor local developmentFine on your own computer, as long as .gitignore excludes it
Environment variables on your server or platformYesThe key stays outside the code, and only the running app can read it
A secrets managerYes, for high-value keysKeys are stored encrypted, access is controlled, and use is recorded
Browser codeNever, for secret keysEvery visitor can read it

Our guides What are environment variables?, What is .env?, and What is a .gitignore file? cover each of these in detail.

The front-end trap: NEXT_PUBLIC_ and VITE_

Key takeaway: variables with these prefixes are copied into the code sent to every visitor's browser, so never use them for secret keys.

Front-end frameworks need a way to give browser code settings such as a public site address. They do it with special prefixes:

FrameworkPrefixWhat happens
Next.jsNEXT_PUBLIC_The value is written into the JavaScript sent to the browser
Vite (used by React, Vue, and Svelte projects)VITE_The value is written into the JavaScript sent to the browser
Create React AppREACT_APP_The value is written into the JavaScript sent to the browser

So NEXT_PUBLIC_OPENAI_API_KEY isn't secret. Anyone can open the browser's developer tools and find it. This is a common leak in apps built quickly with AI tools, because putting the key in a public variable is the fastest way to make a front-end feature work.

The fix is to keep the secret key on the server. The browser calls your own server, and your server calls the outside service with the key. Some services also offer public keys designed for browsers, usually restricted to your website's address. Those are fine to expose, but check the service's documentation to be sure which kind you have.

How API keys leak

Key takeaway: keys usually leak through ordinary mistakes, and automated bots find public ones quickly.

How it leaksExample
Committed to GitA .env file or a hard-coded key pushed to a repository, especially a public one
Built into front-end codeA secret key in a NEXT_PUBLIC_ or VITE_ variable
Shared in chat or ticketsA key pasted into a support ticket, team chat, or forum post
Logs and error messagesAn app that prints its settings, or an error page that shows them
AI coding toolsAn agent reads a .env file and copies the key into code, a commit, or a pull request description
Screenshots and screen sharingA key visible in a terminal or settings page during a demo

Automated bots constantly scan public code for keys, so a key pushed to a public repository can be found and misused soon after. Many code hosts, including GitHub, scan for known key formats and can block or flag them, but don't rely on that alone.

What to do if an API key leaks

Key takeaway: revoke the key first. Cleaning up the code comes after.

  1. Revoke the key, or rotate it, in the service's dashboard straight away. This is the step that stops misuse.
  2. Create a new key and store it properly, in an environment variable or secrets manager.
  3. Update every app and environment that used the old key.
  4. Check the service's usage and billing for activity you don't recognize, and contact the provider if you find any.
  5. Remove the key from your code, and find out how it leaked so it doesn't happen again.

Deleting the key from your code or rewriting Git history doesn't undo a leak. Once a key has been public, assume someone has copied it, and treat the old key as burned.

Keeping API keys safe with AI coding tools

Key takeaway: AI agents can read whatever is in your project folder, so keep real keys out of their reach where you can.

AI coding tools often need an API key themselves, and they work inside projects full of other keys. A few habits help:

  • Give AI tools their own keys, with spending limits set in the provider's dashboard where available.
  • Keep production keys out of local .env files. Use test keys locally, and keep live keys on your hosting platform.
  • Tell agents in your instruction file never to copy keys into code, commits, or messages. Our guide AGENTS.md vs CLAUDE.md vs .cursorrules explains how.
  • Review agent changes for anything that looks like a key before you commit.
  • Turn on secret scanning in your code host, so leaked keys are caught early.

Spending limits and usage caps also protect you if a key does leak. Our upcoming guide What is rate limiting? explains how limits protect apps and budgets.

API keys on Upsun Cloud

Key takeaway: on Upsun Cloud, store API keys as sensitive variables, so they're available to your app but never in your repository.

Upsun Cloud apps read API keys from environment variables that you set on the platform, not from files in your code. According to the Upsun docs on variables:

What to knowDetails from the Upsun docs
Where keys goProject variables for keys that are the same everywhere, or environment variables for keys that differ, such as test and live payment keys
Keeping them hiddenMark them as sensitive. They're hidden in the Console and in command line output, but your app can still read them. Once sensitive, a variable can't be switched back
Reading them in codeThe env: prefix makes the variable available as an ordinary environment variable, such as PAYMENT_API_KEY
Build vs runtimeYou choose whether a variable is available during the build, at runtime, or both. Keep secret keys runtime-only unless the build genuinely needs them
Preview environmentsSet the test key as a project variable and the live key on the main environment with inheritance turned off, so previews get the test key and only production gets the live one
upsun variable:create --level project --name env:PAYMENT_API_KEY --value "sk_test_..." --sensitive true
upsun variable:create --level environment --environment main --name env:PAYMENT_API_KEY --value "sk_live_..." --sensitive true --inheritable false

Values in .upsun/config.yaml are committed to Git, so never put keys there. Our guide What are environment variables? covers how Upsun Cloud variables work in more depth.

You can deploy on Upsun Cloud free for 15 days, with no card needed, or see how security and compliance work on Upsun Cloud.

Frequently asked questions

What is an API key in simple terms?

A secret code that lets an app use an online service on your behalf. The service uses it to know who's asking and which account to charge.

Is an API key the same as a password?

Not quite, but treat it like one. A password usually logs a person in. An API key lets an app or tool use a service, often with no other check.

Where should I store my API key?

In an environment variable on your server or hosting platform, or in a secrets manager. For local development, a .env file is fine if it's excluded from Git.

Is it safe to put an API key in NEXT_PUBLIC_ or VITE_ variables?

Only if the key is designed to be public. Those variables are built into code sent to every visitor's browser, so secret keys must stay on the server.

What should I do if I accidentally pushed an API key to GitHub?

Revoke or rotate the key in the service's dashboard immediately, create a new one, update your apps, and check your usage for misuse. Removing it from the code alone isn't enough.

Can someone use my API key to charge my account?

Yes. Many services bill usage to the account the key belongs to. Spending limits and usage alerts help limit the damage if a key is stolen.

Stay updated

Subscribe to our monthly newsletter for the latest updates and news.

Deploy possibility.
Try Upsun for free.

Build with DispatchDeploy on Cloud