Keep passwords and API keys out of your Claude Code chats

Two safe ways to give Claude Code a key without pasting it in chat, plus a free /secret skill.

You copy the key

At the provider's website. Ctrl+C.

ChatPaste the key here
/secret
A script writes it to .envCRM_KEY=****.env must be in .gitignore first.Example name
Claude uses the name
Use the key named CRM_KEYThe key never enters the chat
  1. You copy the key at the provider's website. You never paste it into the chat.
  2. The /secret skill runs a script that writes the key into a .env file, after checking that .env is ignored by Git. Claude shows only a masked check.
  3. From then on you tell Claude the key's name, for example CRM_KEY, and never its value.
20 minSome setup

You need

  • Claude Code
  • A project folder
  • Git for Windows (Windows only)

You need to give Claude Code an API key. The quick way is to paste it into the chat. Do not.

Claude Code's docs say the files it keeps in your ~/.claude folder are plaintext, and that anything passing through a tool is written to a transcript on disk, including file contents, command output and pasted text. They also say transcripts and history are not encrypted at rest. So a key you paste once sits in a plain text file on your computer, until you replace the key at the provider.

This guide gives you two proper ways to hand over a key.

01 / 05

Way one: a .env file

A .env file is a plain text file in your project folder. Each line is a name and a value, like CRM_KEY=the-key-goes-here. Your app reads it at start-up, so the key never sits inside your code.

  1. Make sure Git ignores it. Your .gitignore file should have a line that says .env. Without it, the key can end up on GitHub.

  2. Fill it in yourself, outside Claude. Open the file in any text editor, type the name, paste the key, save.

  3. Tell Claude only the name. Never the value.

  4. Tell Claude not to open or print the file. This prompt does both:

Use a key by name only
Use the key named CRM_KEY from the .env file in this project. Do not open, read or print the .env file. Never run a command that prints environment variables or the key's value. If you need to check that the key is there, list the names only with: cut -d= -f1 .env
264 characters
  1. Add a block as well. Claude Code's permission rules can deny reads of a file. The docs name Read(./.env) as an example, and say it blocks Claude's file tools from reading that file. Ask Claude to add it, and check the result yourself:

Add a deny rule for .env
Add a deny rule for Read(./.env) to the permissions section of this project's .claude/settings.json. Create the file if it does not exist, and keep anything already in it. Then show me the file.
194 characters

This stops the file tools. It is not a lock on everything: a command that prints the file is a different route, which is why step 4 still matters. Claude Code's docs also say the deny rule is applied on a best-effort basis to tools like Grep and Glob.

Without it, the docs warn that if a tool reads a .env file, the value is written to the transcript. That is why the file stays out of Claude's reach.

02 / 05

Way two: the /secret skill

Way one still asks you to open a file and paste a key by hand. The /secret skill does that step for you. It is a small skill that does three things:

  1. You copy the key at the provider's website. You do not paste it anywhere.

  2. Claude runs a script that reads your clipboard and writes the key into .env (and refuses if the project uses Git and .env is not ignored).

  3. Claude shows a masked check only: the length, plus the first four characters when the key is 24 or longer. The key itself is never printed.

Download the ZIP. It holds the skill, the script and a short note on .env files. The skill's instructions are short enough to read before you install it:

The skill's instructions, shortened
secret/SKILL.md
---
name: secret
description: Take a password, API key or token from the user WITHOUT it ever entering the chat. The user copies the value to the clipboard; a helper script writes it to a gitignored .env file (or a file, or a command's input) and prints only a masked check. Use when you need any credential, when the user types /secret, or when the user is about to paste a key into chat.
---

# secret: take a key without it touching the chat

Anything typed or pasted into this conversation is saved to a file on the user's computer. A key that lands there stays there until it is replaced at the provider. So never ask for a secret in chat. Use this flow.

## Steps
1. Tell the user exactly what to copy and where it will go. Example: "Copy your new Stripe test key. I will save it as STRIPE_KEY in .env in this project."
2. The user copies it (Ctrl+C or Cmd+C) and says "go". They do not type it.
3. Check the destination is safe: the .env file must be listed in .gitignore. If .gitignore has no .env line, add one first.
4. Run the helper with an absolute path: bash SKILL-FOLDER/ingest.sh env STRIPE_KEY /absolute/path/to/project/.env
5. Report the masked line it prints. Ask the user if the length looks right.

## Hard rules
- Never print a secret or a file that holds one. To check a file, list the key names only: cut -d= -f1 .env
- Never read the clipboard yourself. Only the helper script does.
- Never put a secret in a command argument, a commit, a log or a message.
- Never ask the user to type or paste a secret in chat, even "just this once".

That is a shortened view. The ZIP has the full file and the script.

Install

  1. Copy the secret folder from the ZIP into your Claude skills folder: ~/.claude/skills/ on Mac and Linux, or C:\Users\YOU\.claude\skills\ on Windows.

  2. Windows only: install Git for Windows, which includes Git Bash. The script uses it to read your clipboard. On a Mac it uses pbpaste, which is built in. On Linux it uses wl-paste, xclip or xsel, so install one if you have none.

  3. Restart Claude Code and type /secret to check it shows up.

Use

  1. Type /secret and say what the key is for: "Save my Stripe test key as STRIPE_KEY in this project's .env."

  2. Go to the provider's site and copy the key. Do not paste it anywhere.

  3. Tell Claude "go".

  4. Check the line it shows, such as ok: len=32 prefix=sk_t**** (value not printed). The length should match what you copied.

Your clipboard history (Windows Win+V, or a clipboard app) may still hold the key. Clear that entry when you are done.

Download the ZIP for the skill, the script and the .env note.

03 / 05

Already pasted a key into a chat?

Change that key now. Make a new one at the service that issued it and disable the old one. Deleting the chat is not enough, because the old key may already be copied. Then give the new key to Claude by way one or way two.

04 / 05

Test it safely

  1. Use a fake key. Make up a long random string.

  2. Copy the fake string, type /secret, and ask Claude to save it as TEST_KEY.

  3. Check that Claude shows only the masked line.

  4. Run cut -d= -f1 .env in your terminal. You should see the name TEST_KEY and no value.

  5. Check that git status does not list .env.

Where was this tested? The script was run on a made-up key in a throwaway project, with a test switch standing in for the clipboard. It wrote one line, refused when .env was not ignored, refused an empty clipboard, and never printed the value anywhere in its output. The Mac and Linux clipboard readers were tested with stand-in programs only. Reading a real clipboard on Windows, Mac and Linux was not tested, and neither was the pipe mode that hands a key straight to a hosting service. Check the masked line the first time.

Common mistakes

  1. Pasting the key "just once". It lands in the transcript.

  2. No .env line in .gitignore. The key can reach GitHub.

  3. Asking Claude to read the .env file to check it. That writes the key into the transcript. List the names only.

  4. Forgetting the clipboard history. Clear it.

  5. Reusing a key that already leaked. Replace it at the provider.

  6. Letting Claude run a command that prints the key. Commands like env or printenv, or an app that logs its settings, also land in the transcript. Ask for names only.

Does this make a leaked key safe?

No. It stops the key reaching the chat. A key that is already in a chat or on GitHub must be replaced at the provider.

Do I need to code?

No. You copy a key and tell Claude to go.

Why not just trust Claude with the key?

Because the chat is saved to a plain text file. Claude's good behaviour does not change that.

Does it work for passwords as well as API keys?

Yes, anything that fits on one line. A multi-line key file uses the script's file mode.

Finish line

What to do next

  1. Add .env to your .gitignore today.

  2. Install the skill and test it with a fake key.

  3. If you have pasted a real key into a chat before, replace it now.

Back to all the free guides. Two that pair well with this one: give your AI its own login and Claude Code hooks that block pushes, deletes and secrets.

You reached the end of the guide