Skip to main content

PostcodeInfo Client

Test result Coverage report Code Climate Requirements Status

Python package providing an API client for https://github.com/ministryofjustice/postcodeinfo which contains public sector information licensed under the Open Government License v2.0

Installation

pip install postcodeinfo

Usage

Authentication

You will need an authentication token (auth token). If you’re using MOJ DS’s Postcode Info server, you can get a token by emailing platforms@digital.justice.gov.uk with a brief summary of:

  • who you are

  • what project you’re going to be using it on

  • roughly how many lookups you expect to do per day

If you’re running your own server, see https://github.com/ministryofjustice/postcodeinfo#auth_tokens for instructions on how to create a token.

Quick Start

In your code:

>>> import postcodeinfo

>>> # create a client
>>> client = postcodeinfo.Client("YOUR-ACCESS-TOKEN")

>>> # lookup a postcode
>>> postcode = client.lookup_postcode("SW1A 1AA")

>>> # postcode details
>>> postcode.valid
True

>>> postcode.latitude
51.50100893654096

>>> postcode.longitude
-0.141587558526369

>>> postcode.local_authority
{
    'gss_code': 'E09000033',
    'name': 'Westminster'
}

>>> postcode.country
{
    "name": "England",
    "gss_code": "E92000001"
}

>>> postcode.addresses
[
    {
        "uprn": "10033544614",
        "organisation_name": "BUCKINGHAM PALACE",
        "department_name": "",
        "po_box_number": "",
        "building_name": "",
        "sub_building_name": "",
        "building_number": None,
        "thoroughfare_name": "",
        "dependent_thoroughfare_name": "",
        "dependent_locality": "",
        "double_dependent_locality": "",
        "post_town": "LONDON",
        "postcode": "SW1A 1AA",
        "postcode_type": "L",
        "formatted_address": "Buckingham Palace\nLondon\nSW1A 1AA",
        "point": {
            "type": "Point",
            "coordinates": [
                -0.141587558526369,
                51.50100893654096
            ]
        }
    }
]

Configuration

Apart from the auth token, there is only one other parameter the API client needs - api_url.

Explicit api_url

You can set the api_url explicitly by passing it to the Client constructor

# create a client
client = postcodeinfo.Client("YOUR-API-TOKEN", api_url="https://some.dom.ain")

or by setting it on an existing client, like this

client = postcodeinfo.Client("YOUR-API-TOKEN")
client.api_url = "https://some.dom.ain"

Implicit api_url

If you don’t pass an api_url to the constructor, it will attempt to infer one from the environment. The client has a built-in mapping of environment names to URLs.

>>> postcodeinfo.Client.api_urls
{
    'development': 'http://localhost:8000',
    'test': 'http://localhost:8000',
    'staging': 'https://postcodeinfo-staging.dsd.io',
    'production': 'https://postcodeinfo.service.justice.gov.uk'
}

It will use the following rules to infer the URL:

  1. If you pass an env parameter to the constructor (eg: client = postcodeinfo.Client("YOUR-API-TOKEN", env="staging")), it will use that as a reference into the api_urls mapping.

  2. If you have DJANGO_SETTINGS_MODULE set in your environment, it will try to find the following settings in that module:

    POSTCODEINFO_API_URL
    POSTCODEINFO_API_TOKEN
    POSTCODEINFO_API_TIMEOUT
  3. Otherwise it will default to development

Support

This source code is provided as-is, with no incident response or support levels. Please log all questions, issues, and feature requests in the Github issue tracker for this repo, and we’ll take a look as soon as we can. If you’re reporting a bug, then it really helps if you can provide the smallest possible bit of code that reproduces the issue. A failing test is even better!

Contributing

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed

  • Check the issue tracker to make sure someone hasn’t already requested and/or contributed the feature

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure your changes are covered by unit tests, so that we don’t break it unintentionally in the future.

  • Please don’t mess with setup.py, version or history.

Metadata

Release files for postcodeinfo 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for postcodeinfo 0.2.2
File Size Uploaded Provenance Actions
postcodeinfo-0.2.2.tar.gz 6.7 kB Without provenance Details for postcodeinfo-0.2.2.tar.gz

Release files / postcodeinfo-0.2.2.tar.gz

Download URL postcodeinfo-0.2.2.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c84e735bea0ae7aeecdb0a79843edd56bba2461590f0b2d1e4cb4ec208bd42ca
BLAKE2b-256 checksum
How to use checksums
b2d79e1007afb838f29ff941c14bd70a2118eabae2c919309eb43f1db09c4fdf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for postcodeinfo 0.2.2

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page