What Is an API Key? How to Use One Safely
By Anuj Ror · · 3 min read
If you build anything with AI, sooner or later a service will ask you for an API key. Most beginners copy it, paste it into the code and move on. That is exactly how keys get leaked. I'm Anuj Ror, an AI developer from Haryana, and my projects use keys from several services, so here is the simple version of what a key is and how to keep it safe.
What is an API key?
An API key is a long secret text that tells a service who you are. When your app asks a service for something, like a reply from a language model or a voice from a text to speech tool, it sends the key along. The service then knows which account to use and which account to charge.
Think of it as a password for your app.
Why it must stay secret
Anyone who has your key can use the service as you. They can use up your free limit, run up your bill, or get your account blocked. Even on a free plan, a leaked key can burn through your limit in a few hours.
Where keys usually leak
- In code that is uploaded to GitHub.
- In screenshots and screen recordings.
- In chat messages and AI chat prompts.
- In the front-end code of a website, which anyone can open in their browser.
How to keep a key safe
- Put the key in a file called .env on your computer, and add that file to .gitignore so Git never uploads it.
- When you deploy, add the key in the settings of your host, such as Vercel or Render, as an environment variable.
- If you use GitHub Actions, store keys in repository Secrets, not in the workflow file.
- Use a separate key for each project, so you can cancel one without breaking the others.
- Set a spending or usage limit on the service if it lets you.
A trap for Vite and React users
In a Vite project, any variable whose name starts with VITE_ is included in the code that goes to the browser. That means a secret key stored with that name is public. Keep secret keys on the server side, in a small backend or serverless function, and let your website call that function instead of calling the service directly.
What to do if a key leaks
- Delete or revoke the key in the service's dashboard right away.
- Create a new key and update it everywhere you use it.
- Check the usage page for anything you did not do.
- Remember that removing the key from your code is not enough. Once it has been public, treat it as stolen.
A leaked key cannot be un-leaked. You can only replace it.
A short checklist
- The key is in .env and .env is in .gitignore.
- There is no key in my front-end code.
- I never share a key in screenshots or chats.
- Each project has its own key.
- I know how to revoke a key quickly.
What to remember
- An API key is a password for your app.
- Keep it on your computer and your server, never in public code.
- If it leaks, replace it at once.
About the author
Anuj Ror is an AI developer from Kaithal, Haryana, India, who builds voice AI agents and smart AI tools.
Topics: API key, security, .env, beginners, AI tools, Anuj Ror