Documentation

Go Code Snippets

The examples below provide a very basic demonstration of how to integrate against the API using Go.

Examples listed demonstrate who to lookup a postcode and query for an address.

In each instance, you would replace your api key and search terms accordingly.

Please note, these are very simple examples of how to use the API and certainly not production-ready code.

If you think these examples could be improved or made more idiomatic, feel free to let us know.

# Address Search

package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "net/url"
)

type Address struct {
    Line_1    string `json:"line_1"`
    Line_2    string `json:"line_2"`
    Line_3    string `json:"line_3"`
    Post_town string `json:"post_town"`
    Postcode  string `json:"postcode"`
}

type SearchResults struct {
    Total int64     `json:"total"`
    Limit int64     `json:"limit"`
    Page  int64     `json:"page"`
    Hits  []Address `json:"hits"`
}

type ApiResponse struct {
    Message string        `json:"message"`
    Code    int64         `json:"code"`
    Result  SearchResults `json:"result"`
}

func main() {
    addressQuery := "10 Downing Street London"
    apiKey := "iddqd"

    var Url *url.URL
    Url, err := url.Parse("https://api.ideal-postcodes.co.uk/v1/addresses")
    parameters := url.Values{}
    parameters.Add("query", addressQuery)
    parameters.Add("api_key", apiKey)
    Url.RawQuery = parameters.Encode()

    response, _ := http.Get(Url.String())
    
    defer response.Body.Close()

    contents, _ := ioutil.ReadAll(response.Body)

    var result ApiResponse
    _ := json.Unmarshal(contents, &result)

    // result now contains the data you need
}
# Postcode Search

package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "net/url"
)

type Address struct {
    Line_1    string `json:"line_1"`
    Line_2    string `json:"line_2"`
    Line_3    string `json:"line_3"`
    Post_town string `json:"post_town"`
    Postcode  string `json:"postcode"`
}

type ApiResponse struct {
    Message string    `json:"message"`
    Code    int64     `json:"code"`
    Result  []Address `json:"result"`
}

func main() {
    postcode := "ID1 1QD"
    apiKey := "iddqd"
    
    var Url *url.URL
    Url, err := url.Parse("https://api.ideal-postcodes.co.uk/v1/postcodes/" + postcode)
    parameters := url.Values{}
    parameters.Add("api_key", apiKey)
    Url.RawQuery = parameters.Encode()

    response, _ := http.Get(Url.String())
    
    defer response.Body.Close()

    contents, _ := ioutil.ReadAll(response.Body)
    
    var result ApiResponse
    _ := json.Unmarshal(contents, &result)

    // result now contains the data you need
}