Safeguard
Developer Tools•

How do you use curl with an API token without leaking it?

Read headers from a file with curl -H @file or use a netrc file, so the token stays out of shell history. Know what each approach does and does not hide.

Safeguard Security Team
4 min read

Put the token in a file and let curl read it, rather than typing it on the command line. curl can read headers from a file with -H @filename, and it can pull a login from a netrc file. Both keep the secret out of your shell history, though the file itself must be protected.

The problem with inline tokens

The easy way to call an API is to paste the header into the command, something like an Authorization header with a bearer value. That line goes into your shell history, shows up in terminal scrollback, and may appear in process listings while it runs. It also gets copied into tickets and chat when someone asks how you made the call.

Option 1: read headers from a file

The curl manual says the --header option can take an argument in @filename style, which adds a header for each line in the input file. Using @- makes curl read the header file from standard input. You can use --header several times.

curl -H @headers.txt https://api.example.com/v1/items

Here headers.txt holds one header per line. Create it with permissions only your user can read, and keep it out of version control. Because the command no longer includes the value, your history stays clean and the line is safe to paste into a bug report.

Option 2: a netrc file

The manual explains that -n or --netrc makes curl scan a netrc file in your home directory for login name and password. If the NETRC environment variable is set, that file name is used, and --netrc-file overrides every other way of choosing the file. The file format is described in netrc(5). The manual also says the netrc file supplies credentials for a hostname regardless of protocol and port.

This suits basic authentication to a host you call often. It does not fit every bearer token API, so the header file is more flexible there.

What curl hides and what it does not

For options that carry a secret, such as a user name and password given on the command line, the manual says that on systems where it works, curl hides the given option argument from process listings. That helps, but it does not remove the text from your shell history. The file based approaches remove it from both places.

ApproachIn shell historyIn process listFile to protect
Inline headerYesLikelyNo
Header file with @NoNoYes
netrc fileNoNoYes

A quick routine

  1. Create the file with umask 077 set so it is private from the start.
  2. Reference it in the command rather than pasting the value.
  3. Use a token with narrow scope and an expiry date.
  4. Never paste real verbose output, because the -v flag prints request headers.
  5. If a token ever hit your history, revoke it and issue a new one.

Where Safeguard fits

Safeguard works on the dependency and code side of your software: SBOM and dependency scanning, reachability analysis and policy gates. It does not store or manage credentials, so keep using your password manager, secrets manager or cloud identity for that. You can run a free scan of a project at gold.safeguard.sh/scan.

Quick check

Does @filename work with several headers?

The manual says it adds a header for each line in the file.

Does netrc work for bearer tokens?

It is described as a login name and password store. Use a header file for token headers.

Does -v print my token?

Verbose mode prints request headers, so redact them before sharing output. Check your curl version's behaviour.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.