Postcodes
GET /v1/postcode answers what a postcode covers: where its addresses are, how far they spread, how many there are, and which administrative areas hold their middle. It is built from the addresses that carry each code, not from a published boundary, and every field says so in its own way.
curl -G "https://api.oslew.com/v1/postcode" \
-H "Authorization: Bearer $GEO_API_KEY" \
--data-urlencode 'code=03940' \
--data-urlencode 'country=MX'from oslew import Client
# The key is read from GEO_API_KEY.
client = Client(base_url="https://api.oslew.com")
answer = client.postcode("03940", country="MX")import { Client } from "@oslew/client";
// The key is read from GEO_API_KEY.
const client = new Client({ baseUrl: "https://api.oslew.com" });
const answer = await client.postcode("03940", { country: "MX" });HTTP/1.1 200 OK
content-type: application/json
ratelimit-limit: 100
ratelimit-remaining: 298
ratelimit-reset: 1
{
"query": "03940",
"results": [
{
"code": "03940",
"country": "MX",
"lat": 19.365164,
"lon": -99.180155,
"bbox": [
19.362674,
19.367267,
-99.182424,
-99.178061
],
"addresses": 271,
"locality": null,
"region": "Ciudad de México",
"precision_m": 342.89968972092777,
"precision": "postcode",
"admin": [
{
"doc": 2174,
"id": "7f1f3634-1218-43e7-9538-ac3d1f43fc1c",
"name": "México",
"subtype": "country",
"country": "MX",
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 61,
"id": "0831989a-c65b-47db-802e-f930af64b9e4",
"name": "México",
"subtype": "country",
"country": "MX",
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "overture-divisions-maritime",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1560,
"id": "bc8dbc34-544b-47c4-bdb1-d3f5ecfc6af7",
"name": "Ciudad de México",
"subtype": "region",
"country": "MX",
"region": "MX-CMX",
"admin_level": 1,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1621,
"id": "57c3f700-1244-4858-b97d-0980a217b02e",
"name": "Benito Juárez",
"subtype": "county",
"country": "MX",
"region": "MX-CMX",
"admin_level": 2,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1522,
"id": "2b1d49e2-98ef-4665-b9f6-1ce96c9f015b",
"name": "Ciudad de México",
"subtype": "locality",
"country": "MX",
"region": "MX-CMX",
"admin_level": null,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 147,
"id": "America/Mexico_City",
"name": "America/Mexico_City",
"subtype": "timezone",
"country": null,
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "timezone-boundary-builder",
"release": "2026d",
"license": "ODbL-1.0"
}
}
]
}
],
"layers": 1,
"provenance": {
"name": "overture-addresses",
"release": "postcodes",
"license": "CDLA-Permissive-2.0"
},
"took_us": 468
}A real response, captured from the service at commit b8f4bde-dirty. Timing fields such as took_us differ on every call.
Parameters
| Parameter | Default | What it does |
|---|---|---|
code | required | The postcode, in any spelling. Blank is 400, code is required. |
country | none | An ISO 3166-1 code, MX, in either case. Narrows to that country's code. A country name matches nothing. |
prefix | false | Treat code as the beginning of a code and return the codes that start with it. |
limit | 10 | Results at most, clamped to 1–50. |
Any spelling means spaces and hyphens are removed and letters upper-cased before the lookup, since they only ever group characters for a human reader: SW1A 1AA, sw1a1aa andSW1A-1AA are one code. The answer's code is written the way the source recorded it, andquery is what you sent.
A code the data does not hold is not an error: the answer is 200 with an empty results. Whether a code is held depends on the address data loaded for its country — a code with no addresses behind it does not exist here.
What a result says
| Field | Is |
|---|---|
lat, lon | The mean of the code's address points — where its addresses are. |
bbox | The extent of those points, [lat_lo, lat_hi, lng_lo, lng_hi]. |
addresses | How many address points the row was built from. |
precision | Always postcode. |
precision_m | Half the diagonal of bbox, in metres: how large an area the centroid stands for. |
locality, region | The locality and region most of those addresses recorded, or null where they recorded none. |
admin | Every administrative division whose polygon contains the centroid, outermost first. |
country | The ISO country code the rows carry. |
At the top of the answer, provenance names the layer the rows came from, its release and its licence code, and layers says how many postcode layers were consulted. Each entry in admincarries its own provenance.
The centroid is where the addresses are
A postcode is a delivery convention, not a shape. The authoritative outlines are national postal products — licensed, partial, or never published — and drawing an outline around a cloud of points would be a claim the data cannot support. What the addresses do support is a summary of themselves. So the position is their mean: where the addresses are, rather than where the middle of some polygon happens to fall. It is not an official centroid, it does not claim to be one, and it moves when the address data does.
precision_m is the size of the area, not the error of a point
The centroid is stored to a millionth of a degree, which on its own would read as a position good to ten centimetres. What it names is the middle of an area, and the area's own size is the only honest error bar for it. In the answer above, 271 addresses spread over a box about 510 m by 460 m, so precision_m is 343 m — and Avenida Insurgentes Sur 1602, which carries the code, is about 230 m from the centroid. A dense urban code is a few hundred metres; a rural one is kilometres. Read addresses too: a centroid of four addresses is a different object from one of four thousand, and the count lets you tell without asking.
locality and region, or admin
locality and region are whatever most of the code's addresses recorded, and some sources record less than others. Mexico's address rows carry no locality of their own, so the answer above haslocality: null and the state as region. admin is where the complete chain comes from: the divisions containing the centroid — here the county Benito Juárez, the locality Ciudad de México, the region MX-CMX, and the time zone. It is the same chainreverse geocoding returns for a point.
It is the chain at the centroid. A code that straddles a boundary reports the side its middle falls on; compare the bbox with the division if that matters to you.
Codes shared across countries
Five-digit codes and many other formats are used by more than one country. Without country, every country's row for the code comes back, one result each, rather than the service guessing which you meant. That is the honest answer, and it is the one to handle: either send country, or read each result'scountry and let the user choose. With country, only that country's row can come back.
Prefix lookup
With prefix=true, code is the beginning of a code, and the answer is every code that starts with it, in code order, up to limit. It is a short walk through a sorted table, so it is fast enough to run on each keystroke of a postcode field:
curl -G "https://api.oslew.com/v1/postcode" \
-H "Authorization: Bearer $GEO_API_KEY" \
--data-urlencode 'code=0394' \
--data-urlencode 'country=MX' \
--data-urlencode 'prefix=true' \
--data-urlencode 'limit=3'from oslew import Client
# The key is read from GEO_API_KEY.
client = Client(base_url="https://api.oslew.com")
answer = client.postcode("0394", country="MX", prefix=True, limit=3)import { Client } from "@oslew/client";
// The key is read from GEO_API_KEY.
const client = new Client({ baseUrl: "https://api.oslew.com" });
const answer = await client.postcode("0394", { country: "MX", prefix: true, limit: 3 });HTTP/1.1 200 OK
content-type: application/json
ratelimit-limit: 100
ratelimit-remaining: 297
ratelimit-reset: 1
{
"query": "0394",
"results": [
{
"code": "03940",
"country": "MX",
"lat": 19.365164,
"lon": -99.180155,
"bbox": [
19.362674,
19.367267,
-99.182424,
-99.178061
],
"addresses": 271,
"locality": null,
"region": "Ciudad de México",
"precision_m": 342.89968972092777,
"precision": "postcode",
"admin": [
{
"doc": 2174,
"id": "7f1f3634-1218-43e7-9538-ac3d1f43fc1c",
"name": "México",
"subtype": "country",
"country": "MX",
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 61,
"id": "0831989a-c65b-47db-802e-f930af64b9e4",
"name": "México",
"subtype": "country",
"country": "MX",
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "overture-divisions-maritime",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1560,
"id": "bc8dbc34-544b-47c4-bdb1-d3f5ecfc6af7",
"name": "Ciudad de México",
"subtype": "region",
"country": "MX",
"region": "MX-CMX",
"admin_level": 1,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1621,
"id": "57c3f700-1244-4858-b97d-0980a217b02e",
"name": "Benito Juárez",
"subtype": "county",
"country": "MX",
"region": "MX-CMX",
"admin_level": 2,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 1522,
"id": "2b1d49e2-98ef-4665-b9f6-1ce96c9f015b",
"name": "Ciudad de México",
"subtype": "locality",
"country": "MX",
"region": "MX-CMX",
"admin_level": null,
"via": "boundary",
"provenance": {
"name": "overture-divisions",
"release": "2026-08-19.0",
"license": "ODbL-1.0"
}
},
{
"doc": 147,
"id": "America/Mexico_City",
"name": "America/Mexico_City",
"subtype": "timezone",
"country": null,
"region": null,
"admin_level": null,
"via": "interior",
"provenance": {
"name": "timezone-boundary-builder",
"release": "2026d",
"license": "ODbL-1.0"
}
}
]
}
],
"layers": 1,
"provenance": {
"name": "overture-addresses",
"release": "postcodes",
"license": "CDLA-Permissive-2.0"
},
"took_us": 96
}A real response, captured from the service at commit b8f4bde-dirty. Timing fields such as took_us differ on every call.
Asked for up to three, it found one: in the data loaded, 03940 is the only code that begins with0394. The prefix is on the normalised spelling, so sw1a and SW1A are the same prefix. A one- or two-character prefix matches a large run of codes; limit is what keeps the answer short, and the first limit codes in order are the ones you get.
A postcode as geocoding context
The biggest single improvement to geocoding accuracy is sending the postcode with the street — as the text after the last comma of q: Avenida Insurgentes Sur 1602, 03940. There is no postcode parameter on/v1/geocode. Measured on held-out addresses, it took the median error from 673 m with a city to 93 m, and to 19 m with interpolation; getting better accuracy has the numbers and the right and wrong ways to send it.
This route and that context are complementary:
- Use
/v1/postcodeto check a postcode someone typed exists, to fill in the borough and state for it, or to centre a map on it. - Use the postcode as geocoding context to place an address inside it.
- Compare the two to catch a mismatch: a geocoded address whose
normalized.postcodediffers from the one the customer typed, or that lies well outside that code'sbbox, is worth a second look.
from oslew import Client
client = Client(base_url="https://api.oslew.com")
def postcode_fields(code: str, country: str = "MX"):
found = client.postcode(code, country=country)["results"]
if not found:
return None # not a code the data holds
pc = found[0]
chain = {d["subtype"]: d["name"] for d in pc["admin"]}
return {
"code": pc["code"],
"county": chain.get("county"), # the borough, for Mexico City
"locality": pc["locality"] or chain.get("locality"),
"region": chain.get("region"),
"centre": (pc["lat"], pc["lon"]),
"radius_m": pc["precision_m"],
}import { Client } from "@oslew/client";
const client = new Client({ baseUrl: "https://api.oslew.com" });
async function postcodeFields(code: string, country = "MX") {
const { results } = await client.postcode(code, { country });
if (results.length === 0) return null; // not a code the data holds
const pc = results[0];
const chain = Object.fromEntries(pc.admin.map((d) => [d.subtype, d.name]));
return {
code: pc.code,
county: chain.county ?? null, // the borough, for Mexico City
locality: pc.locality ?? chain.locality ?? null,
region: chain.region ?? null,
centre: [pc.lat, pc.lon],
radiusM: pc.precision_m,
};
}Building a map of subtype to name keeps the last division of each kind; where the chain can hold two of one kind — the country from two layers, as it does here — choose by provenance.name if it matters which.
Errors
400,code is required—codewas blank.400,no postcode layer is loaded— the deployment has no postcode data at all.- A missing
codeparameter is refused by the framework as plain text; alimitthat is not a number likewise. See errors.
Endpoints
/v1/postcodeaddressA postcode: the centroid of its addresses, their extent, how many there are, and the administrative chain it sits in.