Engineering basics

What is an API key? Why beginners should never expose it

An API key is like a service pass for your app. Learn why it should not be placed in frontend code or committed to a public GitHub repository.

An API key is a service pass for your application

When you use OpenAI API or another AI service, you will often receive a long string called an API key. That key tells the service which account is making the request and whether that request is allowed.

You can think of it as a service pass. Your code uses the pass to call the AI service. The service uses the key to check permissions and track usage.

This matters because AI APIs often connect to billing or usage limits. If someone else gets your key, they may be able to send requests through your account.

Beginners often make this mistake when building their first AI demo. The tool works, so they publish the code. But if the key is inside the frontend or inside a public repository, the project is not safe.

API security

The API key should stay on the server side

The frontend collects input. The backend keeps the key private and calls the AI service.

Do not put an API key in frontend code

Frontend code runs in the user browser. If you put a key there, the browser can receive it. A normal user may not look for it, but someone with basic technical knowledge can inspect bundled files or network requests.

The safer pattern is simple: the frontend should send user input to your backend. The backend should call the AI service with the API key stored in environment variables. Then the backend returns only the final result to the frontend.

This extra step is not unnecessary complexity. It separates the visible part of your app from the private credentials that protect your account.

Do not commit an API key to GitHub

A second common mistake is committing an API key to GitHub. Even if you remove it later, the key may still exist in Git history. If a key is exposed, the safer response is usually to revoke it and create a new one.

For beginner projects, remember two habits. Do not commit `.env.local` files. Do not hardcode keys directly into normal source files.

This is a basic engineering habit. If you want your AI project to look credible, it is not enough that the demo works. It also needs to avoid exposing secrets.

Risk path

A leaked key can become an account problem

The main risk is not the string itself. The risk is the permission and usage connected to it.

Key takeaways

  • An API key is a credential used by your app to call a service.
  • Do not place API keys in frontend code.
  • Do not commit API keys or `.env.local` files to public repositories.
  • Use backend environment variables to keep keys private.
  • Protecting API keys is part of building AI tools that are safe to share.