ExAzureCore.Http.Plugins.ApiKey (ex_azure_core v0.2.0)

Copy Markdown

Req plugin that adds API key authentication to requests.

Adds the API key as a header with a configurable name. Supports both raw string keys and AzureKeyCredential structs.

Options

  • :api_key - The API key (string or %AzureKeyCredential{})
  • :header_name - Header name (default: "api-key")
  • :prefix - Optional prefix for the header value (e.g., "Bearer")

Common Header Names by Service

  • Cognitive Services: "Ocp-Apim-Subscription-Key"
  • Azure Search: "api-key"
  • Form Recognizer: "Ocp-Apim-Subscription-Key"
  • Translator: "Ocp-Apim-Subscription-Key"

Example

# Using a string key
req = Req.new()
|> ExAzureCore.Http.Plugins.ApiKey.attach(api_key: "my-key")

# Using an AzureKeyCredential
{:ok, credential} = AzureKeyCredential.new("my-key")
req = Req.new()
|> ExAzureCore.Http.Plugins.ApiKey.attach(api_key: credential)

# With custom header name (Cognitive Services)
req = Req.new()
|> ExAzureCore.Http.Plugins.ApiKey.attach(
  api_key: "my-key",
  header_name: "Ocp-Apim-Subscription-Key"
)

# With prefix
req = Req.new()
|> ExAzureCore.Http.Plugins.ApiKey.attach(
  api_key: "my-key",
  prefix: "ApiKey"
)

Summary

Functions

Attaches the API key plugin to a Req request.

Functions

attach(request, opts \\ [])

@spec attach(
  Req.Request.t(),
  keyword()
) :: Req.Request.t()

Attaches the API key plugin to a Req request.