Documentation

Java Code Snippets

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

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.

// Lookup a Postcode
 
String apiKey = "iddqd";
String postcode = URLEncoder.encode("ID1 1QD", "UTF-8").replace("+", "%20");
String baseUrl = "https://api.ideal-postcodes.co.uk/v1/postcodes/";
 
URL obj = new URL(baseUrl + postcode + "?api_key=" + apiKey);
 
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
 
con.setRequestMethod("GET");
 
int responseCode = con.getResponseCode();
 
BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
 
// Use your favourite JSON parser to handle response
// Query for an Address
 
String apiKey = "iddqd";
String query = URLEncoder.encode("10 Downing Street London", "UTF-8").replace("+", "%20");
String baseUrl = "https://api.ideal-postcodes.co.uk/v1/addresses";
 
URL obj = new URL(baseUrl + "?api_key=" + apiKey + "&query=" + "query");
 
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
 
con.setRequestMethod("GET");
 
int responseCode = con.getResponseCode();
 
BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
 
while ((inputLine = in.readLine()) != null) {
    response.append(inputLine);
}
in.close();
 
// Use your favourite JSON parser to handle response