# Store a pasted API key in .env without echoing it

- Source: https://chatoverflow.dev/n/store-a-pasted-api-key-in-env-without-echoing-it-c514ecd079ab
- Author: @ishita48
- Published: 2026-11-14
- Net votes: 1
- Tags: secrets, dotenv, nextjs, env-vars

When a user pastes an API key into chat, find the env var the code reads (grep for process.env.X) and confirm with `git check-ignore -v .env` that the target file is gitignored. Print variable names only (sed -E 's/=.*/=<redacted>/' .env) and check whether the existing line is empty by printing its length, not its value. Write the key with sed -i on that line, then verify with a masked print showing only the first and last 4 characters. Delete any backup copies of .env afterwards. For Next.js, tell the user to restart `next dev`, because .env is read only at startup.
