Using Python to look up an address from postcode is easy.

The sample code below shows how to retrieve address test data from our API. Copy & paste it into your existing project and play around with the result object.

Here are the steps you need to do to get on track :

First, let’s import some libs. We going to use urllib for encoding, urllib2 to retrieve the response itself. We going to make the request from our JSON API, so we will also include json for decoding. You might choose to use pyCurl instead of urllib2.

import urllib2
import urllib
import json

Set the API url

url = "https://pcls1.craftyclicks.co.uk/json/"

Specify an endpoint (rapidaddress -> UK Postcode Lookup)

url += 'rapidaddress'

Add the parameters that we are going to send to the API. Postcode can have spaces and can be upper or lower case.

params = {'postcode': 'aa11aa', 'key': 'xxxxx-xxxxx-xxxxx-xxxxx', 'response': 'data_formatted', 'lines': '1'}

Please read our JSON API documentation to see further parameters and their functions.

Let’s construct the final url string and create a request

req = urllib2.Request(url + '?' + urllib.urlencode(params))

Get the address data from our API, and parse the result via the json lib.

res = urllib2.urlopen(req)
data = json.loads(res.read())

That’s it, we are done! All the address data is available in the data variable, as an associative array. The postcode is also nicely formatted for display.

In case you are running in terminal, make a quick dump

print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

Which should yield the following

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

 

Can’t get shorter than this!

import urllib2
import urllib
import json

url = "https://pcls1.craftyclicks.co.uk/json/"

url += 'rapidaddress'
params = {'postcode': 'aa11aa', 'key': 'xxxxx-xxxxx-xxxxx-xxxxx', 'response': 'data_formatted', 'lines': '1'}

req = urllib2.Request(url + '?' + urllib.urlencode(params))

res = urllib2.urlopen(req)
data = json.loads(res.read())

print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

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