Skip to main content

Overview

api_key_tool_factory builds tools for any API that authenticates with static headers. No credential provider, no refresh flow — just header injection. Use oauth_tool_factory instead when the token is fetched or refreshed per call.

Design

  • Simple — one dictionary of headers
  • Flexible — single or multiple auth headers
  • Overridable — set defaults on the factory, override per tool
  • Checked — an empty header value is rejected at build time, not at the first 401

Basic usage

Single header (most common)

scraper_client.py

Multiple headers

multi_header_client.py

Bearer token

bearer_client.py

Per-tool overrides

Different endpoints can use different keys:
per_tool_keys.py

Signature

The returned builder takes:

Errors

  • Empty header value, on the factory or a tool -> DeclarationError at build time
  • No headers on either the factory or the tool -> DeclarationError naming the tool
  • 401 from the API -> CredentialError (declare credential_statuses={401, 403} if the API means “your token” by 403 too — most mean “not allowed”, which is an APIError)
  • Any other >= 400 -> APIError carrying the status and a body excerpt