Skip to main content

hmrclib

Comprehensive, typed Python client for the HMRC Developer Hub APIs (Making Tax Digital).

hmrclib is a PyPI package that wraps every REST API published on the HMRC Developer Hub with:

  • Generated pydantic v2 models for every OpenAPI schema in every API
  • Typed client classes — one per API, one method per endpoint
  • Full OAuth2 support: client_credentials (application-restricted), authorization_code + refresh_token (user-restricted), with an optional FastAPI callback server for the browser redirect journey
  • Strict typing — the package itself is mypy --strict clean and ruff clean

Installation

pip install hmrclib
# or, with the OAuth callback server extra:
pip install "hmrclib[server]"

Quick start

import os
from hmrclib import OAuth2Client, HMRCClient
from hmrclib.generated.clients import VatApiClient, VatRegisteredCompaniesApiClient

os.environ["HMRC_ENV"] = "sandbox"
os.environ["HMRC_CLIENT_ID"] = "..."
os.environ["HMRC_CLIENT_SECRET"] = "..."

oauth = OAuth2Client()
oauth.client_credentials("read:vat")

client = HMRCClient(oauth=oauth, scope="read:vat")
vat = VatApiClient(client)

# GET /organisations/vat/{vrn}/obligations
result = vat.get_organisations_vat_vrn_obligations(vrn="123456789")
print(result)

Supported APIs

All 73 REST APIs published on the HMRC Developer Hub are generated from their official OpenAPI specifications (vendored under specs/), including Making Tax Digital (Self Assessment, VAT, Property, CIS), Customs, Lifetime ISA, Agent Authorisation, and the test-support APIs.

OAuth2

from hmrclib import OAuth2Client

oauth = OAuth2Client()
oauth.client_credentials("read:vat")  # application-restricted

# user-restricted: build the authorize URL, then exchange the code
url = oauth.authorization_url("hello", state="abc123")
# ... user completes the browser journey ...
token = oauth.authorization_code("CODE_FROM_REDIRECT")
token = oauth.refresh(token.refresh_token)  # keep it alive

OAuth callback server (FastAPI)

uvicorn hmrclib.server:app --port 8300

Serve the registered redirect URI (e.g. https://baseframe.rodmena.co.uk/oauth/hmrc/callback), capture the code and exchange it for tokens:

curl https://baseframe.rodmena.co.uk/oauth/hmrc/result

Development

uv pip install -e ".[dev]"
ruff check .
mypy .
pytest

The generated code can be regenerated from the vendored specs:

python -m hmrclib.codegen --specs specs --out src/hmrclib/generated

License

MIT. hmrclib is not affiliated with HMRC; the API specifications it implements are © Crown copyright, available under the Open Government Licence v3.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hmrclib-0.1.1.tar.gz (836.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hmrclib-0.1.1-py3-none-any.whl (186.2 kB view details)

Uploaded Python 3

File details

Details for the file hmrclib-0.1.1.tar.gz.

File metadata

  • Download URL: hmrclib-0.1.1.tar.gz
  • Upload date:
  • Size: 836.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for hmrclib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9be6cf7dd9541a0c5906df7146b8be8e735a05bd2bf7434e2f934c6efc192e86
MD5 9afa46f9ffa7edfe03b1a672bc88ad8f
BLAKE2b-256 dc759f9d3a9366a16679194fb899d13738f4161378a6dcd8e3092135554be03f

See more details on using hashes here.

File details

Details for the file hmrclib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: hmrclib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 186.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for hmrclib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 842b5ade2b19887d6238a7720d9eda83f4230cc90d822581b481c3a1c7d1d419
MD5 745a9f8cb73927e8396c3c8fc28b428a
BLAKE2b-256 28f0846c93d4c807c0e50a5506c58465316f1c74a7f503b0465833e234822c3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page