Websites York - Template54

Template Menu

UK Postcode lookup using PI v2

This is accessing an API at https://postcodes.io

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Type your input data here

This field is required

Design Notes

I have modified the demo page I received with PI version 2 as follows:

  1. The field pfa seemed to be the only field that gave the correct answer for County
  2. Added 'Ward' which is a local council area within the city and Mickelgate is the correct ward for YO23 1JY
  3. Added Longitude and Latitude and the results seem reasonable given that the postcode by itself does not define a precise building within the postcode area.
  4. Added Country which gives me England which is correct

Test results so far

Note this works for Me and also for Dave but does not work for Gary Wann.

Code used:
async function fetchPostcodeData(postcode) { const town = document.querySelector("[name=town]"); const ward = document.querySelector("[name=ward]"); const county = document.querySelector("[name=county]"); const region = document.querySelector("[name=region]"); const longitude = document.querySelector("[name=longitude]"); const latitude = document.querySelector("[name=latitude]"); const country = document.querySelector("[name=country]"); if (postcode.length < 5) { town.value = ""; ward.value = ""; county.value = ""; region.value = ""; longitude.value = ""; latitude.value = ""; country.value = ""; return; } const url = `https://api.postcodes.io/postcodes/${postcode}`; // URL for the API const data = await Pi.fetchJSON(url); town.value = data.result.admin_district ?? ""; ward.value = data.result.admin_ward ?? ""; county.value = data.result.pfa ?? ""; region.value = data.result.region ?? ""; country.value = data.result.country ?? ""; longitude.value = data.result.longitude ?? ""; latitude.value = data.result.latitude ?? ""; }