Contents
API keys are almost everywhere: AI models, payment gateways, maps, email-sending services. The most common mistake isn’t how they’re used but where they’re stored.
How an API key works
You create the key in the provider’s dashboard, then your app sends it with each request to the API, usually in a header like this:
curl https://api.example.com/v1/items \
-H "Authorization: Bearer $API_KEY"
The provider checks the key, applies its permissions and rate limits, and records usage on your account.
Where to keep the key
| Location | Safe? | Why |
|---|---|---|
| Hard-coded in source | No | Visible to anyone who sees the code or repo |
| Browser JavaScript | No | Any visitor can read it in developer tools |
.env inside public_html |
No | May be downloadable from the browser |
.env outside the web root and in .gitignore |
Yes | Not reachable by visitors, not pushed to Git |
| Environment variables | Yes | The recommended approach |
In code, read the key from the environment instead of writing it in:
import os
api_key = os.environ["OPENAI_API_KEY"]
Good practices
- A separate key per project and per environment (development and production), so you can revoke one without breaking the rest.
- Spending limits and the narrowest permissions the provider allows.
- Rotate keys regularly, and immediately when a team member leaves.
- Turn on secret scanning in GitHub so you’re warned if a key is pushed by mistake.
- Don’t paste keys into chat or email, even to colleagues.
Publishable vs secret keys
Some services, such as payment gateways and maps, give you two keys: a publishable key that may appear in the browser with limited permissions, and a secret key that must stay on the server. Mixing them up is a common mistake, so check each key’s type in the service’s documentation.
On ArabyHost
On AI/Developer plans you run Node.js and Python apps through the CloudLinux selectors and add environment variables on the app’s setup page in cPanel instead of writing them into code. Every account is isolated with CageFS, so other accounts can’t read your files; even so, keep .env outside public_html. See the AI app deployment guide and the AI plans.
FAQ
What should I do if my API key leaks?
Revoke it immediately in the provider's dashboard, create a new one and put it in environment variables. Check usage and billing, and remember that deleting the key from a file isn't enough if it was committed to Git; it stays in the repository history.
Can I put an API key in browser JavaScript?
Not a secret key. Anything sent to the browser can be read by any visitor. Have the front end call your own server, and let the server call the external service with the key.
How is an API key different from a password?
A password is used by a person to sign in; an API key is used by a program. You can create several keys per account, scope each one, and revoke one without changing your account password.