Give Your Coding Agent an API Key Without Pasting It Into Chat
SnapPwd Team
5 min read
When a coding agent needs an API key from you, the quickest answer is to paste the key into the chat. That works, and it leaves the key in the conversation for as long as the conversation is kept. A safer answer takes one extra step: create a one-time SnapPwd link to the key, give the agent the link, and have it run:
npx -y @snappwd/cli@latest get "<link>" --dotenv /srv/app/.env --var API_TOKEN
# Added API_TOKEN in /srv/app/.env
The CLI decrypts the key on the machine where it runs and writes API_TOKEN=… into the file. It prints the file path, never the value, so the key does not enter the agent's context. This needs SnapPwd CLI 1.5.0 or later.
This post explains why pasting is the problem, walks through the workflow, and is specific about what it does and does not protect.
Why not just paste the key?
A key pasted into an agent conversation:
- stays in the conversation history for as long as that history is kept
- is typically sent to the model provider again with each later turn, because the conversation is the model's context
- is kept under the provider's data retention policy, not yours
- can be repeated by the agent later, in a summary, a commit message or a log line
None of this depends on the agent misbehaving. It is where a pasted value goes. The same reasoning applies to pasting keys into Slack or email, covered in how to share API keys with your team.
The workflow, step by step
1. Create a link to the key. Open snappwd.io, paste the key, choose an expiry and click Generate Secure Link. The key is encrypted in your browser; SnapPwd's server only receives ciphertext. From a terminal you can use snappwd put instead, but that puts the key on the command line, where it can end up in your shell history.
2. Give the agent the link and the target. For example:
Install this credential as
OPENAI_API_KEYin/srv/app/.envon the staging server:https://snappwd.io/g/sps-…#…. Usesnappwd get --dotenv, and don't print the file afterwards.
3. The agent runs the command on the machine that needs the key. If the .env file lives on a server, the agent runs the CLI there, for example over SSH, so the key only ever exists on that server:
ssh staging 'npx -y @snappwd/cli@latest get "https://snappwd.io/g/sps-...#..." --dotenv /srv/app/.env --var OPENAI_API_KEY'
# Updated OPENAI_API_KEY in /srv/app/.env
4. The link stops working. SnapPwd deletes the encrypted payload as it returns it. If anyone tries the link again, they get a 404.
What the CLI does to the file
- If the file already has an
OPENAI_API_KEY=line (orexport OPENAI_API_KEY=), that line is replaced. Otherwise a new line is appended. Other lines are left alone. - A missing file is created readable only by its owner (mode
600). An existing file keeps its mode. - The value is written bare when it is plain, in single quotes when it contains spaces or shell characters, and in double quotes with
\nescapes when it contains a single quote or a newline. - The variable name and the file are checked before the link is opened. A typo in the name or a missing directory fails without using up the link.
- If the write still fails after the link has been opened, the key is saved to a private temporary file instead, the command prints that path, and it exits with status 1.
--dotenv works for text secrets only. For a file link, such as a whole .env file shared with put-file, use --output.
One naming detail: the flag is --dotenv, not --env-file. Node.js treats --env-file as its own option even when it appears after the script name, so the CLI would never see it.
Agents that use SnapPwd over MCP
If your agent talks to SnapPwd through its MCP server instead of a shell, the get_secret tool does the same thing when you pass env_file and env_key. The tool result then contains the path and whether the line was added or replaced, not the plaintext. In Claude Code:
claude mcp add snappwd -- npx -y @snappwd/cli mcp
Make it the default
Add a rule to your agent instructions file (AGENTS.md, CLAUDE.md or your tool's equivalent):
When you need a password, token or key from me, ask me for a SnapPwd
link instead of the value. Install it with
npx -y @snappwd/cli@latest get "<link>" --dotenv <file> --var <NAME>
and never print the file or the variable afterwards.
The open-source snappwd-share skill includes the same guidance for agents that load skills.
What this does not protect
Be clear about the boundary:
- It keeps the key out of the conversation, not off the machine. An agent that later runs
cat .env, prints its environment or debugs a failing request can still see the key. A prompt injection could ask it to. - The link is in the chat until the agent opens it. Anyone who reads the conversation before then can open the link first. Once it has been opened, the copy in the chat is useless.
- A 404 is a warning. If the agent's
getfails with a 404 and you did not open the link yourself, someone else opened it or it expired. Revoke the key and send a new link. - It does not replace a secrets manager. For production services, the runtime should read keys from your secrets manager. This workflow is for the handoff: getting a key from you to a machine an agent is setting up.
For a longer list of limits, see what a one-time link does not protect.
Summary
- Don't paste API keys into agent conversations. Send a one-time SnapPwd link.
- Have the agent run
snappwd get "<link>" --dotenv <file> --var <NAME>(CLI 1.5.0+), or callget_secretwithenv_fileandenv_keyover MCP. - The key lands in the file, the agent only sees the path, and the link stops working.
- Treat a 404 as a sign the key may be exposed, and rotate it.
SnapPwd is made by the team writing this post. The CLI and the MCP server inside it are open source under the MIT license, so you can read exactly what --dotenv does before you rely on it.