API Key Handling
Relevant source files
The following files were used as context for generating this wiki page:
API Key Handling describes the mechanisms for providing, validating, and securing the authentication credentials required to interact with the FACEIT Data API. The library supports multiple methods for credential injection, including direct assignment and automatic environment variable discovery.
Sources: README.md:83-93, src/faceit/http/client.py:46-50
Architecture and Configuration
The BaseAPIClient class manages the core logic for API key lifecycle. It validates the key format during initialization and injects it into the HTTP headers for every request. The system uses a specific validator to ensure keys conform to the expected UUID-like format required by FACEIT.
Sources: src/faceit/http/client.py:108-112, src/faceit/http/client.py:145-149
Authentication Methods
You can supply the API key using three primary methods:
- Direct String: Pass the key as a string or UUID object to the resource constructor.
- Environment Variable: Set the
FACEIT_API_KEY(or the internal defaultFACEIT_SECRET) environment variable. - Custom Environment Key: Use the
FromEnvorBaseAPIClient.envwrapper to specify a different environment variable name.
Sources: README.md:83-93, src/faceit/http/client.py:72-74
Data Flow for Key Resolution
This diagram shows how the client resolves an API key from various input types before initiating an HTTP request.
Security and Privacy
The library implements measures to prevent accidental exposure of sensitive credentials.
Credential Masking
The api_key property uses a getter that masks the majority of the token. It returns only the first four and last four characters, separated by ellipses.
Sources: src/faceit/http/client.py:121-123, tests/test_http_client.py:130-134
Validation Logic
The client validates keys using a UUID validator. If the key format is incorrect, it raises a ValueError containing a link to the official FACEIT documentation.
Sources: src/faceit/http/client.py:103-107, tests/test_http_client.py:117-121
Key Implementation Components
| Component | Type | Description |
|---|---|---|
DEFAULT_API_KEY_ENV | env | The default environment variable key searched: FACEIT_SECRET. |
_api_key | str | Internal storage for the validated, raw API key. |
_base_headers | Mapping | Generates the Authorization header using the Bearer scheme. |
_get_secret_from_env | static method | Uses python-decouple to retrieve keys from the environment. |
Sources: src/faceit/http/client.py:74, src/faceit/http/client.py:139-143, src/faceit/http/client.py:188-196
Environment Variable Integration
To use automatic environment loading, the library requires the python-decouple package. This is included in the faceit[env] installation extra. If the package is missing when an environment-based key is requested, the library raises a DecoupleNotFoundError. If the variable itself is missing, it raises a MissingAuthTokenError.
Sources: pyproject.toml:41, src/faceit/http/client.py:188-196, src/faceit/exceptions.py:76-85
Key Resolution Sequence
The API Key Handling system ensures that every interaction with the FACEIT Data Resource is authenticated with a valid, correctly formatted token while providing flexibility for different deployment environments.