JavaScript Postcode Lookup (Fetch API)

Create a seamless, no-reload address verification experience using modern JavaScript.

Frontend Precision: The Power of Async Search

In the era of Single Page Applications (SPAs) and reactive design, page refreshes are a legacy barrier to conversions. Implementing a JavaScript postcode lookup allows you to verify customer data in real-time. By utilizing the browser's native Fetch API, you can create a surgical postcode to address search that validates a specific property before the user even clicks "Next."

 

The findAddress.io 1:1 matching engine is perfectly suited for asynchronous frontend environments. Rather than fetching a heavy list of dozens of properties, your script sends a targeted request for a single building footprint using a house identifier and postcode. This results in a faster, cleaner UK postcode lookup API integration that feels integrated rather than interrupted.


Step 1: Setting up the Event Listener

The logic begins by preventing the browser's default form submission behavior. We want to intercept the user's intent and handle the UK address search via our custom script. We capture the specific house number and postcode values from the DOM, ensuring they are present before proceeding with the postcode search API call.

// Select elements from your form
const btn = document.querySelector('#lookup-btn');

btn.addEventListener('click', async (e) => {
    e.preventDefault();
    
    const house = document.querySelector('#house').value;
    const postcode = document.querySelector('#postcode').value;

    if (!house || !postcode) return alert('Please enter both fields');
    
    executeLookup(house, postcode);
});

Step 2: Performing the 1:1 Fetch Request

With the input data captured, we use the URLSearchParams interface to build a safe, encoded query string for our postcode lookup API. This handles the ampersand delimiters and character encoding automatically. We then use async/await to wait for the findAddress engine to return the unique property record. This non-blocking request ensures the user can continue interacting with your site while the postcode to address search completes in the background.

async function executeLookup(house, postcode) {
    const apiKey = 'YOUR_API_KEY';
    const params = new URLSearchParams({ apiKey, house, postcode });
    const url = `https://findaddress.io/API/?${params.toString()}`;

    try {
        const response = await fetch(url);
        const data = await response.json();
        
        if (data.result === 'Success') {
            populateForm(data.expandedAddress);
        }
    } catch (err) {
        console.error('Lookup failed', err);
    }
}

Step 3: Populating the UI Instantly

The final piece of our postcode lookup implementation is the DOM injection. Once the API returns the expandedAddress object, we map its individual components (street, town, county) directly to the corresponding hidden or visible input fields. This instantaneous "filling" of the form provides the user with an immediate sense of accuracy and security, confirming that their address lookup from postcode was successful without them having to scroll through a generic dropdown list.

function populateForm(addr) {
    // Map JSON keys to your input values
    document.querySelector('#street').value = addr.street;
    document.querySelector('#town').value = addr.town;
    document.querySelector('#county').value = addr.County;
    
    console.log('1:1 Match Found', addr);
}

Modern UX Strategy

User intent is maximized when the barrier to entry is low. By providing a surgical UK address lookup that works instantly on the frontend, you reduce the time-to-completion for your forms. Furthermore, since findAddress.io provides geodata at the root of the response, you can even use JavaScript to render a preview map or calculate localized shipping rates the moment the 1:1 match is verified.

Start Your JavaScript Integration

Implement real-time UK address matching in your browser in under 10 minutes.

Get API Key View API Docs