AutoPostcode
Guide · Next.js

Postcode lookup in Next.js — keep the key on the server

The clean way to do postcode lookup in Next.js is a server route handler that hides your API key, called from a client component. This guide covers both halves with copy-paste code.

No card required · Royal Mail PAF-verified data

Trusted UK address intelligence

App Router
route handler
Server-only
secret key
31M+
PAF addresses
PAF-verified address lookup
Fast REST API & no-code plugins
GDPR-friendly, UK-hosted

For Next.js postcode lookup, create a route handler that calls the AutoPostcode API with your secret key server-side, then fetch that handler from a client component as the user searches. This keeps the key out of the browser while returning Royal Mail PAF addresses (~31M UK delivery points, ~1.8M postcodes) as JSON.

~31M
UK addresses in PAF
~1.8M
UK postcodes
99.9%
lookup uptime

How do I create the server route handler?

In the App Router, add a handler that proxies to AutoPostcode using a server-only env var:

// app/api/lookup/route.ts
export async function GET(req: Request) {
  const postcode = new URL(req.url).searchParams.get("postcode") ?? "";
  const res = await fetch(
    "https://api.autopostcode.com/v1/lookup?postcode=" + encodeURIComponent(postcode),
    { headers: { Authorization: "Bearer " + process.env.AUTOPOSTCODE_KEY } }
  );
  return Response.json(await res.json());
}

How do I call it from a client component?

The client only ever talks to your own endpoint, so the key stays secret:

"use client";
import { useState } from "react";

export function PostcodeLookup() {
  const [pc, setPc] = useState("");
  const [addresses, setAddresses] = useState([]);
  const find = async () => {
    const r = await fetch("/api/lookup?postcode=" + encodeURIComponent(pc));
    const { addresses } = await r.json();
    setAddresses(addresses);
  };
  return (
    <div>
      <input value={pc} onChange={(e) => setPc(e.target.value)} />
      <button onClick={find}>Find address</button>
      <ul>{addresses.map((a) => <li key={a.uprn}>{a.line1}, {a.town}, {a.postcode}</li>)}</ul>
    </div>
  );
}
Name the env var AUTOPOSTCODE_KEY, not NEXT_PUBLIC_AUTOPOSTCODE_KEY. The NEXT_PUBLIC_ prefix would bundle the secret into client JavaScript.

Server handler vs direct client call

ApproachKey safetyRecommended
Route handler proxyKey stays server-sideYes
Direct browser fetchKey exposedPrototypes only

Prefer a pure Node example? See the Node.js postcode lookup API guide, add search-as-you-type with the React address autocomplete guide, and read the endpoint reference in the postcode lookup API docs.

A modern, UK-first alternative

AutoPostcode uses the same Royal Mail PAF data as Loqate, Fetchify and Ideal Postcodes, with a REST API that drops straight into Next.js.

Frequently asked questions

How do I add postcode lookup in Next.js?

Create a route handler that calls the AutoPostcode API with your secret key server-side, then fetch that handler from a client component as the user searches. This keeps the key off the client while returning PAF-verified addresses as JSON.

Should the API call run on the server or client in Next.js?

The call to AutoPostcode should run on the server (a route handler or server action) so your API key never reaches the browser. The client component only calls your own internal endpoint.

Does this work with the App Router?

Yes. Use a route handler at app/api/lookup/route.ts for the server call, and a client component ('use client') for the input and results. The Pages Router works too via an API route.

Where do I store the API key in Next.js?

Store it in an environment variable without the NEXT_PUBLIC_ prefix (e.g. AUTOPOSTCODE_KEY) so it stays server-only and is never bundled into client code.

Can I cache postcode lookups?

Yes. Postcode-to-address results are stable, so you can cache responses at the route handler to reduce repeat calls, while respecting your data licence terms.

Is the data Royal Mail PAF?

Yes. AutoPostcode is built on Royal Mail PAF, covering ~31M UK delivery points across ~1.8M postcodes, so results are authoritative and correctly formatted.

Ready to get started?

Add Royal Mail PAF-verified UK address lookup to your site in minutes — start free, no card required.