env-http-auth
Lightweight HTTP authentication via environment variables for httpx, requests, and raw HTTP clients.
Features
- Zero dependencies by default (stdlib only)
- Universal: Works with
requests,httpx, and any HTTP client - Multiple auth sources: Environment variables, netrc, keyring, config files
- Per-host authentication with suffix patterns
- Type safe: Full type hints for better development experience
Table of Contents
- env-http-auth
Installation
pip install env-http-auth
Quick Start
import os
from env_http_auth import get_auth
# Set environment variable
os.environ["HTTP_AUTH_TOKEN_example_com"] = "my-token"
# Get auth dict for URL
auth = get_auth("https://example.com/path")
# Returns: {'Authorization': 'Bearer my-token'}
Environment Variables
Environment variables are the primary way to configure authentication. Hostnames are normalized (lowercase, dots become underscores).
Variable Naming
| Priority | Variable | Example Value | Output |
|---|---|---|---|
| 1 | HTTP_AUTH_TOKEN_example_com |
mytoken |
Bearer mytoken |
| 2 | HTTP_AUTH_TOKEN__example_com |
mytoken |
Bearer mytoken (suffix) |
| 3 | HTTP_AUTH_HEADER_example_com |
Bearer token |
Bearer token |
| 4 | HTTP_AUTH_BASIC_example_com |
user:pass |
Basic dXNlcjpwYXNz |
| 5 | HTTP_AUTH_TOKEN |
token |
Bearer token |
| 6 | HTTP_AUTH_HEADER |
Bearer token |
Bearer token |
| 7 | HTTP_AUTH |
Custom creds |
Custom creds |
Notes
- Periods (
.) in hostnames become underscores (_) in env var namesexample.com→HTTP_AUTH_TOKEN_example_comartifactory.company.com→HTTP_AUTH_TOKEN_artifactory_company_com
- Matching is case-insensitive
- Use config file for hosts with many periods
Suffix Matching
Double underscore (__) indicates suffix matching:
export HTTP_AUTH_TOKEN__example_com=my-token
# Matches: example.com, sub.example.com, api.example.com
Scheme Specifier
Override the auth scheme per host:
export HTTP_AUTH_SCHEME_example_com=bearer
export HTTP_AUTH_TOKEN_example_com=my-token
# Output: Bearer my-token
export HTTP_AUTH_SCHEME_example_com=basic
export HTTP_AUTH_TOKEN_example_com=user:pass
# Output: Basic dXNlcjpwYXNz
Usage Examples
Basic usage
from env_http_auth import get_auth, get_auth_header
# Get full auth dict
auth = get_auth("https://example.com/path")
# {'Authorization': 'Bearer my-token'}
# Get just the header value
header = get_auth_header("https://example.com/path")
# 'Bearer my-token'
Using requests
from env_http_auth import HTTPEnvAuth
import requests
# Set environment variable
# HTTP_AUTH_TOKEN_example_com=my-token
# Use with requests
response = requests.get(
"https://example.com/api/data",
auth=HTTPEnvAuth()
)
Using httpx
from env_http_auth import HTTPEnvAuth
import httpx
# Set environment variable
# HTTP_AUTH_TOKEN_example_com=my-token
# Use with httpx
client = httpx.Client(auth=HTTPEnvAuth())
response = client.get("https://example.com/api/data")
AuthResolver class
For more control over authentication sources:
from env_http_auth import AuthResolver
# Use only specific sources
resolver = AuthResolver(sources={"env", "netrc"})
# Query by URL or hostname
auth = resolver.for_url("https://example.com/path")
auth = resolver.for_hostname("example.com")
# Custom source order
resolver = AuthResolver(sources={"config", "env", "keyring"})
Priority Chain
When resolving authentication, sources are tried in this order:
- Exact host match -
HTTP_AUTH_TOKEN_example_com - Suffix match -
HTTP_AUTH_TOKEN__example_com(double underscore) - Global env vars -
HTTP_AUTH_TOKEN - Config file -
~/.env-http-auth.ini - netrc -
~/.netrc - keyring - System keyring
Configuration Sources
Environment Variables
Primary source. Supports per-host and global variables. See Environment Variables for details.
netrc
Falls back to ~/.netrc for Basic authentication:
machine example.com
login admin
password secret123
System Keyring
Stores credentials in the system keyring:
import keyring
# Store token
keyring.set_password("example.com", "token", "my-secret")
# Store Basic auth
keyring.set_password("example.com", "username", "admin")
keyring.set_password("example.com", "password", "secret")
Config File
INI format at ~/.env-http-auth.ini:
[example.com]
token = my-token
scheme = bearer
[other.company.com]
basic_user = admin
basic_pass = secret
scheme = basic
CLI
Get authentication headers from the command line:
# Basic usage
$ export HTTP_AUTH_TOKEN_example_com=my-token
$ env-http-auth https://example.com
Authorization: Bearer my-token
# Header only (value only)
$ env-http-auth --header-only https://example.com
Bearer my-token
# No auth found
$ env-http-auth https://unknown.com
No authentication found for https://unknown.com
CLI Options
env-http-auth [-h] [--header-only] url
Get HTTP authentication header for a URL
positional arguments:
url URL to get authentication header for
options:
-h, --help show this help message and exit
--header-only Output only the header value
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
env-http-auth is distributed under the terms of the MIT license.
Metadata
Release files for env-http-auth 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| env_http_auth-0.0.2.tar.gz | 25.5 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| env_http_auth-0.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.0 kB
Release files / env_http_auth-0.0.2.tar.gz
| Download URL | env_http_auth-0.0.2.tar.gz |
|---|---|
| Size | 25.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c4d0402b838ce1748ea1df5f8985a25bd89066b39d0ed25b873a8ab7ae936e1
|
|
BLAKE2b-256 checksum How to use checksums |
23fa1935756c8a4c8757f6c74a7a02b89193b1e3888057b74d44cb095a641c48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 6, 2026.
Transparency logRelease files / env_http_auth-0.0.2-py3-none-any.whl
| Download URL | env_http_auth-0.0.2-py3-none-any.whl |
|---|---|
| Size | 12.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
49a182a9c9ce691e811d9cf0b0329acd42d4bc2a30d61f7ed370e6f634aa5600
|
|
BLAKE2b-256 checksum How to use checksums |
cd25e6e9bff3d5e7ade6b5d08e2004d9fed6557000d15a98d4c4a4c5aa7a9397
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 6, 2026.
Transparency logSecurity for env-http-auth 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for env_http_auth-0.0.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.