fetch supports multiple authentication methods for accessing protected resources.
Basic Authentication sends credentials as a base64-encoded Authorization header.
fetch --basic username:password example.comThe --basic flag sets the Authorization header:
Authorization: Basic base64(username:password)
Credentials in URL userinfo, such as https://user:password@example.com, are
also converted to Basic authentication and removed from the request URL.
Explicit Authorization headers override URL userinfo. The --basic and
--bearer options take precedence over an explicit header.
Digest Authentication uses a challenge-response mechanism that is more secure than Basic Authentication because credentials are never sent in plain text.
fetch --digest username:password example.comThe --digest flag performs a two-step handshake:
- Challenge: The server responds with
401 Unauthorizedand aWWW-Authenticate: Digest ...header containing a nonce and realm. - Response:
fetchcomputes the RFC 7616 digest and resends the request with anAuthorizationheader:
Authorization: Digest username="...", realm="...", nonce="...", uri="...", response="..."
fetch supports MD5, MD5-sess, SHA-256, SHA-256-sess, SHA-512-256, and SHA-512-256-sess. It supports challenges with no qop and selects auth when it appears in a qop list. auth-int and unknown-only qop lists are rejected with a clear diagnostic. Quoted UTF-8 values and quoted-pair escapes are preserved. A challenge response can replay the request body, and a stale nonce is retried once; one-shot bodies such as stdin cannot be replayed for Digest authentication. This authentication replay is separate from transient --retry behavior: it can still resend an unsafe method without --retry-unsafe.
fetch supports curl's --digest flag when using --from-curl:
fetch --from-curl 'curl --digest -u username:password example.com'Bearer tokens are commonly used with OAuth 2.0 and JWT-based authentication.
fetch --bearer mytoken123 example.comThe --bearer flag sets the Authorization header:
Authorization: Bearer mytoken123
For security, avoid putting tokens directly in commands:
fetch --bearer "$API_TOKEN" example.comOr read from a file:
fetch -H "Authorization: Bearer $(cat ~/.api-token)" example.comSign requests for AWS services using AWS Signature V4.
Set the required environment variables:
export AWS_ACCESS_KEY_ID="your-access-key"
export AWS_SECRET_ACCESS_KEY="your-secret-key"fetch --aws-sigv4 REGION/SERVICE url# S3 request
fetch --aws-sigv4 us-east-1/s3 https://my-bucket.s3.amazonaws.com/key
# API Gateway
fetch --aws-sigv4 us-west-2/execute-api https://abc123.execute-api.us-west-2.amazonaws.com/prod/resource
# Lambda function URL
fetch --aws-sigv4 eu-west-1/lambda https://xyz.lambda-url.eu-west-1.on.aws/AWS SigV4 signs the request by:
- Creating a canonical request from the HTTP method, path, query string, headers, and body
- Generating a signing key from your secret key, date, region, and service
- Computing an HMAC-SHA256 signature
- Adding the signature to the
Authorizationheader
For temporary credentials, also set AWS_SESSION_TOKEN. fetch sends it as
x-amz-security-token and includes that header in the signature. SigV4 keeps
duplicate query parameters, sorts their AWS-encoded keys and values, and treats
+ as a literal plus sign.
When AWS signing is enabled, it generates the Authorization header. AWS
credentials and signing headers are removed before a cross-origin redirect.
mTLS provides two-way authentication where both client and server present certificates.
fetch --cert client.crt --key client.key example.comIf your PEM file contains both the certificate and private key:
fetch --cert client.pem example.comWhen the server uses a private CA:
fetch --cert client.crt --key client.key --ca-cert ca.crt example.com# Global mTLS settings
cert = /path/to/client.crt
key = /path/to/client.key
# Host-specific mTLS
[api.secure.example.com]
cert = /path/to/api-client.crt
key = /path/to/api-client.key
ca-cert = /path/to/api-ca.crt- Certificates and keys must be in PEM format
- Encrypted private keys are not supported
- Combined PEM files should have the certificate before the key
Generate test certificates:
# Generate CA
openssl genrsa -out ca.key 4096
openssl req -new -x509 -days 365 -key ca.key -out ca.crt -subj "/CN=Test CA"
# Generate client certificate
openssl genrsa -out client.key 4096
openssl req -new -key client.key -out client.csr -subj "/CN=client"
openssl x509 -req -days 365 -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crtUse with fetch:
fetch --cert client.crt --key client.key --ca-cert ca.crt https://mtls.example.comFor safety, automatic redirects with mTLS configured are allowed only within the initial HTTP origin. A redirect to a different scheme, host, or port is refused before the destination TLS handshake, so the client certificate cannot be presented to an unintended service.
For authentication methods not directly supported, use custom headers:
# API Key in header
fetch -H "X-API-Key: your-api-key" example.com
# Custom token format
fetch -H "X-Auth-Token: custom-token" example.com
# Multiple auth headers
fetch -H "X-API-Key: key" -H "X-Signature: sig" example.com[api.example.com]
header = X-API-Key: your-api-key
header = X-Client-ID: client123Credential-bearing custom headers are removed before a redirected request is
sent to a different scheme, host, or port. This includes the examples above
and generated headers whose names identify authentication material. Header
names are matched by complete components and case boundaries, including
compound names such as X-ApiKey, rather than arbitrary substrings, so
ordinary headers such as X-Keyboard-Layout and X-KeyboardLayout continue
to follow redirects.
Code that generates a credential header with an otherwise unclassified name
must mark it with client.MarkCredentialHeaders. Credential headers are not
restored if a later redirect returns to the original origin; ordinary
non-credential headers continue to follow redirects.
Authentication options are mutually exclusive. You cannot combine:
--basic--digest--bearer--aws-sigv4
If you need multiple authentication headers, use -H for additional headers.
- Avoid embedding secrets in scripts - Use environment variables or secure vaults
- Protect configuration files - Set appropriate file permissions (
chmod 600) - Use HTTPS - Never send credentials over unencrypted HTTP
- Rotate credentials regularly - Especially API keys and tokens
# Using environment variables
export API_TOKEN="$(vault read -field=token secret/api)"
fetch --bearer "$API_TOKEN" example.com
# Using password manager
fetch --basic "$(pass show api/credentials)" example.com
# Reading from secure file
fetch --bearer "$(cat /run/secrets/api-token)" example.com- Verify credentials are correct
- Check if the authentication method matches what the server expects
- Ensure tokens haven't expired
- Authentication succeeded but authorization failed
- Check if your credentials have the required permissions
- Verify certificate and key match:
openssl x509 -noout -modulus -in cert.crt | openssl md5should matchopenssl rsa -noout -modulus -in key.key | openssl md5 - Check certificate expiration:
openssl x509 -noout -dates -in cert.crt - Ensure the CA certificate is correct for the server
- Verify
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYare set - Check the region and service name are correct
- Ensure your credentials have the required IAM permissions
- Verify system clock is accurate (signatures are time-sensitive)
- CLI Reference - All authentication flags
- Configuration - Setting up authentication in config files
- Troubleshooting - Common issues and solutions