Skip to content
oslew
Get a free key

APIs

Postcode lookup API

A postcode lookup API turns a postal code into a position and a description of the area it covers. oslew returns, for each code, the mean position of the addresses that carry it, the box those addresses span, how many there are and the administrative areas around its middle, and it can list the codes that begin with a prefix while someone types. It is built from address records, not from a published boundary, so there is no polygon; the postcode rows are licensed CDLA-Permissive-2.0, while the administrative chain beside them is ODbL.

Last reviewed

A postcode is a delivery convention, not a shape. Postal services draw them to organise rounds, and most never publish an outline that anyone may reuse. What does exist, openly, is the set of addresses that carry each code — and a summary of those addresses is what a postcode lookup can honestly return. GET /v1/postcode is built that way, and every field in its answer says which part of the summary it is.

What comes back for a code

Send code, and optionally country as an ISO code, prefix and limit. Each result describes one country’s use of one code:

Field What it is
lat, lon The mean of the code’s address points: where its addresses are.
bbox The extent of those points, as latitude and longitude bounds.
addresses How many address points the row was built from.
precision Always postcode.
precision_m Half the diagonal of the box: how large an area the centre stands for.
locality, region What most of the code’s addresses recorded, or empty where they recorded nothing.
admin Every administrative division whose polygon contains the centre, outermost first, time zone included.
country The ISO country code the rows carry.

The answer’s provenance names the layer, its release and its licence code, CDLA-Permissive-2.0. That is permissive and carries no share-alike, so a table of postcode centres may sit in your own database beside your records. The admin chain does not share those terms. Each of its entries carries its own provenance, and its divisions and time-zone entry are ODbL, so a table that keeps the chain beside the centres carries share-alike once you share it, as the licensing page sets out.

A centre of addresses, not an official point

The position is the mean of the addresses because that is the one thing the data can support. It answers “where are this code’s addresses” rather than “where is the middle of a polygon”, which for a code shaped like a crescent, or one with a large park inside it, can be somewhere nobody lives. It is not an official centroid, it does not claim to be one, and it moves when new address data arrives.

The accompanying precision_m is not the error of a point. The centre is stored to a fine resolution that would suggest a position good to a hand’s width, but what it names is the middle of an area, and the size of the area is the only honest error bar. A dense city code covers a few hundred metres; a rural one can cover kilometres. Read addresses beside it: a centre computed from a handful of points is a different object from one computed from thousands, and the count lets you tell them apart without guessing.

The admin chain is computed at that centre. A code that straddles a municipal boundary reports whichever side its middle falls on, so if the split matters to you, compare the box with the division. For the containment rules themselves, see the boundaries API.

Spelling, countries and prefixes

Postcodes are written many ways, and none of the variations carry meaning, so spaces and hyphens are removed and letters upper-cased before the lookup: SW1A 1AA, sw1a1aa and SW1A-1AA are one code. The result writes the code the way its source did, and echoes what you sent as query.

Many formats — five digits especially — are used by more than one country. Without country, each country’s row for the code comes back separately; with it, only that country’s can. country is matched only as an ISO code: a country’s name matches nothing.

With prefix=true, code is the start of a code, and the answer is every code that begins with it, in code order, capped by limit at 50 results. A one- or two-character prefix matches a long run of codes and you get the first ones in order, so for a postcode field, start asking once a few characters are in.

A code the data does not hold is not an error: the response is a success with an empty list. Whether a code is held depends on the address data loaded for its country; the released store covers ⟦pending:coverage.postcodes.count⟧ postcodes.

Why the postcode is the context that matters

A postcode is the most useful thing an address can carry into a geocoder, and this route is the natural companion to that fact. Address records are indexed by locality, postcode and country, so a postcode sent as context narrows the search to a small area before any candidate is scored, where a city name leaves every same-named street in a metropolis in contention. The difference is large enough that the accuracy page is built around it, with every measured row published.

Geocoding has no postcode parameter; the code goes after the last comma of the query. Where the two routes meet is in checking one against the other: a geocoded address whose normalised postcode differs from the code the customer typed, or which lies well outside that code’s box, deserves a second look. Places — museums, shops — are not indexed by postcode, so for a named place the city is the better context.

Putting a lookup to work

  • Checking a typed postcode exists before an address form accepts it, and suggesting codes as it is typed with the prefix mode.
  • Filling in the rest of the form — the borough, the state — from the admin chain, so the person types less and spells less.
  • Centring a map on a code, using the box to pick a zoom level that shows all of it.
  • Aggregating by postal area without drawing polygons: the centre and count are enough for a dot map or a coverage table.
  • Catching mismatches between a geocoded position and the code the customer gave, as above, or between a postcode and the town entered beside it.

Validation compares a postcode against a single matched address rather than summarising the code; the address validation API is the route for “is this whole address right”.

What it costs

Postcode lookup is part of the Address product, on every plan including Free at 1 requests a second sustained, burst 5. The Address plan raises that to 20 requests a second sustained, burst 100 for $99 a month. Each lookup, prefix or exact, takes one token from the key’s address bucket, shared with geocoding and validation; there is no monthly count to exhaust.

What it is not

  • Not a boundary. No polygon is returned by this or any other route.
  • Not an official centroid, and not stable across data releases.
  • Not a list of addresses. addresses is a count; to find the addresses themselves, geocode them with the code as context.
  • Not complete where address data is thin. A code with no address records behind it does not exist here, even if the postal service issued it.
  • Not private from the log when used as geocoding context. The text after a query’s last comma is kept normalised but not masked, so a postcode sent as context is recorded as sent.

Bottom line

Use postcode lookup to check, complete and locate postal codes, and to summarise data by postal area without licensing a boundary file. Read the centre as “where the addresses are”, read precision_m and addresses as how much that centre stands for, and send the postcode as context whenever you geocode an address that has one.

Questions

Does the postcode lookup return the postcode boundary?

No. There is no polygon and no route that returns one. Authoritative postcode outlines are national postal products — licensed, partial or never published — and an outline drawn around a cloud of address points would claim more than the data knows. The answer gives the extent of the addresses as a box instead, and the centre of those addresses.

Where exactly is a postcode’s centroid?

At the mean position of the address points that carry the code: where its addresses are, rather than the middle of a shape. It is not an official centroid published by a postal operator, it does not claim to be, and it moves when the address data behind it is updated.

Can I use it to autocomplete a postcode field?

Yes. With prefix=true the code is treated as the beginning of a code, and the answer lists the codes that start with it, in code order, up to the limit you ask for. It is a short walk through a sorted table, cheap enough to run as each character is typed.

What happens when two countries use the same postcode?

Without a country, the row for that code from each country that uses it comes back as its own result, rather than the service guessing which you meant. Send the ISO country code to get only that country’s row, or read the country on each result and let the person choose.

Is a postcode better than a city for geocoding an address?

Much better. Measured on Mexico City held-out numbered addresses, a query carrying the postcode lands a median 19 m from the true point, against 162 m with only the city. The postcode names a small area directly; a city leaves every same-named street in it in play.

Browse every page →