Setup
Install ideal_postcodes, then create a client with your API key. The SDK needs Ruby 3.3 or later and nothing else.
Install
gem install ideal_postcodes
Or add it to your Gemfile:
gem "ideal_postcodes", "~> 3.0"
Create a client
Pass your API key as api_key:.
require "ideal_postcodes"
client = IdealPostcodes::Client.new(api_key: "ak_test")
A client holds no connection, so one instance can serve a whole application, across threads.
Management Key
Key details, usage and logs, licensees and configurations need your Management Key, which begins uk_. Pass it as management_key:. It grants control of your account, so keep it on your server and out of version control.
client = IdealPostcodes::Client.new(
api_key: "ak_test",
management_key: ENV.fetch("IDPC_MANAGEMENT_KEY")
)
puts client.key_details("ak_test").result.lookups_remaining
A client without a Management Key raises IdealPostcodes::MissingManagementKeyError before sending a request that needs one.
Client options
| Option | Purpose |
|---|---|
api_key: | Required. The API key, or a licensee key beginning sl_ |
management_key: | The Management Key for account operations |
base_url: | Defaults to https://api.ideal-postcodes.co.uk/v1 |
timeout: | Seconds allowed to connect, and again for each read and write. Defaults to 30 |
user_agent: | Your application's identifier, sent ahead of the SDK and Ruby versions, for example my-app/1.0 ideal-postcodes-ruby-sdk/3.0.0 ruby/3.4.1 |
transport: | Replaces Net::HTTP. See below |
Request timeout
Pass timeout: in seconds. A request that runs past it raises IdealPostcodes::TransportError, as described in error handling.
client = IdealPostcodes::Client.new(api_key: "ak_test", timeout: 5)
Custom transport
Yes. Pass any object with a call(request) method as transport:. It receives an IdealPostcodes::Request (http_method, url, headers, body) and returns an IdealPostcodes::Response (status, headers, body). The client builds the request, including credentials, and reads the response, so a transport only moves bytes.
require "faraday"
faraday = Faraday.new
transport = lambda do |request|
response = faraday.run_request(
request.http_method.downcase.to_sym, request.url, request.body, request.headers
)
IdealPostcodes::Response.new(status: response.status, headers: response.headers.to_h,
body: response.body.to_s)
end
client = IdealPostcodes::Client.new(api_key: "ak_test", transport: transport)
A transport is also the simplest way to stub the API in your own tests.
Credentials
The API key goes in the Authorization header of every address, place, email and phone request. The Management Key goes in the same header of every key, licensee and configuration request, and nowhere else. Public operations such as key_availability send no credentials.
To bill requests to a licensee, create a client with the licensee's sl_ key as api_key:.