Skip to content
Clear Postcode

Integration · Spreadsheets

Postcode lookup in Google Sheets

Paste a 15-line Apps Script into your sheet and you get =POSTCODE(A2, "council"): council, ward, constituency, region, latitude or longitude for any UK postcode, straight from the API.

Get a free key10,000 lookups a month free · no card

Set it up

  1. 1Get a free key at /developers (secret key, cp_live_).
  2. 2In your sheet: Extensions → Apps Script, paste the code, replace YOUR_KEY, save.
  3. 3In any cell: =POSTCODE(A2, "council"). Fields: council, ward, constituency, region, country, latitude, longitude, status.

The code

const KEY = "YOUR_KEY";

/**
 * UK postcode data. =POSTCODE(A2, "council")
 * @customfunction
 */
function POSTCODE(postcode, field) {
  if (!postcode) return "";
  const url = "https://api.clearpostcode.co.uk/v1/postcodes/" + encodeURIComponent(postcode);
  const r = UrlFetchApp.fetch(url, { headers: { "x-api-key": KEY }, muteHttpExceptions: true });
  if (r.getResponseCode() !== 200) return "Not found";
  const d = JSON.parse(r.getContentText()).data;
  const pick = { council: d.local_authority, ward: d.ward, constituency: d.constituency, region: d.region, country: d.country };
  if (field in pick) return pick[field] ? pick[field].name : "";
  if (field === "latitude") return d.location.latitude;
  if (field === "longitude") return d.location.longitude;
  return d.status;
}

Questions

Google Sheets questions

How many rows can I fill?

Each cell is one lookup: 10,000 a month free, more on a paid plan. Google caches formula results, so a sheet doesn't re-run every lookup on each open.

Can I fill a whole column at once?

Yes: drag the formula down. For thousands of rows, the bulk endpoint (100 postcodes a call) is faster; ask us for the batch script.