SDKs overview
Software Development Kits (SDKs) and client libraries for PostalCodes facilitate programmatic interaction with its suite of APIs, which include zip code lookups, reverse geocoding, and country-specific postal code data. These tools abstract the underlying HTTP requests, authentication, and response parsing, allowing developers to focus on application logic rather than low-level API communication. SDKs typically provide language-specific methods and data structures that map directly to API endpoints and data models, streamlining integration and reducing development time. While official SDKs are maintained directly by PostalCodes, community libraries are developed and supported by the broader developer ecosystem.
The PostalCodes API is designed for simplicity in accessing postal code and basic location data, with documentation focused on common use cases. This approach influences the design of its SDKs, which aim for ease of use and quick integration into various applications requiring geographical data services.
Official SDKs by language
PostalCodes provides official SDKs to support direct integration into applications built with popular programming languages. These SDKs are maintained by PostalCodes to ensure compatibility with the latest API versions and features. They typically include functionalities for all core products, such as zip code lookups, reverse geocoding, and radius searches. Each SDK is designed to align with the idiomatic practices of its respective language, offering a familiar development experience.
| Language | Package Name | Install Command | Maturity |
|---|---|---|---|
| Python | postalcodes-python |
pip install postalcodes-python |
Stable |
| Node.js | @postalcodes/nodejs |
npm install @postalcodes/nodejs |
Stable |
| PHP | postalcodes/php-client |
composer require postalcodes/php-client |
Stable |
| Ruby | postalcodes-ruby |
gem install postalcodes-ruby |
Stable |
For detailed documentation and API references for each official SDK, developers can consult the PostalCodes API documentation portal.
Installation
Installing PostalCodes SDKs typically follows the standard package management practices for each programming language. The following sections provide common installation commands for the officially supported languages.
Python
To install the Python SDK, use pip, the Python package installer. Ensure you have a compatible Python version installed (Python 3.7+ is generally recommended for modern libraries).
pip install postalcodes-python
For virtual environments, consider creating one before installation to manage dependencies effectively. Information on Python package management is available from the official Python documentation.
Node.js
The Node.js SDK is available via npm (Node Package Manager). Navigate to your project directory and execute the following command:
npm install @postalcodes/nodejs
Alternatively, if you are using Yarn:
yarn add @postalcodes/nodejs
Consult the npm install documentation for more details on package installation options.
PHP
For PHP projects, the SDK is distributed through Composer, the dependency manager for PHP. Add the package to your composer.json file or run the command:
composer require postalcodes/php-client
Ensure Composer is installed and configured in your development environment. The Composer installation guide provides instructions for various operating systems.
Ruby
The Ruby SDK is available as a gem. Use the gem install command:
gem install postalcodes-ruby
For projects using Bundler, add gem 'postalcodes-ruby' to your Gemfile and then run bundle install.
Quickstart example
This section provides a quickstart example using the Python SDK to perform a basic postal code lookup. This example assumes you have already installed the postalcodes-python package and have obtained an API key from your PostalCodes account.
import os
from postalcodes import PostalCodesClient
# Replace with your actual API key or set as an environment variable
API_KEY = os.environ.get("POSTALCODES_API_KEY", "YOUR_API_KEY_HERE")
client = PostalCodesClient(api_key=API_KEY)
def get_postal_code_info(country_code, postal_code):
try:
response = client.lookup_postal_code(country=country_code, postal_code=postal_code)
if response.is_success:
data = response.data
print(f"Postal Code: {data.get('postal_code')}")
print(f"Country: {data.get('country_name')} ({data.get('country_code')})")
print(f"City: {data.get('city')}")
print(f"State: {data.get('state')}")
print(f"Latitude: {data.get('latitude')}")
print(f"Longitude: {data.get('longitude')}")
else:
print(f"Error: {response.error_message}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
# Example usage:
get_postal_code_info("US", "90210")
get_postal_code_info("GB", "SW1A 0AA")
get_postal_code_info("CA", "M5V 2H1")
This Python snippet initializes the client with an API key, then calls the lookup_postal_code method to retrieve information for a given country and postal code. The response object is then checked for success, and relevant data points are printed. Remember to replace "YOUR_API_KEY_HERE" with your actual PostalCodes API key or configure it as an environment variable for production applications.
Community libraries
Beyond the official SDKs, the PostalCodes API can be integrated using community-contributed libraries or by making direct HTTP requests. Community libraries often emerge to support languages or frameworks not officially covered, or to provide specialized functionalities or integrations. These libraries are developed and maintained by independent developers and may offer different levels of support, features, and stability compared to official SDKs.
When considering a community library, it is advisable to review its documentation, GitHub repository activity, and community support to assess its reliability and currency. Developers may also choose to interact with the PostalCodes API directly using standard HTTP client libraries available in virtually all programming languages, such as requests in Python, axios in JavaScript, or Guzzle in PHP. This approach offers maximum flexibility but requires manual handling of request construction, authentication, and response parsing, as detailed in the PostalCodes API Reference.
For example, a direct HTTP request in Python using the requests library might look like this:
import requests
import os
API_KEY = os.environ.get("POSTALCODES_API_KEY", "YOUR_API_KEY_HERE")
BASE_URL = "https://api.postalcodes.net/v1/"
def lookup_postal_code_http(country_code, postal_code):
endpoint = f"lookup?country={country_code}&postal_code={postal_code}"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
try:
response = requests.get(f"{BASE_URL}{endpoint}", headers=headers)
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
data = response.json()
if data and data.get('status') == 'success':
print(f"Postal Code: {data.get('data', {}).get('postal_code')}")
print(f"Country: {data.get('data', {}).get('country_name')}")
# ... print other fields as needed ...
else:
print(f"API Error: {data.get('message', 'Unknown error')}")
except requests.exceptions.HTTPError as errh:
print(f"HTTP Error: {errh}")
except requests.exceptions.ConnectionError as errc:
print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
print(f"Something went wrong: {err}")
# Example usage:
lookup_postal_code_http("US", "90210")
This direct approach demonstrates the flexibility of interacting with RESTful APIs, a common pattern described in web development resources like the Mozilla Developer Network HTTP documentation, which outlines the fundamentals of HTTP requests and responses.