# What is an API key? And why never to paste one into code

> An API key is a secret code that lets an app use an online service, like a password for software. Learn how keys leak and the safe habits beginners need.

- URL: https://genkid.app/blog/api-keys-and-secrets
- Topic: Safety & privacy
- Published: 2026-10-29 (updated 2026-11-15)
- Publisher: GenKid (https://genkid.app)

## Key takeaways

- An API key is a unique code an app sends to an online service to prove which project is making the request — a bit like a password for software.
- If a key leaks, someone else can use the service as you — running up bills or reaching private data.
- Never write keys directly in code, paste them into AI chats, or share them in screenshots.
- Keep secrets in secure settings (like environment variables) that stay on the server, not in the browser.
- If a key leaks, tell an adult and revoke it immediately; deleting it from the code isn't enough.

## What is an API key?

An **API key** is a unique code, usually a long string of letters and numbers like `sk_test_4f9a...`, that an app sends to an online service to prove which project is making the request. Think of it as a password for your app: whoever has the key can use the service *as you*.

Apps use online services through an **API** — a way for programs to talk to each other (see [what is an API?](https://genkid.app/blog/what-is-an-api)). Many APIs need a key so they know who is asking, what they're allowed to do and who to bill.

Other kinds of secrets work the same way: database passwords, access tokens and private keys. Developers call them all **secrets**.

## Why leaked keys are a big deal

If someone gets your key, they might be able to:

- **run up bills** on a paid service — some keys are linked to payment cards;
- **read or change private data**, like the information your app stores;
- **pretend to be your app**, sending messages or making requests in your name.

Leaks happen more often than you'd think. Keys get committed to public code repositories, pasted into forums for help, or shown in screenshots and videos. Public code can be searched by anyone, including automated programs looking for keys, so assume a leaked key can be found very quickly.

## How keys leak — and how to avoid it

| How it leaks | Safe habit |
|---|---|
| Written directly in code | Keep keys in environment variables or a secrets manager |
| Code pushed to a public repository | Never commit secret files; use ignore files |
| Pasted into an AI chat for help | Remove or replace secrets with placeholders first |
| Shown in a screenshot or video | Hide or blur them, or use test keys |
| Put in front-end (browser) code | Keep secret keys on the server; anything in the browser is visible to users |

That last one surprises many beginners. **Anything your web page sends to the browser can be seen by anyone who visits it**, using the browser's developer tools. So secret keys must stay on the server side. See [front end vs back end](https://genkid.app/blog/front-end-vs-back-end). When your app goes live, hosts give you a private place for these settings; see [what is web hosting?](https://genkid.app/blog/what-is-web-hosting)

## Publishable keys vs secret keys

Some services give you two kinds of key. A **publishable** (or public) key is designed to sit in browser code: it identifies your project but can't do anything sensitive on its own. A **secret** key can do much more, so it must never leave the server. The payments service Stripe, for example, says only its publishable keys are safe to expose outside your app's back end. If you're not sure which kind you have, treat it as secret.

## Tools that help

Platforms are adding protections. GitHub, for example, offers **secret scanning**, which checks a repository's entire history for leaked credentials, and **push protection**, which can block a push that contains a recognisable secret and explain why, so you can remove it before it reaches your repository.

These tools are helpful, but they don't catch everything. Good habits matter more.

## AI tools and secrets

AI coding tools add a new risk. When you ask an AI to fix code, you might paste in a file that contains a key. And AI tools sometimes write placeholder code with a key directly in it, which beginners then fill in.

Rules for working with AI:

1. **Never paste real keys into prompts.** Replace them with `YOUR_KEY_HERE`.
2. **Reject AI changes that hard-code secrets.** See [how to check code an AI wrote](https://genkid.app/blog/how-to-review-ai-generated-code).
3. **Don't give coding agents access to secrets they don't need.** See [what is an AI coding agent?](https://genkid.app/blog/what-is-an-ai-coding-agent)

## If a key leaks

1. **Tell an adult** straight away, especially if the key is linked to payments.
2. **Revoke or rotate the key** in the service's dashboard. This makes the old key stop working.
3. **Replace it** with a new key, stored safely this time.
4. **Check for misuse** — unexpected usage or charges.

Deleting the key from your code isn't enough: if it was ever pushed to a public repository, it may still be in the [Git history](https://genkid.app/blog/what-is-git) or already copied. OWASP's guidance is that exposed keys should be revoked immediately.

## Learning this safely

Under-13s shouldn't be handling real keys for real services. GenKid teaches secrets with a simulated environment in the sandbox, so learners practise keeping keys out of code without any real accounts. Real setups are guardian-guided at higher levels. GenKid's own AI keys are never exposed to learners. See the [pathway](https://genkid.app/pathway) and our [safety approach](https://genkid.app/safety).

## Frequently asked questions

### Is an API key the same as a password?

They're similar: both are secrets that prove identity. A password is usually for a person logging in; an API key is usually for a program using a service.

### Can I put an API key in my JavaScript file?

Not a secret one. Anything in front-end JavaScript can be seen by users. Secret keys belong on a server, in environment variables or a secrets manager.

### What is an environment variable?

It's a setting stored outside your code, in the environment where the code runs. It lets you use secrets without writing them into files you share.

### What should I do if I accidentally shared a key?

Tell an adult and revoke the key in the service's dashboard right away, then create a new one and store it safely.

### Is it safe to share a publishable key?

Usually, yes: publishable keys are designed to appear in browser code and can't do sensitive things on their own. Check the service's documentation to be sure which kind of key you have, and never assume a key is publishable just because your app works with it in the browser.

## Sources

- [About push protection](https://docs.github.com/en/code-security/concepts/secret-security/about-push-protection) — GitHub Docs
- [Secret scanning](https://docs.github.com/en/code-security/concepts/secret-security/secret-scanning) — GitHub Docs
- [Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) — OWASP Foundation
- [API keys](https://docs.stripe.com/keys) — Stripe Docs
