Parse a postal address in Go, PHP, Ruby or Java

Updated 28 November 2026 · parseaddr.com · plain markdown

libpostal has bindings for Go, PHP, Ruby and Java (listed in its README), but each one needs the C library built and about 1.8 GB of model data on every machine (why installs fail). These examples call the hosted API instead. Each uses only the standard library and returns postcode sw1a 2aa and road downing st for the sample address.

Get a free key API reference

curl

curl -s https://api.parseaddr.com/v1/parse \
  -H "Authorization: Bearer $PARSEADDR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"address": "flat 2, 10 downing st london sw1a 2aa"}'

Response:

{"input":"flat 2, 10 downing st london sw1a 2aa","components":[...],"parsed":{"unit":"flat 2","house_number":"10","road":"downing st","city":"london","postcode":"sw1a 2aa"},"country_code":null}

Go (Go 1.24)

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
)

func main() {
	body, _ := json.Marshal(map[string]string{"address": "flat 2, 10 downing st london sw1a 2aa"})
	req, _ := http.NewRequest("POST", "https://api.parseaddr.com/v1/parse", bytes.NewReader(body))
	req.Header.Set("Authorization", "Bearer "+os.Getenv("PARSEADDR_API_KEY"))
	req.Header.Set("Content-Type", "application/json")
	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	var out struct {
		Parsed map[string]string `json:"parsed"`
	}
	json.NewDecoder(resp.Body).Decode(&out)
	fmt.Println(resp.StatusCode, out.Parsed["postcode"], out.Parsed["road"])
}

Prints 200 sw1a 2aa downing st.

PHP (PHP 8.3, curl extension)

<?php
$ch = curl_init('https://api.parseaddr.com/v1/parse');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer ' . getenv('PARSEADDR_API_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => json_encode(['address' => 'flat 2, 10 downing st london sw1a 2aa']),
]);
$data = json_decode(curl_exec($ch), true);
echo curl_getinfo($ch, CURLINFO_HTTP_CODE), ' ', $data['parsed']['postcode'], ' ', $data['parsed']['road'], "\n";

Prints 200 sw1a 2aa downing st.

Ruby (Ruby 3.3)

require "net/http"
require "json"

uri = URI("https://api.parseaddr.com/v1/parse")
req = Net::HTTP::Post.new(uri, "Authorization" => "Bearer #{ENV.fetch('PARSEADDR_API_KEY')}",
                               "Content-Type" => "application/json")
req.body = { address: "flat 2, 10 downing st london sw1a 2aa" }.to_json
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |http| http.request(req) }
parsed = JSON.parse(res.body)["parsed"]
puts "#{res.code} #{parsed['postcode']} #{parsed['road']}"

Prints 200 sw1a 2aa downing st.

Java (Java 21, java.net.http)

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Parse {
    public static void main(String[] args) throws Exception {
        String json = "{\"address\": \"flat 2, 10 downing st london sw1a 2aa\"}";
        HttpRequest req = HttpRequest.newBuilder(URI.create("https://api.parseaddr.com/v1/parse"))
            .header("Authorization", "Bearer " + System.getenv("PARSEADDR_API_KEY"))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(json))
            .build();
        HttpResponse<String> res = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
        System.out.println(res.statusCode() + " " + res.body());
    }
}

Prints the status and the same JSON body as the curl example. Read parsed from it with the JSON library you already use.

Errors

StatusMeaning
401Missing or unknown API key.
422The body is not {"address": "..."}.
429Monthly cap reached, or over 10 calls a second on a free key. Wait for Retry-After seconds.

Python and JavaScript have their own pages: Python, JavaScript.

parseaddr · home · agents · pricing · llms.txt · .md
Pyke Limited, registered in England and Wales, company number 09748427.