Python – UK Postcode Lookup Example

Here is an example of a Python function that uses our UK Postcode Lookup JSON API to return address data for a postcode.

Note

This example requires the Requests library.

import requests

def do_lookup(postcode):
	url = "https://pcls1.craftyclicks.co.uk/json/rapidaddress/?"

	payload = {
		'postcode': postcode,
		'key': 'xxxxx-xxxxx-xxxxx-xxxxx',
		'lines': 2,
		'response': 'data_formatted'
	}

	r = requests.get(url, params=payload)
	data = r.json()
	return data

This can be tested using one of our free test postcodes.

print(do_lookup('AA1 1AA'))

This will produce the following result:

{
    "delivery_points": [
        {
            "organisation_name": "THE BAKERY",
            "department_name": "",
            "line_1": "1 HIGH STREET",
            "line_2": "CRAFTY VALLEY",
            "udprn": "12345678",
            "dps": "1A"
        },
        {
            "organisation_name": "FILMS R US",
            "department_name": "",
            "line_1": "3 HIGH STREET",
            "line_2": "CRAFTY VALLEY",
            "udprn": "12345679",
            "dps": "1B"
        },
        {
            "organisation_name": "FAMILY BUTCHER",
            "department_name": "",
            "line_1": "7 HIGH STREET",
            "line_2": "CRAFTY VALLEY",
            "udprn": "12345680",
            "dps": "1C"
        },
        {
            "organisation_name": "",
            "department_name": "",
            "line_1": "BIG HOUSE, HIGH STREET",
            "line_2": "CRAFTY VALLEY",
            "udprn": "12345681",
            "dps": "1D"
        },
        {
            "organisation_name": "",
            "department_name": "",
            "line_1": "LITTLE COTTAGE",
            "line_2": "17 HIGH STREET, CRAFTY VALLEY",
            "udprn": "12345682",
            "dps": "2A"
        }
    ],
    "delivery_point_count": 5,
    "postal_county": "POSTAL COUNTY",
    "traditional_county": "TRADITIONAL COUNTY",
    "town": "BIG CITY",
    "postcode": "AA1 1AA"
}

Let us know if you run into any issues while integrating our address finder into your application – Contact usopen in new window.