Return addresses for a full postcode
Use GET /api/public/lookup when a user clicks Find my address. The response is an array of candidate delivery points. Let the user choose the address. Offer manual entry when no address matches.
Unix shell
curl -sS --get 'https://autopostcode.com/api/public/lookup' --data-urlencode 'postcode=SW1A 1AA' -H "x-api-key: $AUTOPOSTCODE_API_KEY"PowerShell
Invoke-RestMethod -Uri 'https://autopostcode.com/api/public/lookup?postcode=SW1A%201AA' -Headers @{ 'x-api-key' = $env:AUTOPOSTCODE_API_KEY }JavaScript (Node.js)
const key = process.env.AUTOPOSTCODE_API_KEY;
if (!key) throw new Error('Configure AUTOPOSTCODE_API_KEY on this server');
const url = new URL('https://autopostcode.com/api/public/lookup');
url.searchParams.set('postcode', 'SW1A 1AA');
const response = await fetch(url, { headers: { 'x-api-key': key } });
const requestId = response.headers.get('x-request-id');
if (response.status === 404) console.log('Offer manual address entry');
else if (!response.ok) throw new Error('HTTP ' + response.status + ' (' + requestId + ')');
else console.log(await response.json());Python
import json, os
from urllib.error import HTTPError
from urllib.request import Request, urlopen
request = Request('https://autopostcode.com/api/public/lookup?postcode=SW1A%201AA',
headers={'x-api-key': os.environ['AUTOPOSTCODE_API_KEY']})
try:
with urlopen(request, timeout=15) as response:
print(json.load(response))
except HTTPError as error:
if error.code == 404:
print('Offer manual address entry')
else:
print(error.code, error.headers.get('X-Request-Id'))Suggest addresses as the user types
Use GET /api/public/autocomplete with at least three meaningful characters for type-ahead. Display suggestions and groups, and expand an opaque group token when a user chooses a group.
Keep a random session ID for one address selection journey. Debounce requests and generate a new ID when the user selects an address or clears the field. The first matched request in a rolling one-hour session window consumes a credit.
Call from your server with the secured website key. Connect browser widgets to your own authenticated or rate-limited proxy and forward the session ID. Keep the website key out of all browser bundles.
Server-side autocomplete (Node.js)
async function searchAddresses(query, session) {
const url = new URL('https://autopostcode.com/api/public/autocomplete');
url.search = new URLSearchParams({ q: query, session });
const response = await fetch(url, { headers: { 'x-api-key': process.env.AUTOPOSTCODE_API_KEY } });
if (!response.ok) throw new Error('Autocomplete HTTP ' + response.status);
return response.json();
}