Keep your API key on the server, never in the browser

Store an API key in environment settings and explain why it must never end up in client code.

Your API key for an AI model is only secret while it sits on a machine you control. Store it in an environment variable on your server, and point your web page at that server so the page never contacts the model provider directly.

Code that ends up on a user's laptop or phone is on hardware you have no control over, and whoever uses that device can read every line of it. On a first build, the mistake people make most often is leaving the key where users can get at it. It happens by default, and nobody needs any hacking skill to find it.

What happened to Priya's feedback page

Priya works in operations at a logistics firm in Jurong (an invented example, including the timings below). Last month she put together a small web page for her team that summarises customer feedback. To get it running quickly, she pasted her API key straight into the page's JavaScript, and it was working by the first afternoon. A visitor then opened the page, copied the key out of the code and ran their own requests on it, all billed to Priya. Two weeks later an email arrived from her provider about unusual usage on her account.

Before a browser can display a page, it downloads every file the page depends on: the HTML, the styles and the JavaScript. Those files are saved on the user's computer. With the browser's developer tools open, anyone can go through them line by line in the sources tab or the network tab, which is where Priya's key was sitting.

Disguising the key gives no protection. Minified code, a key cut into pieces and an encoded key all fail the same way, because the browser must put the key back together before it can send it, and a curious user can follow those same steps. Mobile apps carry the same risk. An app package is a file stored on the phone, free tools will unpack it and list the text it contains, and you should treat any key built into an app as public.

Storing the key in an environment variable

An environment variable is a named value handed to a program by the operating system or hosting service when the program starts, and it lives outside your code. The code refers to it by name, so none of the files you write contain the key itself. In JavaScript you read it with process.env.MODEL_API_KEY. In Python you read it with os.environ["MODEL_API_KEY"].

On your laptop, these values usually go in a file called .env inside the project folder. It is convenient, and it is also a common source of leaks: people commit .env to git without meaning to and push it to a public repository. .gitignore holds the list of files git should never track, and the setup only protects you if you work in this order:

Add .env to .gitignore before the .env file exists. Create .env in the project folder and put the key in it. Change your code so it reads the key by name, with process.env.MODEL_API_KEY or os.environ["MODEL_API_KEY"]. Run git status and check that git is ignoring the file. If .env shows up as a new file to add, git is not ignoring it, and you need to fix .gitignore before committing anything.

For the live site, leave .env behind when you deploy. Each hosting service has a settings page for environment variables or secrets, and you paste the key into that page. Bigger teams tend to use a secrets manager, a service that stores keys, decides who may read them and keeps a record every time one is read. Your code stays identical on your laptop and on the live site; the two environments differ only in the value the code is handed.

Routing model calls through your server

The safe arrangement has two links: the browser talks to your server, and your server talks to the model provider. The user's input travels from the browser to a route on your server. Your server takes the key from its environment, forwards the request to the provider, receives the reply and returns only the result to the browser.

On Priya's page, the safe version has the browser send the feedback text to a server route ending in /api/summarise. Her server reads the key from its environment, calls the model provider and returns just the summary. The key moves only between her server and the provider, and it appears in none of the files the browser downloads.

Running your own server in the middle also gives you a single place to check what users send, cap how long their input can be, limit how many requests each person makes and log what happened. With no request limits in place, one enthusiastic user could spend your monthly budget in an afternoon. Lesson 6.5, "Ship a small working app", goes into these limits in more detail.

In the course "Automate your work with AI and no-code tools", the automation platform did this go-between job and kept your keys on its own servers. You are now building that layer yourself.

What to do when a key leaks

Settle on your leak response before you need it. Keys get out through a commit, a screenshot posted in a team chat, a support ticket, or a web page with the key written into it, which is how Priya's escaped.

Start in the provider's console: revoke the leaked key and create a replacement. Do this first, before you tidy up the code or try to trace how the leak happened. Removing the commit or the file will not undo the leak. Git keeps history, so an old commit can still be reached, and anything that went to a public repository may already have been copied. Automated scanners search public code for strings shaped like keys, so assume yours has been seen. Revoking is the one step that makes every leaked copy useless, wherever those copies have ended up.

With the old key revoked, add the new key to your environment settings and redeploy. Next, look through the provider's usage page for any requests you did not make, and review the spending limit you set in lesson 1.1. A spending limit can turn a leak into an annoying email rather than a large bill, and Priya's limit is the reason her leak ended with her provider's email and no large bill.

In the activity below you will move your key into an environment variable and prove to yourself that it is not sitting in any file you might share.

Set your key as an environment variable on your computer, then check that it does not appear in any file you would share or commit.

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).