Autocomplete
find_address turns a partial address into suggestions as the user types. resolve_address turns the chosen suggestion into a full address.
require "ideal_postcodes"
client = IdealPostcodes::Client.new(api_key: "ak_test")
hits = client.find_address(query: "9 Malyons Road", limit: 5).result.hits
abort "No matches" if hits.empty?
address = client.resolve_address(hits.first.id).result
puts "#{address.line_1}, #{address.post_town}, #{address.postcode}"
Find suggestions
Call find_address with the partial address as query:. The response holds result.hits, an array of suggestions with an id and a suggestion to show. An empty array means nothing matched.
It takes every other find_address keyword too: limit:, context: to search another country, and the filters and biases, such as postcode_outward: or bias_post_town:. See the API reference for each one.
The SDK runs on your server. Have the browser send each keystroke to an endpoint of yours that calls find_address, so your key never leaves the server.
Country switching
key_availability lists the countries a key can search. It needs only the API key and costs nothing.
require "ideal_postcodes"
client = IdealPostcodes::Client.new(api_key: "ak_test")
key = client.key_availability("ak_test").result
key.contexts.each do |country|
puts "#{country.emoji} #{country.description} #{country.iso_3}"
end
context = key.context.empty? ? key.contexts.first.iso_3 : key.context
hits = client.find_address(query: "1 Main Street", context: context).result.hits
Each entry in contexts has iso_3 (the context: code), iso_2, description and emoji. context is the licensed country that best matches the IP address the request came from, or an empty string when none does. When the SDK runs on your server, that is your server's address, so take the user's choice over it.
Resolve a suggestion
Pass the hit's id to resolve_address. The result is an Address with line_1, line_2, line_3, post_town, postcode, uprn and the other standard fields. See responses for how to read it.
native holds the raw record from the address's source dataset, when the API returns one. It is nil otherwise, so check its class before reading dataset-specific fields.
address = client.resolve_address("paf_2670849").result
puts address.native.delivery_point_suffix if address.native.is_a?(IdealPostcodes::Types::PafAddress)
Cost
Suggestions are free. Each resolve_address costs one lookup. The API rate limits, then suspends, integrations that request suggestions without resolving any. The default limit is 3,000 suggestion requests per 5 minutes for each key and IP address.
Errors
An HTTP error raises IdealPostcodes::ApiError. A failed connection raises IdealPostcodes::TransportError. See error handling. See resolve for the parameters.