Integrate geocoding the right way
Tags: geocode, javascript, api, node
Published: 2022-04-19 11:55:27

This is the first article in a series covering integrating geocode to Node.js applications in a way that lets you:

- Create an integration that never breaks
- Use different API providers based on location
- Make sure you always get the data you need

## There is no single best provider

When integrating geocoding, most developers start by comparing a couple of geocoding API providers, choose the seemingly best one and bake it in their app.

However, there is no single best Geocoding API. Moreover, getting familiar with every considered provider takes time, and hard-wiring just one achieves nothing but vendor lock.

## Integrate them all

Instead of looking for a unicorn, [integrate the business use-case](https://dev.to/superface/stop-the-manual-api-plumbing-5639) and access any provider of preference at the runtime. This can be achieved using [Superface](https://www.superface.ai)'s open source OneSDK, which serves as a universal interface to many APIs. Here’s how to do it.

## Integrate geocoding using Superface OneSDK

The following steps are pretty straightforward, however if this is your first run with Superface or you just want a better understanding of what’s happening under the hood, [read the documentation](https://superface.ai/docs).

1. Install Superface OneSDK package in your project folder.
    
    ```bash
    npm install @superfaceai/one-sdk
    ```
    
2. Install **`address/geocoding`** profile.
    
    ```bash
    npx @superfaceai/cli install address/geocoding
    ```
    
3. Configure desired providers. `Nominatim` offers their API publicly. No keys required.
    
    ```bash
    # Here.com
    npx @superfaceai/cli configure here -p address/geocoding
    
    # Google Maps
    npx @superfaceai/cli configure google-apis-maps -p address/geocoding
    
    # Azure Maps
    npx @superfaceai/cli configure azure -p address/geocoding
    
    # OpenCage
    npx @superfaceai/cli configure opencage -p address/geocoding
    
    # TomTom
    npx @superfaceai/cli configure opencage -p address/geocoding
    
    # Nominatim
    npx @superfaceai/cli configure nominatim -p address/geocoding
    ```
    
    If you need another provider, [let me know.](mailto:v@superface.ai)
    
4. Obtain API keys for the desired providers and set them as an environment variable. 
    
    ```bash
    # Here.com
    export HERE_API_KEY=your-value-from-here
    
    # Google Maps
    export GOOGLE_APIS_MAPS_API_KEY=your-value-from-google-maps
    
    # Azure Maps
    export AZURE_API_KEY=your-value-from-azure
    
    # OpenCage
    export OPENCAGE_API_KEY=your-value-from-opencage
    
    # TomTom
    export TOMTOM_API_KEY=your-value-from-tomtom
    ```
    
    How to get API keys: [Here.com](https://superface.ai/docs/tutorials/getting-api-keys/here-com) | [Google Maps](https://superface.ai/docs/tutorials/getting-api-keys/google-maps) | [Azure Maps](https://superface.ai/docs/tutorials/getting-api-keys/azure-maps) | [OpenCage](https://superface.ai/docs/tutorials/getting-api-keys/opencage) | [TomTom](https://superface.ai/docs/tutorials/getting-api-keys/tomtom)
    
5. Use the following code in your Node.js application and replace the example address with your input:
    
    ```jsx
    const { SuperfaceClient } = require('@superfaceai/one-sdk');

    async function Geocoding() {
      const sdk = new SuperfaceClient();
     
      // Load the installed profile
      const profile = await sdk.getProfile('address/geocoding');
      
      // Choose the provider
      const provider = await sdk.getProvider('nominatim');
      
      // Use the profile
      const result = await profile
        .getUseCase('Geocode')
        .perform({
          addressCountry: 'United States',
          addressLocality: 'Manhattan',
          postalCode: 'NY 10036',
          streetAddress: 'Times Square',
        },
        { provider }
      );
      
      // Check out the result
      try {
        const data = result.unwrap();
        console.log(data);
      } catch (error) {
        console.error(error);
      }
    }
      
    Geocoding();
    ```
    
6. Run the code.
    
    Calling the `Geocoding` function should return a similar result:
    
    ```bash
    { latitude: '40.757280550000004', longitude: '-73.98585503545917' }
    ```
    

And that’s it. Your basic integration is ready. 

## Read next

- [Use different providers for different locations](https://dev.to/superface/using-a-different-geocode-service-for-different-countries-2j07)