Skip to main content

APIKeyBase

No overview available.

Attributes

AttributeTypeDescription
model[APIKey](../../../openapi/models/apikey.md?sid=fastapi_openapi_models_apikey)This attribute stores an instance of the APIKey class, which defines the API key's location, name, and description for authentication.

Constructor

Signature

def APIKeyBase(
location: [APIKeyIn](../../../openapi/models/apikeyin.md?sid=fastapi_openapi_models_apikeyin),
name: str,
description: str | None,
scheme_name: str | None,
auto_error: bool
) - > null

Parameters

NameTypeDescription
location[APIKeyIn](../../../openapi/models/apikeyin.md?sid=fastapi_openapi_models_apikeyin)The location of the API key (e.g., header, query).
namestrThe name of the API key parameter.
description`strNone`
scheme_name`strNone`
auto_errorboolWhether to automatically raise an HTTPException on authentication failure.

Methods


make_not_authenticated_error()

@classmethod
def make_not_authenticated_error() - > [HTTPException](../../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception)

The WWW-Authenticate header is not standardized for API Key authentication but the HTTP specification requires that an error of 401 "Unauthorized" must include a WWW-Authenticate header. Ref: https://datatracker.ietf.org/doc/html/rfc9110#name-401-unauthorized For this, this method sends a custom challenge APIKey.

Returns

TypeDescription
[HTTPException](../../../exceptions/httpexception.md?sid=fastapi_exceptions_httpexception)An HTTPException with a 401 status code and a WWW-Authenticate header set to "APIKey".

check_api_key()

@classmethod
def check_api_key(
api_key: str | None
) - > str | None

Checks if an API key is provided. If no API key is provided and auto_error is true, it raises an HTTPException. Otherwise, it returns the provided API key or None.

Parameters

NameTypeDescription
api_key`strNone`

Returns

TypeDescription
`strNone`