clearpostcode 1.0.0__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {clearpostcode-1.0.0 → clearpostcode-2.0.0}/.gitignore +1 -0
- clearpostcode-2.0.0/PKG-INFO +223 -0
- clearpostcode-2.0.0/README.md +197 -0
- clearpostcode-2.0.0/pyproject.toml +60 -0
- clearpostcode-2.0.0/src/clearpostcode/__init__.py +52 -0
- clearpostcode-2.0.0/src/clearpostcode/_async_client.py +295 -0
- clearpostcode-2.0.0/src/clearpostcode/_base.py +282 -0
- clearpostcode-2.0.0/src/clearpostcode/_calls.py +140 -0
- clearpostcode-2.0.0/src/clearpostcode/_client.py +291 -0
- clearpostcode-2.0.0/src/clearpostcode/_errors.py +106 -0
- clearpostcode-2.0.0/src/clearpostcode/_generated.py +582 -0
- clearpostcode-2.0.0/src/clearpostcode/_version.py +1 -0
- clearpostcode-1.0.0/PKG-INFO +0 -78
- clearpostcode-1.0.0/README.md +0 -58
- clearpostcode-1.0.0/pyproject.toml +0 -49
- clearpostcode-1.0.0/smoke.py +0 -20
- clearpostcode-1.0.0/src/clearpostcode/__init__.py +0 -142
- clearpostcode-1.0.0/tests/test_client.py +0 -136
- clearpostcode-1.0.0/uv.lock +0 -363
- {clearpostcode-1.0.0 → clearpostcode-2.0.0}/LICENSE +0 -0
- {clearpostcode-1.0.0 → clearpostcode-2.0.0}/src/clearpostcode/py.typed +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: clearpostcode
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: UK postcode lookup API client: validate, autocomplete, nearest, reverse geocode, distance, council, ward and constituency, plus Royal Mail address lookup. Typed, sync and async, retries built in.
|
|
5
|
+
Project-URL: Homepage, https://clearpostcode.co.uk/developers
|
|
6
|
+
Project-URL: Documentation, https://clearpostcode.co.uk/developers/docs
|
|
7
|
+
Project-URL: Repository, https://github.com/clearpostcode/postcode-api
|
|
8
|
+
Project-URL: Changelog, https://github.com/clearpostcode/postcode-api/blob/main/CHANGELOG.md
|
|
9
|
+
Author-email: ClearPostcode <hello@clearpostcode.co.uk>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: address lookup,constituency,council,geocoding,postcode,postcode api,postcode autocomplete,postcode lookup,postcode validation,reverse geocode,royal mail paf,uk postcode,ward
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
22
|
+
Classifier: Typing :: Typed
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Requires-Dist: httpx<1,>=0.27
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# clearpostcode
|
|
28
|
+
|
|
29
|
+
Every UK postcode, answered in milliseconds: look up, validate and autocomplete postcodes, with the council, ward,
|
|
30
|
+
constituency, coordinates, police force, NHS area and deprivation decile behind each one. Find the nearest postcodes,
|
|
31
|
+
reverse geocode a point, measure distances, list a district, search places, and look up full Royal Mail addresses.
|
|
32
|
+
|
|
33
|
+
- **Typed end to end:** every answer is a `TypedDict` generated from the API's OpenAPI document, every error a class,
|
|
34
|
+
and a full lookup is a `PostcodeRecord` with every field. mypy strict and pyright check each one.
|
|
35
|
+
- **Sync and async:** `ClearPostcode` and `AsyncClearPostcode`, on httpx, with context managers.
|
|
36
|
+
- **Built for production:** automatic retries with backoff that honour `Retry-After` and the rate-limit headers,
|
|
37
|
+
timeouts, cursor pagination and bulk calls of any size.
|
|
38
|
+
- **Free to start:** 10,000 postcode lookups a month on a free key, no card, and many calls work with no key at all.
|
|
39
|
+
|
|
40
|
+
Two products, one key: **Postcode data** (every UK postcode and its areas, from open data) and **Address lookup**
|
|
41
|
+
(every address at a postcode, from Royal Mail PAF). Python 3.11 or later.
|
|
42
|
+
|
|
43
|
+
## Install
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
uv add clearpostcode # or: pip install clearpostcode
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Quickstart
|
|
50
|
+
|
|
51
|
+
Runs as printed, with or without a key. The client reads your key from `CLEAR_POSTCODE_KEY` when it is set; without
|
|
52
|
+
one, calls run on the free keyless allowance.
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from clearpostcode import ClearPostcode
|
|
56
|
+
|
|
57
|
+
with ClearPostcode() as cp:
|
|
58
|
+
pc = cp.postcodes.lookup("SW1A 1AA").data
|
|
59
|
+
council, seat = pc["local_authority"], pc["constituency"]
|
|
60
|
+
print(council and council["name"], seat and seat["name"])
|
|
61
|
+
# Westminster Cities of London and Westminster
|
|
62
|
+
|
|
63
|
+
check = cp.postcodes.validate("m1 1ae").data
|
|
64
|
+
print(check["valid"], check["postcode"]) # True M1 1AE
|
|
65
|
+
|
|
66
|
+
print(len(cp.postcodes.autocomplete("SW1A 1").data) > 0) # True
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Get a free key in under a minute at [clearpostcode.co.uk/developers](https://clearpostcode.co.uk/developers#key):
|
|
70
|
+
secret keys start `cp_live_`.
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
export CLEAR_POSTCODE_KEY=cp_live_...
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## What you can call
|
|
77
|
+
|
|
78
|
+
| Call | What it answers |
|
|
79
|
+
|---|---|
|
|
80
|
+
| `postcodes.lookup(postcode, fields=[...])` | The full record: status, coordinates, council, ward, constituency and every other area |
|
|
81
|
+
| `postcodes.validate(postcode)` | Is the format valid, is it in the current edition, is it live |
|
|
82
|
+
| `postcodes.autocomplete(q, limit=)` | Postcodes (or districts) that start with what was typed. Free |
|
|
83
|
+
| `postcodes.nearest(postcode, radius=, limit=)` | Live postcodes nearest a postcode, with distances |
|
|
84
|
+
| `postcodes.reverse(latitude, longitude, radius=, limit=)` | Live postcodes nearest a point |
|
|
85
|
+
| `postcodes.distance(from_, to)` | Kilometres, miles and the bearing between two postcodes |
|
|
86
|
+
| `postcodes.bulk(postcodes, fields=[...])` | Any number of postcodes, sent 100 a call. Secret key |
|
|
87
|
+
| `postcodes.bulk_reverse(points)` | Nearest postcodes for any number of points, 20 a call. Secret key |
|
|
88
|
+
| `outcodes.get(outcode)` | A district: counts, sectors, centre, councils, wards, places |
|
|
89
|
+
| `outcodes.postcodes(outcode)` / `outcodes.all_postcodes(outcode)` | Every live postcode in a district, by page or all of them. Secret key |
|
|
90
|
+
| `sectors.get(sector)` · `areas.get(area)` | A sector or an area |
|
|
91
|
+
| `places.search(q)` | Cities, towns, villages and neighbourhoods by name |
|
|
92
|
+
| `account.usage()` | Your plan, allowance, lookups used and reset date. Secret key |
|
|
93
|
+
| `addresses.suggest`, `retrieve`, `by_postcode`, `all_by_postcode`, `by_udprn` | Address lookup (below) |
|
|
94
|
+
|
|
95
|
+
Every call answers a frozen `Result`: `data` and `meta` as the API sends them, plus `status`, `request_id` and
|
|
96
|
+
`rate_limit` (`policy`, `limit`, `remaining`, `reset`). Every field is documented at
|
|
97
|
+
[clearpostcode.co.uk/developers/docs](https://clearpostcode.co.uk/developers/docs).
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
from clearpostcode import ClearPostcode
|
|
101
|
+
|
|
102
|
+
with ClearPostcode() as cp:
|
|
103
|
+
r = cp.postcodes.lookup("EH1 1YZ", fields=["location", "ward"])
|
|
104
|
+
ward, where = r.data.get("ward"), r.data.get("location")
|
|
105
|
+
print(ward and ward["name"], where and where["latitude"]) # City Centre 55.950328
|
|
106
|
+
print(r.request_id is not None, r.rate_limit is not None) # True True
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Async
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
import asyncio
|
|
113
|
+
|
|
114
|
+
from clearpostcode import AsyncClearPostcode
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
async def main() -> None:
|
|
118
|
+
async with AsyncClearPostcode() as cp:
|
|
119
|
+
a, b = await asyncio.gather(cp.postcodes.lookup("M1 1AE"), cp.postcodes.lookup("CF10 1EP"))
|
|
120
|
+
councils = [r.data["local_authority"] for r in (a, b)]
|
|
121
|
+
print(*(c["name"] for c in councils if c)) # Manchester Cardiff
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
asyncio.run(main())
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Errors
|
|
128
|
+
|
|
129
|
+
Every error is a `ClearPostcodeError`. When the API answers with an error, you get an `APIError` subclass carrying
|
|
130
|
+
the problem's `code`, `detail`, `status` and the `request_id` to quote to support:
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
from clearpostcode import ClearPostcode, NotFoundError
|
|
134
|
+
|
|
135
|
+
with ClearPostcode() as cp:
|
|
136
|
+
try:
|
|
137
|
+
cp.postcodes.lookup("SW1A 9ZZ")
|
|
138
|
+
except NotFoundError as e:
|
|
139
|
+
print(e.code) # postcode_not_found
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
| Class | Status | Codes |
|
|
143
|
+
|---|---|---|
|
|
144
|
+
| `BadRequestError` | 400, 413 | `invalid_postcode`, `unknown_parameter`, `bad_request`, `secret_key_in_url`, `body_too_large` |
|
|
145
|
+
| `AuthenticationError` | 401 | `invalid_key`, `key_required` |
|
|
146
|
+
| `QuotaExceededError` | 402 | `quota_exceeded`, `address_credits_exhausted` |
|
|
147
|
+
| `PermissionDeniedError` | 403 | `origin_not_allowed`, `publishable_key_not_allowed`, `secret_key_in_browser`, `address_lookup_not_enabled` |
|
|
148
|
+
| `NotFoundError` | 404 | `postcode_not_found`, `not_covered`, `not_found` |
|
|
149
|
+
| `UnprocessableEntityError` | 422 | `no_position` |
|
|
150
|
+
| `RateLimitError` | 429 | `rate_limited`, `daily_cap_reached`, `suggestions_capped` |
|
|
151
|
+
| `InternalServerError` | 5xx | `server_error`, `service_unavailable` |
|
|
152
|
+
| `APIConnectionError`, `APITimeoutError` | | The API could not be reached, or did not answer in time |
|
|
153
|
+
|
|
154
|
+
## Retries and timeouts
|
|
155
|
+
|
|
156
|
+
429s, 5xx answers, timeouts and dropped connections are retried twice by default, with exponential backoff and
|
|
157
|
+
jitter. A `Retry-After` from the API is honoured exactly, and a 429 without one waits for the `RateLimit` reset. A
|
|
158
|
+
wait longer than `max_retry_delay` (a key's daily cap until midnight, say) is raised at once, with `retry_after` in
|
|
159
|
+
seconds.
|
|
160
|
+
|
|
161
|
+
```python
|
|
162
|
+
from clearpostcode import ClearPostcode
|
|
163
|
+
|
|
164
|
+
with ClearPostcode(timeout=5.0, max_retries=3) as cp:
|
|
165
|
+
print(cp.with_options(timeout=2.0).postcodes.validate("SW1A 1AA").data["valid"]) # True
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## Pages and bulk calls
|
|
169
|
+
|
|
170
|
+
```python secret
|
|
171
|
+
from clearpostcode import ClearPostcode
|
|
172
|
+
|
|
173
|
+
with ClearPostcode() as cp: # a secret key in CLEAR_POSTCODE_KEY
|
|
174
|
+
print(sum(1 for _ in cp.outcodes.all_postcodes("SW1A")) > 100) # True: every live postcode, page after page
|
|
175
|
+
|
|
176
|
+
r = cp.postcodes.bulk(["SW1A 1AA", "M1 1AE", "EH1 1YZ"], fields=["local_authority"])
|
|
177
|
+
print(r.meta["found"] if r.meta else 0) # 3
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Bulk calls take lists of any length: the client sends 100 postcodes (or 20 points) a call, one after another, and
|
|
181
|
+
joins the answers in the order you sent them. If a chunk fails part-way (the month's lookups run out, say), the error
|
|
182
|
+
carries `partial`: everything the chunks before it answered, so nothing you paid for is lost. Bulk calls are retried
|
|
183
|
+
only on answers that say nothing was billed (429 and 503).
|
|
184
|
+
|
|
185
|
+
## Address lookup
|
|
186
|
+
|
|
187
|
+
Full UK address lookup on Royal Mail's Postcode Address File: the customer types a postcode or the start of an
|
|
188
|
+
address, picks it from the list, and you save a clean Royal Mail address. It is in early access and switches on per
|
|
189
|
+
account: once the business your key serves has Address lookup switched on in your
|
|
190
|
+
[dashboard](https://clearpostcode.co.uk/dashboard#address-lookup), every call below answers. Until then they raise
|
|
191
|
+
`PermissionDeniedError` with code `address_lookup_not_enabled`. Join at
|
|
192
|
+
[clearpostcode.co.uk/address-lookup](https://clearpostcode.co.uk/address-lookup).
|
|
193
|
+
|
|
194
|
+
```python secret
|
|
195
|
+
from clearpostcode import ClearPostcode
|
|
196
|
+
|
|
197
|
+
with ClearPostcode() as cp:
|
|
198
|
+
# Suggestions are free (within a daily allowance per business). Kind "container" lists what it holds.
|
|
199
|
+
found = cp.addresses.suggest("2 high street porthmore").data
|
|
200
|
+
if found and found[0]["kind"] == "container":
|
|
201
|
+
found = cp.addresses.suggest("", container=found[0]["id"]).data
|
|
202
|
+
pick = found[0] if found else None
|
|
203
|
+
|
|
204
|
+
# Resolving an address costs one address credit.
|
|
205
|
+
if pick and pick["kind"] == "address":
|
|
206
|
+
address = cp.addresses.retrieve(pick["id"]).data
|
|
207
|
+
print(address["line_1"], address["post_town"], address["postcode"])
|
|
208
|
+
|
|
209
|
+
# Every address at a postcode, 100 a page.
|
|
210
|
+
for a in cp.addresses.all_by_postcode("AB10 1AA"):
|
|
211
|
+
print(a["udprn"], a["line_1"])
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
## Plans
|
|
215
|
+
|
|
216
|
+
Free: 10,000 postcode lookups a month, no card. Not-found answers, autocomplete and address suggestions cost nothing.
|
|
217
|
+
More lookups and Address lookup: [clearpostcode.co.uk/pricing](https://clearpostcode.co.uk/pricing). What's new:
|
|
218
|
+
[CHANGELOG](https://github.com/clearpostcode/postcode-api/blob/main/CHANGELOG.md).
|
|
219
|
+
|
|
220
|
+
## Licence
|
|
221
|
+
|
|
222
|
+
MIT. Postcode data: Contains OS data © Crown copyright and database right; Royal Mail data © Royal Mail copyright and
|
|
223
|
+
database right; Office for National Statistics data licensed under the Open Government Licence v3.0.
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# clearpostcode
|
|
2
|
+
|
|
3
|
+
Every UK postcode, answered in milliseconds: look up, validate and autocomplete postcodes, with the council, ward,
|
|
4
|
+
constituency, coordinates, police force, NHS area and deprivation decile behind each one. Find the nearest postcodes,
|
|
5
|
+
reverse geocode a point, measure distances, list a district, search places, and look up full Royal Mail addresses.
|
|
6
|
+
|
|
7
|
+
- **Typed end to end:** every answer is a `TypedDict` generated from the API's OpenAPI document, every error a class,
|
|
8
|
+
and a full lookup is a `PostcodeRecord` with every field. mypy strict and pyright check each one.
|
|
9
|
+
- **Sync and async:** `ClearPostcode` and `AsyncClearPostcode`, on httpx, with context managers.
|
|
10
|
+
- **Built for production:** automatic retries with backoff that honour `Retry-After` and the rate-limit headers,
|
|
11
|
+
timeouts, cursor pagination and bulk calls of any size.
|
|
12
|
+
- **Free to start:** 10,000 postcode lookups a month on a free key, no card, and many calls work with no key at all.
|
|
13
|
+
|
|
14
|
+
Two products, one key: **Postcode data** (every UK postcode and its areas, from open data) and **Address lookup**
|
|
15
|
+
(every address at a postcode, from Royal Mail PAF). Python 3.11 or later.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
uv add clearpostcode # or: pip install clearpostcode
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Quickstart
|
|
24
|
+
|
|
25
|
+
Runs as printed, with or without a key. The client reads your key from `CLEAR_POSTCODE_KEY` when it is set; without
|
|
26
|
+
one, calls run on the free keyless allowance.
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from clearpostcode import ClearPostcode
|
|
30
|
+
|
|
31
|
+
with ClearPostcode() as cp:
|
|
32
|
+
pc = cp.postcodes.lookup("SW1A 1AA").data
|
|
33
|
+
council, seat = pc["local_authority"], pc["constituency"]
|
|
34
|
+
print(council and council["name"], seat and seat["name"])
|
|
35
|
+
# Westminster Cities of London and Westminster
|
|
36
|
+
|
|
37
|
+
check = cp.postcodes.validate("m1 1ae").data
|
|
38
|
+
print(check["valid"], check["postcode"]) # True M1 1AE
|
|
39
|
+
|
|
40
|
+
print(len(cp.postcodes.autocomplete("SW1A 1").data) > 0) # True
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Get a free key in under a minute at [clearpostcode.co.uk/developers](https://clearpostcode.co.uk/developers#key):
|
|
44
|
+
secret keys start `cp_live_`.
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
export CLEAR_POSTCODE_KEY=cp_live_...
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## What you can call
|
|
51
|
+
|
|
52
|
+
| Call | What it answers |
|
|
53
|
+
|---|---|
|
|
54
|
+
| `postcodes.lookup(postcode, fields=[...])` | The full record: status, coordinates, council, ward, constituency and every other area |
|
|
55
|
+
| `postcodes.validate(postcode)` | Is the format valid, is it in the current edition, is it live |
|
|
56
|
+
| `postcodes.autocomplete(q, limit=)` | Postcodes (or districts) that start with what was typed. Free |
|
|
57
|
+
| `postcodes.nearest(postcode, radius=, limit=)` | Live postcodes nearest a postcode, with distances |
|
|
58
|
+
| `postcodes.reverse(latitude, longitude, radius=, limit=)` | Live postcodes nearest a point |
|
|
59
|
+
| `postcodes.distance(from_, to)` | Kilometres, miles and the bearing between two postcodes |
|
|
60
|
+
| `postcodes.bulk(postcodes, fields=[...])` | Any number of postcodes, sent 100 a call. Secret key |
|
|
61
|
+
| `postcodes.bulk_reverse(points)` | Nearest postcodes for any number of points, 20 a call. Secret key |
|
|
62
|
+
| `outcodes.get(outcode)` | A district: counts, sectors, centre, councils, wards, places |
|
|
63
|
+
| `outcodes.postcodes(outcode)` / `outcodes.all_postcodes(outcode)` | Every live postcode in a district, by page or all of them. Secret key |
|
|
64
|
+
| `sectors.get(sector)` · `areas.get(area)` | A sector or an area |
|
|
65
|
+
| `places.search(q)` | Cities, towns, villages and neighbourhoods by name |
|
|
66
|
+
| `account.usage()` | Your plan, allowance, lookups used and reset date. Secret key |
|
|
67
|
+
| `addresses.suggest`, `retrieve`, `by_postcode`, `all_by_postcode`, `by_udprn` | Address lookup (below) |
|
|
68
|
+
|
|
69
|
+
Every call answers a frozen `Result`: `data` and `meta` as the API sends them, plus `status`, `request_id` and
|
|
70
|
+
`rate_limit` (`policy`, `limit`, `remaining`, `reset`). Every field is documented at
|
|
71
|
+
[clearpostcode.co.uk/developers/docs](https://clearpostcode.co.uk/developers/docs).
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
from clearpostcode import ClearPostcode
|
|
75
|
+
|
|
76
|
+
with ClearPostcode() as cp:
|
|
77
|
+
r = cp.postcodes.lookup("EH1 1YZ", fields=["location", "ward"])
|
|
78
|
+
ward, where = r.data.get("ward"), r.data.get("location")
|
|
79
|
+
print(ward and ward["name"], where and where["latitude"]) # City Centre 55.950328
|
|
80
|
+
print(r.request_id is not None, r.rate_limit is not None) # True True
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Async
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
import asyncio
|
|
87
|
+
|
|
88
|
+
from clearpostcode import AsyncClearPostcode
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
async def main() -> None:
|
|
92
|
+
async with AsyncClearPostcode() as cp:
|
|
93
|
+
a, b = await asyncio.gather(cp.postcodes.lookup("M1 1AE"), cp.postcodes.lookup("CF10 1EP"))
|
|
94
|
+
councils = [r.data["local_authority"] for r in (a, b)]
|
|
95
|
+
print(*(c["name"] for c in councils if c)) # Manchester Cardiff
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
asyncio.run(main())
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Errors
|
|
102
|
+
|
|
103
|
+
Every error is a `ClearPostcodeError`. When the API answers with an error, you get an `APIError` subclass carrying
|
|
104
|
+
the problem's `code`, `detail`, `status` and the `request_id` to quote to support:
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
from clearpostcode import ClearPostcode, NotFoundError
|
|
108
|
+
|
|
109
|
+
with ClearPostcode() as cp:
|
|
110
|
+
try:
|
|
111
|
+
cp.postcodes.lookup("SW1A 9ZZ")
|
|
112
|
+
except NotFoundError as e:
|
|
113
|
+
print(e.code) # postcode_not_found
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
| Class | Status | Codes |
|
|
117
|
+
|---|---|---|
|
|
118
|
+
| `BadRequestError` | 400, 413 | `invalid_postcode`, `unknown_parameter`, `bad_request`, `secret_key_in_url`, `body_too_large` |
|
|
119
|
+
| `AuthenticationError` | 401 | `invalid_key`, `key_required` |
|
|
120
|
+
| `QuotaExceededError` | 402 | `quota_exceeded`, `address_credits_exhausted` |
|
|
121
|
+
| `PermissionDeniedError` | 403 | `origin_not_allowed`, `publishable_key_not_allowed`, `secret_key_in_browser`, `address_lookup_not_enabled` |
|
|
122
|
+
| `NotFoundError` | 404 | `postcode_not_found`, `not_covered`, `not_found` |
|
|
123
|
+
| `UnprocessableEntityError` | 422 | `no_position` |
|
|
124
|
+
| `RateLimitError` | 429 | `rate_limited`, `daily_cap_reached`, `suggestions_capped` |
|
|
125
|
+
| `InternalServerError` | 5xx | `server_error`, `service_unavailable` |
|
|
126
|
+
| `APIConnectionError`, `APITimeoutError` | | The API could not be reached, or did not answer in time |
|
|
127
|
+
|
|
128
|
+
## Retries and timeouts
|
|
129
|
+
|
|
130
|
+
429s, 5xx answers, timeouts and dropped connections are retried twice by default, with exponential backoff and
|
|
131
|
+
jitter. A `Retry-After` from the API is honoured exactly, and a 429 without one waits for the `RateLimit` reset. A
|
|
132
|
+
wait longer than `max_retry_delay` (a key's daily cap until midnight, say) is raised at once, with `retry_after` in
|
|
133
|
+
seconds.
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
from clearpostcode import ClearPostcode
|
|
137
|
+
|
|
138
|
+
with ClearPostcode(timeout=5.0, max_retries=3) as cp:
|
|
139
|
+
print(cp.with_options(timeout=2.0).postcodes.validate("SW1A 1AA").data["valid"]) # True
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Pages and bulk calls
|
|
143
|
+
|
|
144
|
+
```python secret
|
|
145
|
+
from clearpostcode import ClearPostcode
|
|
146
|
+
|
|
147
|
+
with ClearPostcode() as cp: # a secret key in CLEAR_POSTCODE_KEY
|
|
148
|
+
print(sum(1 for _ in cp.outcodes.all_postcodes("SW1A")) > 100) # True: every live postcode, page after page
|
|
149
|
+
|
|
150
|
+
r = cp.postcodes.bulk(["SW1A 1AA", "M1 1AE", "EH1 1YZ"], fields=["local_authority"])
|
|
151
|
+
print(r.meta["found"] if r.meta else 0) # 3
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Bulk calls take lists of any length: the client sends 100 postcodes (or 20 points) a call, one after another, and
|
|
155
|
+
joins the answers in the order you sent them. If a chunk fails part-way (the month's lookups run out, say), the error
|
|
156
|
+
carries `partial`: everything the chunks before it answered, so nothing you paid for is lost. Bulk calls are retried
|
|
157
|
+
only on answers that say nothing was billed (429 and 503).
|
|
158
|
+
|
|
159
|
+
## Address lookup
|
|
160
|
+
|
|
161
|
+
Full UK address lookup on Royal Mail's Postcode Address File: the customer types a postcode or the start of an
|
|
162
|
+
address, picks it from the list, and you save a clean Royal Mail address. It is in early access and switches on per
|
|
163
|
+
account: once the business your key serves has Address lookup switched on in your
|
|
164
|
+
[dashboard](https://clearpostcode.co.uk/dashboard#address-lookup), every call below answers. Until then they raise
|
|
165
|
+
`PermissionDeniedError` with code `address_lookup_not_enabled`. Join at
|
|
166
|
+
[clearpostcode.co.uk/address-lookup](https://clearpostcode.co.uk/address-lookup).
|
|
167
|
+
|
|
168
|
+
```python secret
|
|
169
|
+
from clearpostcode import ClearPostcode
|
|
170
|
+
|
|
171
|
+
with ClearPostcode() as cp:
|
|
172
|
+
# Suggestions are free (within a daily allowance per business). Kind "container" lists what it holds.
|
|
173
|
+
found = cp.addresses.suggest("2 high street porthmore").data
|
|
174
|
+
if found and found[0]["kind"] == "container":
|
|
175
|
+
found = cp.addresses.suggest("", container=found[0]["id"]).data
|
|
176
|
+
pick = found[0] if found else None
|
|
177
|
+
|
|
178
|
+
# Resolving an address costs one address credit.
|
|
179
|
+
if pick and pick["kind"] == "address":
|
|
180
|
+
address = cp.addresses.retrieve(pick["id"]).data
|
|
181
|
+
print(address["line_1"], address["post_town"], address["postcode"])
|
|
182
|
+
|
|
183
|
+
# Every address at a postcode, 100 a page.
|
|
184
|
+
for a in cp.addresses.all_by_postcode("AB10 1AA"):
|
|
185
|
+
print(a["udprn"], a["line_1"])
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Plans
|
|
189
|
+
|
|
190
|
+
Free: 10,000 postcode lookups a month, no card. Not-found answers, autocomplete and address suggestions cost nothing.
|
|
191
|
+
More lookups and Address lookup: [clearpostcode.co.uk/pricing](https://clearpostcode.co.uk/pricing). What's new:
|
|
192
|
+
[CHANGELOG](https://github.com/clearpostcode/postcode-api/blob/main/CHANGELOG.md).
|
|
193
|
+
|
|
194
|
+
## Licence
|
|
195
|
+
|
|
196
|
+
MIT. Postcode data: Contains OS data © Crown copyright and database right; Royal Mail data © Royal Mail copyright and
|
|
197
|
+
database right; Office for National Statistics data licensed under the Open Government Licence v3.0.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "clearpostcode"
|
|
3
|
+
version = "2.0.0"
|
|
4
|
+
description = "UK postcode lookup API client: validate, autocomplete, nearest, reverse geocode, distance, council, ward and constituency, plus Royal Mail address lookup. Typed, sync and async, retries built in."
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
authors = [{ name = "ClearPostcode", email = "hello@clearpostcode.co.uk" }]
|
|
9
|
+
keywords = ["postcode", "uk postcode", "postcode lookup", "postcode api", "postcode validation", "postcode autocomplete", "address lookup", "royal mail paf", "reverse geocode", "geocoding", "council", "ward", "constituency"]
|
|
10
|
+
classifiers = [
|
|
11
|
+
"Programming Language :: Python :: 3",
|
|
12
|
+
"Programming Language :: Python :: 3.11",
|
|
13
|
+
"Programming Language :: Python :: 3.12",
|
|
14
|
+
"Programming Language :: Python :: 3.13",
|
|
15
|
+
"Programming Language :: Python :: 3.14",
|
|
16
|
+
"Framework :: AsyncIO",
|
|
17
|
+
"Operating System :: OS Independent",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Topic :: Software Development :: Libraries",
|
|
20
|
+
"Typing :: Typed",
|
|
21
|
+
]
|
|
22
|
+
dependencies = ["httpx>=0.27,<1"]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://clearpostcode.co.uk/developers"
|
|
26
|
+
Documentation = "https://clearpostcode.co.uk/developers/docs"
|
|
27
|
+
Repository = "https://github.com/clearpostcode/postcode-api"
|
|
28
|
+
Changelog = "https://github.com/clearpostcode/postcode-api/blob/main/CHANGELOG.md"
|
|
29
|
+
|
|
30
|
+
[dependency-groups]
|
|
31
|
+
dev = ["pytest>=8", "pytest-asyncio>=0.24", "ruff>=0.8", "mypy>=1.13"]
|
|
32
|
+
|
|
33
|
+
[build-system]
|
|
34
|
+
requires = ["hatchling"]
|
|
35
|
+
build-backend = "hatchling.build"
|
|
36
|
+
|
|
37
|
+
[tool.hatch.build.targets.wheel]
|
|
38
|
+
packages = ["src/clearpostcode"]
|
|
39
|
+
|
|
40
|
+
[tool.ruff]
|
|
41
|
+
line-length = 120
|
|
42
|
+
target-version = "py311"
|
|
43
|
+
src = ["src", "tests"]
|
|
44
|
+
|
|
45
|
+
[tool.ruff.lint]
|
|
46
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "RUF"]
|
|
47
|
+
per-file-ignores = { "src/clearpostcode/_generated.py" = ["E501"] }
|
|
48
|
+
|
|
49
|
+
[tool.mypy]
|
|
50
|
+
strict = true
|
|
51
|
+
python_version = "3.11"
|
|
52
|
+
files = ["src", "tests", "scripts"]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|
|
56
|
+
asyncio_mode = "auto"
|
|
57
|
+
asyncio_default_fixture_loop_scope = "function"
|
|
58
|
+
|
|
59
|
+
[tool.hatch.build.targets.sdist]
|
|
60
|
+
include = ["src", "README.md", "LICENSE", "pyproject.toml"]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""ClearPostcode: UK postcode data (lookup, validation, autocomplete, nearest, reverse geocoding, distance, districts,
|
|
2
|
+
sectors, areas and places) and Address lookup (Royal Mail PAF, early access), sync and async.
|
|
3
|
+
Docs: https://clearpostcode.co.uk/developers/docs"""
|
|
4
|
+
|
|
5
|
+
from ._async_client import AsyncClearPostcode
|
|
6
|
+
from ._base import BULK_POINTS, BULK_POSTCODES, AsyncPage, BulkResult, Page, RateLimit, Result
|
|
7
|
+
from ._calls import ReversePoint
|
|
8
|
+
from ._client import ClearPostcode
|
|
9
|
+
from ._errors import (
|
|
10
|
+
APIConnectionError,
|
|
11
|
+
APIError,
|
|
12
|
+
APITimeoutError,
|
|
13
|
+
AuthenticationError,
|
|
14
|
+
BadRequestError,
|
|
15
|
+
ClearPostcodeError,
|
|
16
|
+
InternalServerError,
|
|
17
|
+
NotFoundError,
|
|
18
|
+
PermissionDeniedError,
|
|
19
|
+
QuotaExceededError,
|
|
20
|
+
RateLimitError,
|
|
21
|
+
UnprocessableEntityError,
|
|
22
|
+
)
|
|
23
|
+
from ._generated import * # noqa: F403
|
|
24
|
+
from ._generated import __all__ as _types
|
|
25
|
+
from ._version import __version__
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"BULK_POINTS",
|
|
29
|
+
"BULK_POSTCODES",
|
|
30
|
+
"APIConnectionError",
|
|
31
|
+
"APIError",
|
|
32
|
+
"APITimeoutError",
|
|
33
|
+
"AsyncClearPostcode",
|
|
34
|
+
"AsyncPage",
|
|
35
|
+
"AuthenticationError",
|
|
36
|
+
"BadRequestError",
|
|
37
|
+
"BulkResult",
|
|
38
|
+
"ClearPostcode",
|
|
39
|
+
"ClearPostcodeError",
|
|
40
|
+
"InternalServerError",
|
|
41
|
+
"NotFoundError",
|
|
42
|
+
"Page",
|
|
43
|
+
"PermissionDeniedError",
|
|
44
|
+
"QuotaExceededError",
|
|
45
|
+
"RateLimit",
|
|
46
|
+
"RateLimitError",
|
|
47
|
+
"Result",
|
|
48
|
+
"ReversePoint",
|
|
49
|
+
"UnprocessableEntityError",
|
|
50
|
+
"__version__",
|
|
51
|
+
*_types,
|
|
52
|
+
]
|