DEFRA Environment Client 🌿
Python client for DEFRA’s environment.data.gov.uk APIs — fast, typed, and async‑friendly. 🌟
Installation 🧰
uv pip install environment-client
Usage 🐍
import asyncio
from environment.flood_monitoring import FloodClient
from environment.public_register import PublicRegisterClient
async def main():
"""
An example of how to use the clients to get data from the APIs.
"""
async with (
FloodClient() as flood_client,
PublicRegisterClient() as public_register_client,
):
# Get flood warnings
flood_warnings = await flood_client.get_flood_warnings()
print(f"Found {len(flood_warnings)} flood warnings.")
# Search for waste operations registrations
waste_operations = await public_register_client.get_waste_operations(limit=5)
print(f"Found {len(waste_operations.items)} waste operations.")
# Search across all registers
all_registrations = await public_register_client.search_all_registers(name_search="Limited", limit=5)
print(f"Found {len(all_registrations.items)} registrations with 'Limited' in the name.")
if __name__ == "__main__":
asyncio.run(main())
Supported APIs 🌐
- Real-time Flood Monitoring (flood warnings, areas, stations, measures, readings)
- Bathing Waters
- Asset Management
- Hydrology
- Rainfall
- Water Quality Data Archive (WQA)
- Public Register (waste operations, end-of-life vehicles, industrial installations, water discharges, radioactive substances, waste carriers/brokers, waste exemptions, water discharge exemptions, scrap metal dealers, enforcement actions, flood risk exemptions)
Package Name vs Import Path 🔤
- Distribution (PyPI):
environment-client - Import path (Python):
environment
Example:
from environment.flood_monitoring import FloodClient
Note: The distribution is named environment-client while the import path is environment. This keeps imports concise but clarifies the project scope on PyPI.
⚠️ Important: WQA API Replacement
Note: The Water Quality Archive (WQA) APIs will be replaced later this year, meaning that the existing APIs will no longer work after Spring/Summer 2025. As of now, many water-quality/view endpoints return HTTP 404. We’ve:
- Added a
DeprecationWarningwhen instantiatingWaterQualityDataArchiveClient. - Marked WQA tests as
skippeduntil the replacement API is available.
For updates, see DEFRA’s support pages: https://environment.data.gov.uk/apiportal/support
Implementation Status 📊
-
Flood Monitoring
- Base:
https://environment.data.gov.uk/flood-monitoring - Implemented:
get_flood_warnings(/id/floods),get_flood_areas(/id/floodAreas),get_stations(/id/stations),get_station_by_id,get_measures(/id/measures),get_measure_by_id,get_readings(/data/readings),get_reading_by_id. - Notes: Uses canonical
/idfor entities and/datafor readings. Integration tests use VCR.
- Base:
-
Rainfall
- Base: Flood Monitoring (parameterised)
- Implemented: Stations and measures filtered with
parameter=rainfall; readings via/data/readings?parameter=rainfall; reading-by-id via/data/readings/{measure_id}/{timestamp}. - Notes: Rainfall is part of Flood Monitoring; not a separate base path.
-
Tide Gauge
- Base: Flood Monitoring (typed)
- Implemented: Stations via
/id/stations?type=TideGauge, station-by-id, readings via/data/readings?stationType=TideGauge, reading-by-id via/data/readings/{measure_id}/{timestamp}.
-
Hydrology
- Base:
https://environment.data.gov.uk/hydrology - Implemented: Stations, station-by-id, measures, measure-by-id, readings per-measure via
/id/measures/{id}/readings(lists do not expose a global/id/readings). - Notes: Some fields (e.g.,
status,riverName,station,unit) are normalised for model compatibility.
- Base:
-
Bathing Waters
- Base:
https://environment.data.gov.uk - Implemented:
get_bathing_waters(/doc/bathing-water.json), plus related entity lookups under/id/*.
- Base:
-
Asset Management
- Base:
https://environment.data.gov.uk/asset-management - Implemented: Assets, maintenance activities/tasks/plans, capital schemes (JSON endpoints under
/id/*.json).
- Base:
-
Catchment Planning (Catchment Data)
- Base:
https://environment.data.gov.uk/catchment-planning - Status: Placeholder only (
get_catchment_datareturns[]until the correct endpoint is confirmed).
- Base:
-
Water Quality Data Archive (WQA)
- Base:
https://environment.data.gov.uk/water-quality/view - Status: Being replaced by DEFRA; many endpoints currently return HTTP 404. Client issues a
DeprecationWarning. Tests are skipped until the replacement API is available.
- Base:
Testing & VCR 🧪
- Tests are recorded/replayed with
pytest-vcr(record mode: once). - Cassettes are stored under
tests/cassettes/with per-module subfolders (e.g.,rainfall/,hydrology/,tide_gauge/,integration/). - To re-record a cassette, delete the corresponding YAML file and re-run the specific test.
- Integration tests also use VCR to avoid live network dependency.
Development 🛠️
Contributing? See AGENTS.md for full repository guidelines (structure, style, testing, and PR conventions).
This project uses uv for dependency management.
- Install dependencies:
uv syncorjust install - Run tests:
just test - Run integration tests:
just test-integration - Lint (fix):
just lint - Format:
just format - Example script:
just run-main
Contributing 🤝
- Start with AGENTS.md for repository structure, coding style, testing, and PR conventions.
- Open an issue for larger changes; link issues in PRs.
- Follow commit prefixes (e.g.,
feat:,fix:,docs:) and keep messages concise. - Run
just lint,just format, andjust testbefore pushing. Update or re-record VCR cassettes when tests change network interactions.
Releasing 🚀
Automatic Releases (Default)
Every merge to main is released once its tests pass. Merge your PR and GitHub Actions:
- Runs the tests on the merge, and waits for them to pass
- Bumps the version: the patch (0.5.11 → 0.5.12), unless the PR title ends with
[minor]or[major] - Creates the git tag and GitHub release
- Publishes to PyPI via Trusted Publishing
There are no manual steps. A merge that changes only .github/, such as a Dependabot action update, releases nothing; the next release includes it.
Minor and Major Releases
For a minor or major release, end the PR title with [minor] or [major], for example Add hydrology readings endpoint [minor]. The squash-merge commit takes the PR title, and the workflow bumps that part instead of the patch (0.5.x → 0.6.0, or 0.5.x → 1.0.0).
Nobody can push a bump commit or a v* tag by hand: the repository rules reserve both for the release app.
How It Works
- Automatic workflow:
.github/workflows/auto-version.ymlbumps the version on every merge to main, then tags and releases as thecogna-package-releaserGitHub App - Publishing workflow:
.github/workflows/publish.ymlpublishes to PyPI when a release is created - Trusted Publishing: PyPI accepts uploads only from
publish.ymlrunning in thepypienvironment, which onlyv*tags can deploy to
Setup
The repository settings (rules and the release and pypi environments) are managed in Terraform in cogna-co/terraform. The release environment needs:
- variable
RELEASE_APP_CLIENT_ID: the app's client ID - secret
RELEASE_APP_PRIVATE_KEY: a private key for the app
The app's token, unlike GITHUB_TOKEN, lets the release trigger publish.yml.
Links:
- PyPI project: https://pypi.org/project/environment-client/
- Workflow setup guide:
.github/workflows/README.md
Metadata
Release files for environment-client 0.5.16
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| environment_client-0.5.16.tar.gz | 19.8 kB | With provenance | Details for environment_client-0.5.16.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| environment_client-0.5.16-py3-none-any.whl | Python 3 | none | any | Details for environment_client-0.5.16-py3-none-any.whl |
Total release size: 49.9 kB
Release files / environment_client-0.5.16.tar.gz
| Download URL | environment_client-0.5.16.tar.gz |
|---|---|
| Size | 19.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae32e4f0f30d1862fdc23498c43ddd74bba015679a87d497b9a03698a5fcde7f
|
|
BLAKE2b-256 checksum How to use checksums |
5ec35948420d1b62aa1746c8b8f5e39af4e306d0629c9d1fcac76dd8f0808c3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logRelease files / environment_client-0.5.16-py3-none-any.whl
| Download URL | environment_client-0.5.16-py3-none-any.whl |
|---|---|
| Size | 30.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cd4b7bffe54dfe7c8ff71900d4da465e76c14eb4088c03d4da9879ff21104d6e
|
|
BLAKE2b-256 checksum How to use checksums |
5b97ca9d3d0c59639d097a203c9540a5c92b81c17ff816488a427cb120cc04d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logSecurity for environment-client 0.5.16
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for environment_client-0.5.16.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 27, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.