Explore skills

Blog / Security

Keeping API Keys Out of Skills

A skill is a folder that gets copied, synced and shared. Where API keys leak from it, how to read keys from the environment instead, and how to check a folder.

Keeping API Keys Out of Skills

A skill is a folder that gets copied, synced, committed and shared. Anything you paste into it travels with it, so the key should live somewhere the folder does not.

Table of contents

The short answer

Do not write a key into SKILL.md, a script, a reference file or an example. Write the skill so it reads the key from an environment variable (or from a secrets manager) and tells the user how to set it. The folder then contains only the name of the variable, which is safe to copy, commit, sync and publish.

This matters more for skills than for most code, for a plain reason: a skill is designed to move. You save it in ~/.claude/skills, commit it to a project's .claude/skills, upload it to a catalog, or send a zip to a colleague. The Claude Code docs describe the project folder with "Commit it so your team gets it too" (Claude Code skills docs, checked 2026-11-23). If a key is inside, every one of those moves copies it.

I build AI agents full-time, and I have pasted a key into a skill while testing and nearly committed it. This article covers where that happens, the pattern I use instead, and how to check a folder. It draws on Anthropic's documentation, the Claude Code and MCP documentation and Ragspaces catalog pages, all read on 2026-11-23. The scanning tools named here are the ones those catalog pages name.

Why skills leak keys

Three properties of skills make leaks easy.

A skill is plain text that gets read. The body of SKILL.md enters the conversation. The Claude Code docs say "the rendered SKILL.md content enters the conversation as a single message and stays there across later turns." A key written in the body is in the conversation, and in any transcript, log or shared session that includes it. Scripts are different: Anthropic says scripts run and "only the output" enters context, "the script code itself never enters context." But a script file is still a file in the folder, and it is copied with everything else.

A skill is shared by design. Folders move between computers and people. Ragspaces' sync, for instance, uploads a skill when the agent saves it and pulls changes to other computers "within a minute." Git moves it to every clone. A zip moves it to whoever gets the zip.

A skill is written while you are trying things. You paste a key to see whether the script works, and you intend to remove it later. The folder is saved the moment you finish editing, whether you remembered or not.

Anthropic's security section is written for people installing skills, but its list is a useful checklist for authors too: "Data exposure: Skills with access to sensitive data could be designed to leak information to external systems," and "Skills that fetch data from external URLs pose particular risk" (Agent Skills overview). A key in your folder is exactly what such a skill would take.

Where keys end up in a skill

Check these places, in roughly the order I find keys in them:

  1. A script's constants. API_KEY = "sk-..." at the top of a helper under scripts/.
  2. Example commands in SKILL.md. A curl example with a real Authorization header pasted from a terminal.
  3. Reference files. A copied API doc page that included the author's personal sample key.
  4. Config files. A .env or config.json that the script reads, left inside the folder.
  5. Test fixtures. Saved responses or request logs that contain headers.
  6. Frontmatter and descriptions. Rare, but I have seen a key pasted into a description while debugging.
  7. MCP configuration. A key placed in a .mcp.json that is committed with a project. The Claude Code MCP docs describe a project scope that writes .mcp.json, which is meant to be shared (Claude Code MCP docs).
  8. Git history. A key you deleted is still in earlier commits. One Ragspaces catalog page, secrets-in-git-history by @ivanwilson, is described as mining "GitHub, GitLab and git history for ... leaked credentials" with tools such as trufflehog and gitleaks, which is a sign that history is where people look.

The pattern: read from the environment

The skill names a variable. The user sets it. The script reads it. In SKILL.md:

---
name: geocode-address
description: Convert a street address to latitude and longitude using the Acme geocoding API. Use when the user gives an address and asks for coordinates.
---

## Setup

This skill needs an Acme API key in the environment variable `ACME_API_KEY`.
If the variable is not set, stop and tell the user how to set it. Do not ask
the user to paste the key into the chat.

## Steps

1. Run `python scripts/geocode.py "<address>"`.
2. Report latitude and longitude, and the match confidence the script returns.

And the script:

import os
import sys
import urllib.request
import urllib.parse

key = os.environ.get("ACME_API_KEY")
if not key:
    print("ACME_API_KEY is not set. Create a key in your Acme account, then set it "
          "as an environment variable and run this again.")
    sys.exit(2)

query = urllib.parse.urlencode({"q": sys.argv[1]})
req = urllib.request.Request(
    f"https://api.acme.example/v1/geocode?{query}",
    headers={"Authorization": f"Bearer {key}"},
)
with urllib.request.urlopen(req) as resp:
    print(resp.read().decode())

Three details matter. The script fails with a clear message when the variable is missing, which matches Anthropic's advice that scripts "handle error conditions rather than deferring to Claude" (best practices). The key never appears in the output, so it never enters the conversation. And the instruction tells Claude not to ask for the key in chat, because a key typed into a chat is in the transcript.

The domain acme.example and the variable name are placeholders: swap in your service's URL and variable before you use the script.

Writing the setup steps

A skill that needs a key must also help the user get one. Two catalog skills do exactly that, and I read their pages on 2026-11-23:

Neither page says how the skill stores the key, so check that in the SKILL.md before you install. The pattern of a separate setup skill is the point: when the key is missing, a short, specific set of steps beats a stack trace.

A good setup section says:

  • the exact variable name;
  • where the user creates the key, in one sentence;
  • how to set it for the shell they use, and that it should be set outside the skill folder;
  • what a missing or wrong key looks like, so Claude can recognize it;
  • what the skill does when the variable is absent (stops and explains).

Ragspaces shows "the setup steps to run it yourself" on each skill page. If your skill needs a key, those steps are the place a user will look, so state the variable name there as well as in SKILL.md.

Scripts that call an API

A few habits for scripts that hold or use credentials:

  • Read, do not echo. Never print the key, never include it in an error message, never write it to a log. The output is what enters context.
  • Keep keys out of command lines. Arguments appear in process listings and shell history. An environment variable or a file with restricted permissions is better than --key abc.
  • Do not store a key next to the script. A .env file inside the skill folder is part of the skill. If you must use a file, keep it outside the folder, for example in a config directory in the user's home, and read the path from a variable. If the skill runs on a server where Claude Code works unattended, the keys in that machine's environment need the same care, and this guide to locking down a Claude Code server lists five settings for it.
  • Use the narrowest key you can. A read-only key limited to one project does less damage than an account-wide one.
  • Prefer a secrets manager for team use. One catalog skill, creating-secrets-using-best-practices by @tararomano46, is described as creating and managing secrets in AWS Secrets Manager with dedicated KMS keys and rotation. Another, env-secrets-manager by @alicelindqvist87, has a one-line description ("A skill for env secrets manager.") and I would read its README before trusting it. I read the pages only.

The best-practices guide also warns against magic numbers; the same spirit applies to secrets: a constant in a script that nobody can explain, or a credential nobody remembers pasting, is a liability.

One more thing about scripts: Claude Code's docs mention ! shell injection, where "the !<command> syntax runs shell commands before the skill content is sent to Claude," and a disableSkillShellExecution setting that turns it off. If a command in a skill's body prints an environment variable, its output becomes part of the text Claude receives. Do not print secrets in such commands.

MCP servers and keys

Skills often work alongside MCP tools. The Claude Code MCP docs describe adding a server with claude mcp add --transport http <name> <url> and three scopes: local, project (.mcp.json) and user. The practical rule: the more shared the scope, the less a key should appear in it. A project-scoped .mcp.json is committed to the repository, so a key typed into it is shared with everyone who clones.

The MCP specification points the same way from the server side. Servers "MUST validate all tool inputs, implement proper access controls, rate limit tool invocations, sanitize tool outputs" (MCP specification, tools). If a skill is exposed as a tool, the server should hold the key and the caller should send only arguments. That is the design behind hosted, run-only skills, and I describe it in the post on turning a skill into an MCP tool.

Also, the spec says clients "MUST consider tool annotations to be untrusted unless they come from trusted servers," and clients should "show tool inputs to the user before calling the server, to avoid malicious or accidental data exfiltration." Do not rely on a tool's description to hide a secret. Descriptions are shown to models and, often, to users.

Checking a folder before you share it

Before you commit, zip or publish a skill, do these checks. None needs special software.

  1. Search the folder for key shapes. Look for the prefixes your providers use, and for the words key, token, secret, password, bearer and authorization. A plain text search across the skill folder is enough for a first pass.
  2. List the hidden files. .env, .env.local, credentials.json and editor backup files are easy to miss because they do not show in a normal listing.
  3. Read every script's top. Constants and defaults live there.
  4. Read examples and fixtures. Saved headers are the usual culprit.
  5. Run a scanner on the repository. A catalog page lists trufflehog and gitleaks as tools for history. ghost-scan-secrets by @ninaberg is described as scanning "codebase for leaked API keys, tokens, passwords, and sensitive data" and generating findings with severity.
  6. Check git history, not only the working tree. A deleted key is not a removed key.
  7. Add a guard. Put .env and credentials files in .gitignore, and consider a pre-commit scan so a mistake is caught before it leaves your machine.
  8. Re-read what you are about to share, once, as a stranger. Anthropic's advice to users is to "review all files bundled in the Skill: SKILL.md, scripts, images, and other resources." Do that review of your own skill.

A related point: file metadata can hold surprising things. A catalog skill, secrets-in-file-metadata by @fatima_loop, covers extracting embedded EXIF, XMP and IPTC fields and Office and PDF metadata. If your skill ships images or documents, strip them first.

If a key already leaked

Assume it was copied the moment it left your machine.

  1. Revoke or rotate the key at the provider first. Deleting it from the file does not stop anyone who already has it.
  2. Issue a new key and put it in the environment, not the folder.
  3. Check usage logs at the provider for calls you did not make, for the period the key was exposed.
  4. Clean the folder and the history. Remove the key from current files, and from git history if it was committed. History rewriting is a separate task with its own risks; do the rotation first.
  5. Tell the people who might hold a copy. If the skill was shared, say which version was affected.
  6. Update the skill to read from the environment so the same mistake cannot repeat.

For a skill that synced somewhere you do not control, ask that service how it handles removal. I can only document one: Ragspaces says deleted skills go to ~/.claude/ragspaces-trash and are never erased, which means a deleted skill's local copy remains in that folder until you clear it yourself. So after rotating the key, check the trash folder too.

What Ragspaces does and does not do

Ragspaces is an open catalog of 5,299 agent skills for Claude Code and Codex (home page count on 2026-11-23). Its sync hook uploads a skill when your agent saves it, and "API keys in files are blanked out before upload." That is a safety net for the skill that left a key in a script by accident. It is not a reason to paste keys into skills.

Limits I would assume: I cannot document which key formats are detected, so a key in an unusual format, or in a place the blanking does not cover, may not be caught, and a blanked key means a skill that then fails for the next user. Your local files are unchanged, and the key is still in your git history if you committed the folder. It also does nothing for keys in a repository you push elsewhere.

Visibility matters here too. Each skill can be private (only you), visible to followers, or public in the catalog. With protection on, others call the skill as a tool and never see its prompts or scripts, so a key held by the skill's runtime is not exposed to callers. Keep a skill private while you check it, as I describe in the post on testing a skill before publishing (how it works).

Catalog pages (as of 2026-11-23) that show how authors handle setup and descriptions:

The notes above come from each skill's page description.

Common mistakes

Pasting a key to test and forgetting it. Test with the environment variable from the start.

Asking the user to paste a key into the chat. It lands in the transcript. Ask them to set the variable.

Printing the key in an error. Output enters context.

Putting a key in a committed .mcp.json. The project scope is shared with the repository.

Deleting the key from the file and calling it done. It is still in history, and possibly in someone's copy.

Relying on automatic blanking. It is a net, with limits I cannot document.

Using one account-wide key for everything. Use the narrowest key, so that a leak costs less.

Frequently asked questions

Should I ever put an API key in SKILL.md?

No. The body of SKILL.md enters the conversation and the folder gets copied, committed and shared. Name an environment variable in the body, tell Claude to stop if it is missing, and tell the user how to set it.

How does a skill read a key?

Through an environment variable read by a script, for example os.environ.get("ACME_API_KEY") in Python. The script should fail with a clear message when the variable is missing and should never print the key, because script output enters context.

Does Ragspaces remove keys from my skills?

It says API keys in files are blanked out before upload. Your local files are not changed, and I cannot document which formats are detected, so do not depend on it. Keep keys out of the folder in the first place.

What if I committed a key to a repository?

Revoke or rotate it at the provider first, then issue a new key, check usage logs, and clean the files and history. Deleting the line from the current file is not enough, because earlier commits still contain it.

Is a project-scoped MCP config safe for keys?

Not for plain keys. The Claude Code docs describe the project scope as a .mcp.json file meant to be shared with the repository. Keep keys in a narrower scope or in the environment.

Can a hosted skill hide its key from callers?

Yes, if it runs on a server. A caller sends arguments and gets results while the server holds the key. On Ragspaces, protection means others call the skill as a tool and never see its prompts or scripts.

Key takeaways

  • Skills are copied, synced, committed and shared, so a key inside the folder travels with it.
  • Name an environment variable in SKILL.md, read it in the script, and stop with a clear message if it is missing.
  • Never print the key and never ask for it in chat, because output and chat both enter context.
  • Search the folder, the hidden files and the git history before you share.
  • If a key leaked, rotate it first and clean up second.

The bottom line

Write the skill so that it works with the key and contains nothing secret. The folder can then be shared freely, and the key stays with the person who owns it. Automatic blanking on upload is a safety net, not a plan.

If you want to read how published skills describe their setup before you write yours, browse the Ragspaces catalog. Skill pages show the description, README and reviews with no account.

Related articles

About this research

Dmitry Makarenko co-founded Zipy as VP R&D, leading 50+ engineers across five countries, and now builds AI agents full-time. (LinkedIn)

This article is based on Anthropic's Agent Skills overview and best practices, the Claude Code skills and MCP documentation, the Model Context Protocol specification, and Ragspaces catalog pages and product facts, all read on 2026-11-23. Content reviewed and updated: 2026-11-26

Keep reading