All notesGuide

How to keep API keys safe in an AI-built app

Stop AI-written apps from leaking API keys: where to store secrets, what never to paste into a chat and how to rotate a key that leaked.

Debraj

Assistants love to paste a key straight into the code “to make it work”. That key then lives in your history, your repository and your chat log.

The rules

  • Never paste a key into a chat or a prompt.
  • Never commit a key or a .env file.
  • Read keys from environment variables.
  • Give every key the smallest permission it needs.

Where keys should live

In a secrets store the app can read and people cannot. On Cohostly your assistant requests a secret by name and you type the value on a private page, so it never appears in the conversation.

If a key already leaked

  • Revoke it at the provider straight away.
  • Create a new one and store it as a secret.
  • Search the repository history for the old value.
  • Check the provider’s usage for activity you do not recognise.

Better than keys

For Google, Slack and similar services, use a connection with limited scope instead of a key. For model calls, use a gateway so the app never holds the provider key.

Questions

Is it safe to give an AI my API key?

Not in a prompt. Let it request a secret by name and enter the value yourself.

How do I rotate a key?

Make a new key at the provider, replace the secret and redeploy; the old key can then be revoked.

Are environment variables secure?

They keep keys out of the code. The store holding them must also be locked down.

Keep reading

See what people host on Cohostly