AI
API keys on shared hosting: protection step by step
How to store API keys safely on shared hosting when connecting AI tools to your site: where the key lives, how to restrict it, and what to do immediately if it leaks.
Last reviewed:
Why the key is the risk point
An API key is your identity to the provider: every request carrying it bills to you and your balance. On shared hosting your code runs in an environment whose core you do not control, so any key leak means someone else consumes your quota and may reach your account data depending on the key's permissions.
Safe-storage rules
- Never put the key inside published code files or in a Git repository, even a private one.
- Store it in a config file outside the public web root (outside public_html), or in an environment variable if your plan supports it.
- Restrict the key in the provider's console where possible: allowed services or IP addresses, as Google's API key guidance recommends.
- Issue a separate key per project or script so you can trace unusual consumption and revoke one key without the rest.
- Review the provider's usage log regularly and set spending alerts where available.
Rotation and deletion
Provider guidance recommends deleting unused keys immediately and rotating active keys regularly. Rotation in practice: create a new key, update the config file on hosting, confirm the script works, then revoke the old one.
If the key leaks
- Open the provider's console and revoke the leaked key immediately.
- Create a new key and update the config file on your hosting.
- Review the usage log for requests you did not make and report to the provider when needed.
- Investigate how it leaked: a file inside the web root, a published repository, a script from an unknown source? Fix the cause before resuming.
Sources
The rules are summarized in our own wording from OpenAI's API key safety guidance and Google's API key guidance, both read on 2026-12-02.
Latest updates
Sources were read on 2026-12-02. Restriction options differ between providers and evolve, so check your provider's console for the options available for your keys specifically.
Related pages
Sources
- OpenAI Help: Best practices for API key safety · read on
- Google Support: API key best practices · read on
Wording is ours, from the official source. Images and video belong to their owners and are credited with a link to the official page.