Postcode lookup

Postcodes() returns the addresses at a postcode. An unknown postcode returns an *APIError with code 4040, whose body holds the API's closest matching postcodes.

lookup.gogo
package main

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.Postcodes(context.Background(), "BR8 7RE", nil)

	if apiErr, ok := errors.AsType[*idealpostcodes.APIError](err); ok && apiErr.Code == 4040 {
		var body struct {
			Suggestions []string `json:"suggestions"`
		}
		_ = json.Unmarshal(apiErr.Body, &body)
		fmt.Println("Did you mean:", body.Suggestions)
		return
	}
	if err != nil {
		log.Fatal(err)
	}
	for _, address := range res.Result {
		fmt.Printf("%s, %s, %s\n", address.Line1, address.PostTown, address.Postcode)
	}
}

Return value​

A *PostcodeResponse.

FieldContents
ResultThe addresses on this page, as AddressListItem structs
TotalAddresses at the postcode across all pages
Page and LimitThis page's number, from 0, and its size

An unfound postcode costs no lookup. Up to five suggestions come back in the error body, or none when the input is too far from a valid postcode. Other errors, such as an invalid key or an empty balance, also return *APIError. See error handling.

Multiple Residence pagination​

A small number of postcodes return more than 100 premises. The API returns 100 addresses per page, so set Page until you have Total addresses.

pages.gogo
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	var addresses []idealpostcodes.AddressListItem
	for page := 0; ; page++ {
		res, err := client.Postcodes(context.Background(), "CV4 7AL", &idealpostcodes.PostcodesParams{Page: page})
		if err != nil {
			log.Fatal(err)
		}
		addresses = append(addresses, res.Result...)
		if len(res.Result) == 0 || len(addresses) >= res.Total {
			break
		}
	}
	fmt.Println(len(addresses), "addresses")
}

Field selection​

Set Filter to the fields you need, as a comma-separated list of API field names. Fields left out of the response stay at their zero value, such as "".

filter.gogo
package main

import (
	"context"
	"fmt"
	"log"

	idealpostcodes "go.ideal-postcodes.co.uk/sdk"
)

func main() {
	client := idealpostcodes.NewClient("ak_test")
	res, err := client.Postcodes(context.Background(), "BR8 7RE", &idealpostcodes.PostcodesParams{
		Filter: "line_1,line_2,post_town,postcode",
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(res.Result[0].Line1)
}

Every parameter is on the Postcodes() reference page.