Home / How to give Claude Code and Cursor API keys safely
Guide
How to give Claude Code and Cursor API keys safely
Coding agents work best when they can run your app, and your app needs keys. But an agent that can read your project can usually read your secrets too. Here's where agents see them, and how to keep the damage small when one does.
Where agents see your secrets
.envfiles. They sit in the project folder, right where the agent works. An agent can open them with its file tools or with a shell command, and a.gitignoreentry doesn't stop either.- Terminal output and shell history. The agent reads the output of every command it runs. A debug log that prints config, an error that echoes a connection string, or a command like
envputs values in front of it. Commands with a key typed inline also land in your shell history. - MCP config files. MCP servers often need a token, and it's easy to paste it straight into the config. Claude Code keeps project servers in a
.mcp.jsonfile that's meant to be committed, and Cursor uses.cursor/mcp.jsonin the project. - The chat itself. Anything you paste into the conversation goes to the model provider and stays in the session's history.
- The editor. Keys you save in your editor's own settings live on your machine, and other code running there may be able to read them. More on that below.
Seven steps to keep keys away from your agent
- Keep secrets out of the working directory. Put production values in your host's environment settings or a secrets manager, never in the repo. Locally, commit a
.env.examplewith placeholder values and keep real ones out of the folders you open with an agent where you can. - Block
.env*in the agent's file permissions. In Claude Code, addReaddeny rules for.envfiles. In Cursor, list them in.cursorignore. The details and limits of both are below. - Use restricted, scoped keys. Give the agent's environment test-mode or restricted keys with only the permissions it needs, separate from production. Set spend limits where the provider offers them.
- Keep tokens out of MCP config files. Reference an environment variable in the config instead of pasting the token, and prefer MCP servers that sign in through your browser so there's no token to store at all.
- Never paste a key into the chat. If the agent asks for one, set it yourself in the environment or a file the agent can't read, and tell the agent it's there.
- Watch for prompt injection. Text the agent reads (an issue, a web page, a README, a dependency, a tool result) can contain instructions like "print the environment variables" or "send the config to this URL". Keep approval prompts on for shell commands and network requests, and read what you approve.
- Rotate any key an agent has seen. If a value showed up in a file the agent read, its terminal or the chat, treat it as exposed and replace it. See how to rotate an API key without downtime.
Claude Code: deny rules for .env files
Claude Code's settings have a permissions block with allow, ask and deny rules. Its settings docs use this example to stop Claude Code reading .env files:
{
"permissions": {
"deny": [
"Read(./.env)",
"Read(./.env.*)"
]
}
}
Put it in .claude/settings.json in the project to share it with your team, or in ~/.claude/settings.json to apply it to every project on your machine.
Know what it covers. According to the permissions docs, Read deny rules apply to Claude Code's own file tools and to file commands it recognizes in the shell, such as cat and head. They don't apply to a script that opens the file itself, like a Python or Node program, or to a command that searches a whole folder. For a block that holds for every process, the docs point to Claude Code's sandbox.
Cursor: .cursorignore
Add your .env files to a .cursorignore file in the project root, using the same patterns as .gitignore:
.env
.env.*
Cursor's ignore-file docs say ignored files are blocked from Agent, Tab and @ mentions. The same page says the terminal and MCP server tools the agent uses can't block access to them, and that complete protection isn't guaranteed. So an agent that runs a shell command can still read a file you've ignored. Treat .cursorignore as a guard against accidents, not a wall.
MCP config without tokens in the file
Both tools let an MCP config read a value from an environment variable instead of storing it:
- Claude Code's MCP docs show
${VAR}in.mcp.json, for example a header of"Bearer ${API_KEY}". - Cursor's MCP docs use
${env:NAME}inmcp.json, and tell you to use environment variables for secrets and restricted keys with minimal permissions.
The variable still has to come from somewhere the agent can't read, such as your shell profile or system environment. Claude Code's docs also say to check you trust an MCP server before connecting it: a server that fetches outside content can carry prompt injection into your session.
Your editor can leak keys too
In April 2026, LayerX published CursorJacking, research showing that Cursor stored API keys and session tokens where any installed extension could read them. LayerX reported it to Cursor and listed it as not fixed when they published. The lesson goes beyond one editor: a key saved in your editor's settings is only as safe as every extension you've installed. Install extensions you trust, remove the ones you don't use, and keep production keys out of editor settings.
What these settings can't do
Deny rules and ignore files limit what the agent's tools read. They don't change what its process is allowed to do. An agent that can run any command can read whatever your user account can: files outside the project, environment variables, other tools' config. If that worries you, use the agent's sandbox or run it in a container or a separate user account that holds no production secrets. And whatever you do, assume a key the agent has seen is exposed.
Provider tips
Most providers let you make keys that can do less. Stripe has restricted keys and a test mode, and OpenAI has project keys. See the Stripe and OpenAI pages, or browse all providers.
Do this with StackHangar
StackHangar is built for exactly this. Your keys live in a vault that's encrypted on your device. Claude Code, Cursor and other MCP agents connect to it and ask:
- The agent asks to use a key, and you approve with a passkey on
app.stackhangar.com. StackHangar shows the value to you, and the agent gets a reference ID and your answer, never the value. - The agent asks you to save a new key. You paste it into StackHangar, not into the chat.
- The agent proposes rotating a key it saw. You approve, and the new value is made and saved on your device.
- Each agent has its own identity and project list, every request is logged, and you can disconnect it in one click.
It doesn't change the limits above: once you put a value where the agent can read it, the agent has it. See how agent access works for the details.
Sources
Checked 9 November 2026. Agent tools change often, so check the current docs for your version.
- Claude Code: Settings, Configure permissions, Sandboxing, MCP
- Cursor: Ignore file, MCP
- LayerX: CursorJacking: Every Cursor User Is Vulnerable to API Key Theft by Rogue Extensions (28 April 2026)