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.
Make sure Git ignores it. Your
.gitignorefile should have a line that says.env. Without it, the key can end up on GitHub.Fill it in yourself, outside
Claude. Open the file in any text editor, type the name, paste the key, save.
Tell
Claude only the name. Never the value.
Tell
Claude not to open or print the file. This prompt does both:
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:
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:
You copy the key at the provider's website. You do not paste it anywhere.
Claude runs a script that reads your clipboard and writes the key into
.env(and refuses if the project uses Git and.envis not ignored).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:
--- 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
Copy the
secretfolder from the ZIP into yourClaude skills folder:
~/.claude/skills/on Mac and Linux, orC:\Users\YOU\.claude\skills\on Windows.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 useswl-paste,xcliporxsel, so install one if you have none.Restart
Claude Code and type
/secretto check it shows up.
Use
Type
/secretand say what the key is for: "Save my Stripe test key as STRIPE_KEY in this project's .env."Go to the provider's site and copy the key. Do not paste it anywhere.
Tell
Claude "go".
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
Use a fake key. Make up a long random string.
Copy the fake string, type
/secret, and askClaude to save it as TEST_KEY.
Check that
Claude shows only the masked line.
Run
cut -d= -f1 .envin your terminal. You should see the nameTEST_KEYand no value.Check that
git statusdoes 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
Pasting the key "just once". It lands in the transcript.
No
.envline in.gitignore. The key can reach GitHub.Asking
Claude to read the
.envfile to check it. That writes the key into the transcript. List the names only.Forgetting the clipboard history. Clear it.
Reusing a key that already leaked. Replace it at the provider.
Letting
Claude run a command that prints the key. Commands like
envorprintenv, 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
Add
.envto your.gitignoretoday.Install the skill and test it with a fake key.
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