Claude API key security: how to keep your Anthropic key safe

Keep your Claude API key on the server: store it as a secret or environment variable, call Claude from server-side code, and never put it in browser code or a public repo.

Gary 路 Published 路 Updated 路 6 min read

Diagram of where a Claude API key belongs: the browser calls your server route, and only the server's secret store holds the key.
On this page
  1. What a Claude API key is (and what it isn't)
  2. Why the key must never reach the browser
  3. Example: before and after
  4. Anthropic's recommended key practices
  5. What to do if your Claude API key leaks
  6. Claude API keys and AI coding agents
  7. Quick checklist
  8. FAQ

Keep your Claude API key on the server only: store it as an encrypted secret or environment variable, call the Claude API from server-side code, and never put the key in browser code, a public repo or a chat with a third party. If it leaks, delete it in the Claude Console and create a new one.

Anthropic's own help center puts it plainly: "Much like a credit card number, if someone obtains and uses your API key, they incur charges on your behalf."

By Gary, Greenlight 路 Last checked: Oct 19, 2026 路 Drafted with AI tools and reviewed before publishing.

What a Claude API key is (and what it isn't)

A Claude API key (Anthropic's docs show it as a static secret starting with sk-ant-api...) lets any program that holds it call the Claude API and bill your Console account. It's different from a Claude chat subscription: the subscription is for the Claude apps; the key is for code you write or tools you connect.

Why the key must never reach the browser

Anything shipped to the browser, including your JavaScript bundle, can be read by every visitor. A common mistake in AI-built apps is calling the Claude API straight from the frontend with the key in a VITE_ or NEXT_PUBLIC_ variable. Those prefixes exist to expose values to the browser (Vite (external), Next.js (external)), so a secret placed there is public.

The safe pattern:

  1. Store the key as a secret on your host or backend (for example, an environment variable or your builder's Secrets panel).
  2. Call Claude from a server-side function or API route.
  3. Have your frontend call your function, never Anthropic directly.

Example: before and after

Before (key in the browser): a React component calls the Claude API with the key read from a VITE_ variable. Anyone opening DevTools can copy it.

After (key on the server): the component posts the user's message to /api/chat. That route reads ANTHROPIC_API_KEY from server environment variables, calls Claude and returns only the answer.

From Anthropic's API key best practices (external) article:

  • Don't share it, including in forums, emails or support tickets.
  • Be careful with third-party tools. Giving a tool your key gives that tool access to your Console account.
  • Use environment variables. If you use a local .env file, add it to .gitignore. In the cloud, prefer encrypted secret storage.
  • Rotate keys on a schedule (Anthropic gives every 90 days as an example).
  • Use separate keys for development, testing and production, so you can disable just one.
  • Scan your repos for secrets with your Git host's secret scanning or tools like Gitleaks.
  • Watch usage and limits. Review usage in the Console, and set spend or auto-reload limits as a safeguard.

Anthropic also says it partners with GitHub's secret scanning program: if a Claude API key shows up in a public GitHub repo, GitHub notifies Anthropic, which automatically deactivates the key and emails the owner. That's a backstop, not a plan; a key can also leak through a website bundle, a log or a screenshot.

What to do if your Claude API key leaks

  1. Delete the key in the Claude Console (the API keys page, then the three-dot menu next to the key, then "Delete API key").
  2. Create a new key and store it as a server-side secret.
  3. Update every place that used the old key and redeploy.
  4. Remove the old key from code and history. Deleting a file doesn't remove it from past Git commits.
  5. Check usage in the Console for activity you don't recognize.

The same order works for other secrets; see our step-by-step guide to a leaked Supabase service role key. To see what a leaked key could cost, see our Claude API pricing breakdown.

Claude API keys and AI coding agents

If an AI agent like Claude Code works in your project, it can read files on your machine, including .env. Deny rules for secret files help; see Claude Code permissions and sandboxing.

Quick checklist

  • The key lives only in server-side secrets or environment variables.
  • No VITE_ or NEXT_PUBLIC_ variable holds a secret.
  • .env is in .gitignore and isn't served by your live site.
  • Separate keys for dev and production.
  • Spend limits or auto-reload limits set.
  • Source maps are off in production.
  • HTTPS and security headers are on.

FAQ

Is it safe to put a Claude API key in frontend code? No. Anything in the browser can be read by visitors. Call Claude from a server-side function instead.

What does a Claude API key look like? Anthropic's authentication docs show API keys starting with sk-ant-api. Treat the whole string as a password.

How often should I rotate my Anthropic API key? Anthropic suggests a regular schedule, giving every 90 days as an example, and immediately if you think it leaked.

What happens if I push my Claude API key to GitHub? Per Anthropic, GitHub's secret scanning notifies Anthropic, which deactivates the key and emails you. Create a new key and remove the old one from your history.

Is a Claude API key the same as a Claude subscription? No. A subscription is for the Claude apps; an API key bills API usage in your Console account.

What does Greenlight's free scan check? From the outside only: HTTPS, security headers, exposed .env and .git files, public source maps, and common secret key patterns in your homepage's scripts, including Anthropic and OpenAI API key formats. If it finds a Supabase project and its public key there, it counts which tables return rows to that public key (it reads no row data). It doesn't log into your builder or Supabase, read your code repository or RLS policies, or test logins and app logic, and "nothing found" isn't a guarantee.

Sources

On this site

Related posts

Greenlight is independent and not affiliated with or endorsed by Anthropic, Claude, Claude Code, GitHub, Gitleaks, React, Vite, Next.js or Supabase. Product names are used only to identify the tools this guide is about. This page is general information, not professional security advice, legal advice, a security audit or a guarantee. Check your own setup against the vendor's current docs.