Long curl commands with tokens become maintainable, reusable and safer when moved into config files and variables.
Any command-line option can live in a config file, read with -K file (or -K - for stdin). Each line is an option without the leading dashes, optionally with =: header = "Authorization: Bearer xyz", url = "https://...", fail-with-body. curl also reads ~/.curlrc automatically unless the first option is -q.
Config files keep secrets out of ps output and shell history, and let a team share request recipes. Feeding a config from stdin is a neat trick to pass a header without it ever appearing in a process list:
printf 'header = "Authorization: Bearer %s"\n' "$TOKEN" | curl -K - https://api.example.com/me
Variables (curl 8.3+) make commands templatable:
--variable name=valuesets one,--variable name@fileloads a file's contents, and--variable %NAMEimports an environment variable (with%NAME=defaultas fallback).- Options prefixed with
--expand-substitute{{name}}:--expand-header,--expand-url,--expand-data,--expand-json... - Functions transform values on expansion:
{{name:trim}},{{name:url}}(URL-encode),{{name:json}}(JSON string escaping, without the surrounding quotes),{{name:b64}}; they can be chained.
Expansion only happens in --expand- options, so a literal {{ elsewhere stays untouched.
Example
# api.curl -- a reusable request recipe
# silent
# show-error
# fail-with-body
# header = "Accept: application/json"
curl -K api.curl https://api.example.com/items
# pass a secret via stdin config: never visible in ps
printf 'header = "Authorization: Bearer %s"\n' "$TOKEN" | curl -K - https://api.example.com/me
# variables (curl 8.3+)
curl --variable %TOKEN --expand-header 'Authorization: Bearer {{TOKEN}}' https://api.example.com/me
curl --variable 'q=rust & go' --expand-url 'https://api.example.com/search?q={{q:url}}'
curl --variable 'name=Ann "A"' --expand-json '{"name":"{{name:json}}"}' https://api.example.com/users
# body: {"name":"Ann \"A\""}Remember
- -K file reads options from a file; ~/.curlrc is read by default
- --variable %ENV / name=value / name@file, used via --expand-*
- {{v:url}}, {{v:json}}, {{v:trim}} encode values safely
Watch out for
- Putting tokens directly on the command line in shared machines or CI logs; use -K - or variables from env.