SDKs overview

OpenCage offers a range of Software Development Kits (SDKs) and client libraries designed to facilitate interaction with its Geocoding and Reverse Geocoding APIs. These tools streamline the process of integrating location-based services into various programming environments by providing idiomatic interfaces for common tasks such as sending geocoding queries and parsing API responses. The available SDKs cover several widely used programming languages, including official and community-maintained options, ensuring broad accessibility for developers. The primary goal of these libraries is to abstract the complexities of direct HTTP requests, authentication, and error handling, allowing developers to integrate geocoding functionality with fewer lines of code and reduced development effort. OpenCage's API primarily uses data from OpenStreetMap, a collaborative project to create a free editable map of the world, along with other open data sources, which is accessed through these SDKs for global address lookup and reverse geocoding needs OpenCage API Overview.

Official SDKs by language

OpenCage provides official client libraries for several programming languages, which are actively maintained to ensure compatibility with the latest API features and best practices. These libraries are typically available through standard package managers for each respective language, simplifying installation and dependency management. Each official SDK aims to offer a consistent developer experience, providing methods for performing both forward geocoding (converting addresses to coordinates) and reverse geocoding (converting coordinates to addresses). They handle aspects like API key authentication, request formatting, and response parsing, returning data structures native to the language.

Language Package Name/Repository Installation Command (Example) Maturity
Python opencage pip install opencage Stable
Ruby opencage-api-client gem install opencage-api-client Stable
PHP opencage/opencage-api-client composer require opencage/opencage-api-client Stable
Node.js opencage-api-client npm install opencage-api-client or yarn add opencage-api-client Stable
Java com.opencagedata:opencage-geocoding-java Maven/Gradle dependency Stable
Go github.com/opencagedata/go-opencage go get github.com/opencagedata/go-opencage Stable
Elixir opencage_geocoding Add to mix.exs dependencies Stable
Rust opencage Add to Cargo.toml dependencies Stable
C# OpenCage.Geocoding Install-Package OpenCage.Geocoding Stable

Installation

Installing an OpenCage SDK typically involves using the package manager specific to the programming language you are working with. The process is designed to be straightforward, allowing developers to quickly add the library to their project dependencies. Comprehensive installation instructions and detailed usage guides for each official client library are available in the OpenCage API client documentation.

Python

To install the Python client:

pip install opencage

Ruby

To install the Ruby client (Gem):

gem install opencage-api-client

PHP

The PHP client is installed via Composer:

composer require opencage/opencage-api-client

Node.js

For Node.js projects, use npm or yarn:

npm install opencage-api-client
# or
yarn add opencage-api-client

Java

For Java, include the dependency in your Maven pom.xml or Gradle build.gradle file. For Maven:

<dependency>
    <groupId>com.opencagedata</groupId>
    <artifactId>opencage-geocoding-java</artifactId&n>
    <version>(latest version)</version>
</dependency>

Replace (latest version) with the current stable version found in the OpenCage Java client documentation.

Go

Install the Go client using the standard go get command:

go get github.com/opencagedata/go-opencage

Elixir

Add {:opencage_geocoding, "~> (latest version)"} to your list of dependencies in mix.exs:

def deps do
  [
    {:opencage_geocoding, "~> (latest version)"}
  ]
end

Then run mix deps.get. Refer to the OpenCage Elixir client page for the latest version.

Rust

Add the opencage crate to your Cargo.toml file:

[dependencies]
opencage = "(latest version)"

The current version can be found in the OpenCage Rust client documentation.

C#

Install the C# client via NuGet:

Install-Package OpenCage.Geocoding

Quickstart example

The following examples demonstrate how to perform a basic geocoding query using the official OpenCage SDKs. These snippets assume you have already installed the respective client library and have an OpenCage API key available. For more complex queries, such as reverse geocoding, filtering results, or handling specific language outputs, refer to the detailed documentation for each client library.

Python Quickstart

from opencage.geocoder import OpenCageClient

API_KEY = "YOUR_OPENCAGE_API_KEY"
geocoder = OpenCageClient(API_KEY)

query = "Eiffel Tower, Paris"
results = geocoder.geocode(query)

if results and len(results['results']) > 0:
    first_result = results['results'][0]['geometry']
    print(f"Latitude: {first_result['lat']}, Longitude: {first_result['lng']}")
else:
    print("No results found.")

Ruby Quickstart

require 'opencage/api_client'

API_KEY = 'YOUR_OPENCAGE_API_KEY'
client = OpenCage::ApiClient.new(api_key: API_KEY)

query = 'Eiffel Tower, Paris'
response = client.geocode(query)

if response.ok?
  first_result = response.results.first.geometry
  puts "Latitude: #{first_result['lat']}, Longitude: #{first_result['lng']}"
else
  puts "Error: #{response.status.message}"
end

PHP Quickstart

require_once 'vendor/autoload.php';

use OpenCage\Geocoding\Geocoder;

$apiKey = 'YOUR_OPENCAGE_API_KEY';
$geocoder = new Geocoder($apiKey);

$query = 'Eiffel Tower, Paris';
$response = $geocoder->geocode($query);

if ($response && count($response['results']) > 0) {
    $firstResult = $response['results'][0]['geometry'];
    echo "Latitude: " . $firstResult['lat'] . ", Longitude: " . $firstResult['lng'] . "\n";
} else {
    echo "No results found.\n";
}

Node.js Quickstart

const OpenCageClient = require('opencage-api-client');

const API_KEY = 'YOUR_OPENCAGE_API_KEY';

async function geocodeAddress() {
  try {
    const data = await OpenCageClient.geocode({
      q: 'Eiffel Tower, Paris',
      key: API_KEY,
    });

    if (data.results.length > 0) {
      const firstResult = data.results[0].geometry;
      console.log(`Latitude: ${firstResult.lat}, Longitude: ${firstResult.lng}`);
    } else {
      console.log('No results found.');
    }
  } catch (error) {
    console.error('Error during geocoding:', error);
  }
}

geocodeAddress();

Community libraries

Beyond the officially supported SDKs, the OpenCage API's broad utility has led to the development of several community-contributed client libraries and integrations across various programming languages and platforms. These community projects often fill gaps for less common languages or integrate OpenCage's functionality into specific frameworks or tools. While not officially maintained by OpenCage, they can offer valuable alternatives for developers whose tech stacks are not covered by official SDKs. Developers are advised to check the project's documentation and community support when using these libraries, as their maintenance and compatibility may vary. A comprehensive list of community-contributed libraries is typically aggregated on the OpenCage website or within developer forums, reflecting the API's adaptability to diverse development environments OpenCage community client list. For example, integrations with geographic information systems (GIS) or specific web frameworks might be available through community efforts, extending the reach of the OpenCage API beyond its core client libraries. The OpenStreetMap project, which OpenCage heavily relies on, also has a vibrant developer community that often contributes to related geospatial tools. Exploring OpenStreetMap developer resources can sometimes lead to additional community tools that complement OpenCage integrations.