{"slug": "notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows", "title": "Notes on wrapping the UK Companies House API for shared workflows", "summary": "A developer built a shared wrapper service around the UK Companies House REST API to support multiple workflows, teammates and AI agents using one integration. The writeup details handling the API's 600-requests-per-5-minutes-per-key limit and a concurrency trap where mutating client.auth on a shared httpx.AsyncClient can leak one caller's key onto another's request; the fix stores incoming keys in an async ContextVar and passes auth explicitly per request. The wrapper also adds SHA-256-hashed per-key token buckets, zero-padded company numbers for consistent cache keys, and a 5-minute TTL cache to conserve upstream quota.", "body_md": "The official UK Companies House REST API is straightforward for single-user scripts. Once multiple workflows, teammates, or AI agents start sharing one wrapper service, a couple of practical constraints show up.\n\nFirst, Companies House applies a `600 requests per 5 minutes` rate limit per API key. Second, allowing callers to pass their own API key in a header (BYOK) has an easy concurrency trap. Mutating `client.auth` on a shared `httpx.AsyncClient` can leak one caller's key onto another caller's request.\n\nStore incoming keys inside an async `ContextVar` (resetting in `finally:`) and pass auth explicitly per request:\n\n```\nresp = await client.get(\n    f\"https://api.company-information.service.gov.uk/company/{company_number}\",\n    auth=httpx.BasicAuth(resolved_key, \"\"),\n)\n```\n\nBeyond key isolation, a lightweight wrapper adds a few small quality-of-life improvements over hitting the upstream endpoints directly. Hashing each resolved key with SHA-256 lets you enforce independent `600 / 300s` token buckets per key so heavy users don't slow down BYOK callers. Zero-padding short company numbers (`48839` to `00048839`) keeps cache keys consistent, and a 5-minute TTL cache avoids spending upstream quota on repeat profile or filing checks.\n\nThis kind of setup is mainly useful for UK KYB onboarding flows, accounting and compliance teams pulling officer histories, and n8n or MCP automations that need predictable rate handling. If you'd rather connect to an existing hosted instance with these guards built in, it is live here: [uk-companies-house-registry-mcp](https://apify.com/data-goose/uk-companies-house-registry-mcp).", "url": "https://wpnews.pro/news/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows", "canonical_source": "https://dev.to/datagoose/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows-1g5c", "published_at": "2026-12-04 03:08:53+00:00", "updated_at": "2026-12-04 03:16:33.866271+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "agent-protocols"], "entities": ["UK Companies House", "httpx", "Apify", "n8n", "MCP"], "also_reported_by": [], "takeaway": "When proxying a shared API key pool, store each caller's key in an async ContextVar and pass httpx.BasicAuth explicitly per request instead of mutating a shared AsyncClient's auth, which can leak one caller's key onto another's request.", "recommendation": {"advice": "Isolate per-caller credentials in ContextVars and pass auth per request, then hash keys with SHA-256 to enforce independent 600/300s token buckets so heavy users don't starve BYOK callers.", "playbook": "agent-architecture", "playbook_url": "https://wpnews.pro/playbooks/agent-architecture"}, "alternates": {"html": "https://wpnews.pro/news/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows", "markdown": "https://wpnews.pro/news/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows.md", "text": "https://wpnews.pro/news/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows.txt", "jsonld": "https://wpnews.pro/news/notes-on-wrapping-the-uk-companies-house-api-for-shared-workflows.jsonld"}}