AITraining2U

Programs

Resources

Case Studies

Quick Links

Enquire Now
Security

Leaked API keys in AI apps: find them, fix them, stop them

What to do in the first hour, and a 15-minute check for apps built with AI.

By AITraining2U Editorial Team 2026-12-03 6 min read
Jan14–15

Next public cohort

14–15 Jan 2027 · Thu–Fri

Worq TTDI

Limited seats · see all dates

A padlock on a computer keyboard

If a secret key has leaked, replace it first. Cleaning up the code comes later. Anyone who copied the key can still use it after you delete the file. Then check what was used, set a spending limit and fix the cause.

Do not paste the leaked key into a chat to ask if it is safe. That gives it to one more place.

Key leaked? Do this now

The first hour

  1. 1
    Find the owner
    Which service is it, and which app uses the key?
  2. 2
    Under attack? Turn it off
    Do this even if the app stops.
  3. 3
    Otherwise, swap it safely
    Create a new key, put it in the app, test, then turn the old one off.
  4. 4
    Read the usage log
    Look for strange times, places, amounts and downloads.
  5. 5
    Lower the spending limit
    So the bill cannot grow overnight.
  6. 6
    Check for personal data
    If any was reachable, call your data protection officer.
  7. 7
    Fix the cause
    Move the key to the server and scan every project.

What is an API key?

It is a password that lets one program use another. If your app uses Claude, the Claude key proves it is your account. Anyone with the key may be able to spend your money or read your data.

Where should the key live?
Safe
  • On your server
  • In a secret manager
  • One key per app
  • With a spending limit
Unsafe
  • In web page code
  • In VITE_ or NEXT_PUBLIC_
  • In a chat or screenshot
  • In a public project

How common is it?

GitGuardian, a company that sells secret scanning, counted 28.65 million new secrets in public code on GitHub in 2025. That is 34% more than the year before. Secrets for AI services rose 81%. Of the keys that worked in 2022, over 64% still worked in January 2026.

Which keys may be public?

Key or settingCan visitors see it?Safe?
Claude, OpenAI or Gemini API keyMust notKeep it on your server
Supabase publishable or anon keyYes, it is made for thatOnly if Row Level Security is on and correct for every table
Supabase secret or service_role keyMust notIt skips the security rules
Anything starting with VITE_Yes, it can go into the web pageNever put a secret there
Anything starting with NEXT_PUBLIC_Yes, it can go into the web pageNever put a secret there
Database passwordMust notKeep it on your server

Row Level Security is a database rule that decides which rows each person can see.

Where keys leak

  • In web page code, because AI put the key in the front end to make a demo work.
  • In a .env file uploaded to GitHub.
  • In a screenshot, slide or screen recording.
  • In a chat, support ticket or WhatsApp group.
  • In logs that print the whole request.
  • In a workflow file shared as a template.

Check your app in 15 minutes

  1. Open your live site. Press F12, then choose Sources or Network.
  2. Search for sk-, AIza, service_role and secret. These are only clues; finding nothing does not prove you are safe.
  3. Check that .env is listed in .gitignore.
  4. Look at GitHub’s Security tab for alerts.
  5. Log in as customer A. Try to open customer B’s record.
  6. Check who can see your keys in n8n, Vercel, Supabase and shared folders.

Only test apps you own or have permission to test.

Copy this prompt

Look for secrets in my projectA coding assistant your company approves
Check this project for exposed secrets. Do not print any secret; show only the file, line and type.

Check: front-end code, VITE_ and NEXT_PUBLIC_ settings, .env files, .gitignore, logs, test files, workflow exports and git history if you can see it.
For each: what it opens, who can see it, how urgent, and how to replace it. Then say what you could not check. Do not change files or call any service.
Build it safely from the startA coding assistant your company approves
Build this feature so that no secret reaches the browser. Put all AI and database-admin calls on the server. Check the user's login and permission on the server for every request. Add a rate limit, a daily spending cap and logs that never print keys or personal data. List every environment setting, say if it is public or secret, and show me how to test that a visitor cannot see it.

Set a limit on every AI account

ProviderWhat you can setWatch out
Claude APIYour own limits for the organisation and each workspaceStandard monthly caps start at US$500, so set a lower one
OpenAI APIHard limits for the organisation, project or keyAn alert only warns you. A hard limit stops use, though it may go slightly over
Gemini APIProject caps, still experimentalThey can be about 10 minutes late

GitHub can help, but not with everything

Secret scanning is free and automatic for public projects. GitHub also blocks many known secrets when you push to a public project, but people can bypass the warning. Private company projects need a paid add-on. Anthropic says it turns off Claude keys that GitHub finds in public projects. Do not wait for that.

After replacing a key, you usually do not need to rewrite the project history. Copies may still exist, which is why replacing it comes first.

Did personal data leak too?

A leaked key is not always a personal data breach. It may be one if somebody could reach customer or staff records. Malaysia’s guideline gives 72 hours to tell the Commissioner about a breach that meets its rules. If it is likely to cause significant harm, the people affected must be told within 7 days after that. The guideline treats more than 1,000 people as significant scale. Parts of it count the clock differently, so get advice at once. See our PDPA and AI guide.

Seven habits that prevent it

  • One key for each app, and one for testing.
  • Give each key the least access it needs.
  • Store keys in a secret manager or the host’s secret settings.
  • Turn on secret scanning before the first upload.
  • Set spending caps on day one.
  • Remove old keys every three months.
  • Write down who replaces which key, and practise once.

Next, read how to review AI-written code and how to defend against prompt injection.

HRDC-claimable · 2 days · hands-on
Claude and multi-agent course

Build with Claude and keep access under control.

Next public class: 14–15 January 2027 at Worq TTDI · seats available

Making apps with AI? See our AI vibe coding course.

HRD Corp claimable. AITraining2U is an HRD Corp registered training provider. Our public and in-house courses are claimable under the HRD Corp Claimable Courses (SBL-KHAS) scheme, and we prepare the grant paperwork with you. See HRDC-claimable AI training: courses, costs and how to claim.

Frequently Asked Questions

Replace it and turn the old one off. If it is being abused, turn it off at once. Then read the usage log, lower the limit and fix where it leaked.

No. Copies may exist in history, forks and downloads. Replace the key first.

It is made to be public, but only if Row Level Security is on and correct for every table. The service_role or secret key must never be public.

No. VITE_ values can go into the code that visitors download. Keep the key on your server.

It can be if somebody could reach personal data. Ask your data protection officer at once, because the 72-hour clock may apply.

Build safely with AI

Learn to use Claude with proper access and checks.