What Is an API Key?

An API key is a long secret string a program sends with each request to prove which account is calling a service, such as a payment, maps or email provider. Treat most keys like a password: keep them on a server, never in website code, limit what they can do, and replace them if exposed.

API key, OAuth token and password compared
API keyOAuth access tokenPassword
Who uses itYour own code or serverAn app acting for a user who approved itA person signing in
How you get itCopy it from the provider's dashboardIssued after the user clicks AllowChosen by the person
Typical lifetimeUntil you rotate or delete itShort, then refreshedUntil changed
ScopeWhatever you allowed when creating itThe permissions the user grantedEverything the account can do

Updated

If you run a website or store, you meet API keys whenever you connect a service: payments, maps, email, a form tool or an AI agent. Most are secret, a few are meant to be public, and mixing them up is how bills and data leaks happen. Paste a key into the inspector below to see what kind it looks like; it runs entirely in your browser and sends nothing anywhere.

API key inspector

Paste a key or just its first few characters. The check runs in your browser only; still, prefer a test key or a dummy value over a live secret.

Paste something to inspect.

Using a key safely from a server

The key lives in an environment variable on the server. The browser calls your own endpoint, which adds the key and calls the provider.

.env (never committed)

Placeholder values only. Add .env to .gitignore so it never reaches your repository.

# .env
PAYMENTS_SECRET_KEY=sk_test_your_key_here
MAPS_BROWSER_KEY=your_restricted_browser_key

api/quote.js

A tiny server route. The secret is read on the server and never sent to the browser.

export default async function handler(req, res) {
  const r = await fetch("https://api.example.com/v1/quotes", {
    method: "POST",
    headers: {
      Authorization: `Bearer ${process.env.PAYMENTS_SECRET_KEY}`,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ amount: Number(req.body?.amount) || 0 }),
  });
  res.status(r.status).json(await r.json());
}

Public keys and secret keys

Some services issue a pair. A publishable key identifies your account from the browser but cannot do anything sensitive, while the secret key on your server can charge cards or read data. Stripe's docs mark only the publishable key as safe to expose.

A browser key is still worth restricting. Google Cloud's guidance is to add API key restrictions, which limit the ways a key can be used and so reduce the harm if someone copies it.

Leaked key checklist

First, rotate or delete the key in the provider's dashboard. Second, update the new key in every place that used the old one. Third, check the provider's usage logs for calls you did not make. Fourth, find how it leaked, such as a committed file or a front-end bundle, and close that gap.

How it works

  1. You create the key in the provider's dashboard and copy it once; many providers never show it again.
  2. Your server stores it in an environment variable or the host's secret settings.
  3. Each request to the provider carries the key, usually in an Authorization header.
  4. The provider looks the key up, checks what it is allowed to do, and answers or refuses.

Tips

Frequently asked questions

Is an API key the same as a password?

They are both secrets, but a password is for a person signing in while an API key is for software calling a service. Keys are long and random, usually limited to certain actions, and can be replaced without changing anyone's login.

Can I put an API key in my website's JavaScript?

Only if the provider says that key type is safe to expose. Stripe's publishable keys starting pk_ are meant for front-end code; its secret and restricted keys are not. Anything in the browser can be read by every visitor.

Where should I store API keys?

In a secrets vault or your host's environment variable settings. Stripe and Google both say not to put keys in source code or commit them to code repositories.

What should I do if my API key leaked?

Revoke or rotate it at once in the provider's dashboard, then put the new key in your server settings. GitHub's guidance on detected secrets is to rotate the affected credential immediately; scrubbing history alone does not make a live key safe.

API key or OAuth?

Use an API key when your own server talks to a service on your own account. Use OAuth when your app must act for other people on their accounts, because each of them then grants and can revoke access themselves.

Related: API key generator, .env file format, Next.js environment variables, Notion API key, Rate limiting explained.

Start for free or see all free tools. We.Inc is free to start, with no card needed. AI building is on paid plans from $20 a month.

More in Guides

We.Inc is an AI-powered website builder you can resell under your own brand. Launch a branded client dashboard, bill on Stripe Connect, and deliver AI-generated websites in minutes. White-label plans start at $99 a month for 25 client sites, with a 7-day free trial and no per-site fees.

Product

Who It's For

Features

Resources

Company

View Sitemap