العربية
Back

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

  1. Open the provider's console and revoke the leaked key immediately.
  2. Create a new key and update the config file on your hosting.
  3. Review the usage log for requests you did not make and report to the provider when needed.
  4. 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

Wording is ours, from the official source. Images and video belong to their owners and are credited with a link to the official page.

#api-key#security#hosting#article

Frequently asked questions

Is a file outside the web root fully safe?

Safer than inside it because the browser cannot serve it, but not fully safe: any compromised code on your account might read it. So restrict the key itself at the provider and monitor usage, the most important protection layer.

What if the key leaks?

Revoke it immediately from the provider's console, create a new one and update your scripts, then review the usage log for activity you did not perform.