How to retrieve spatial data from APIs, the no code way

Summary

Learn how to retrieve spatial data from APIs without coding! Discover the benefits of using REST APIs for scalable and automated geospatial analysis.

This post may describe functionality for an old version of CARTO. Find out about the latest and cloud-native version here.
How to retrieve spatial data from APIs, the no code way

Looking for a more scalable approach to retrieving spatial data? APIs make this possible, but many organizations are still struggling to realize their potential.

Application Programming Interfaces (APIs)  are an essential component of the Modern Geospatial Tech Stack, with them becoming an increasingly popular way to retrieve data from a service, for use in another.

This blog will guide you through the basics of working with APIs - specifically focusing on retrieving spatial data - and how you can use this to run advanced spatial analysis and create powerful, interactive maps.

APIs: what are they?

APIs may seem like a complicated concept, but they are essentially just a way of allowing different applications to communicate with each other. They make data sharing a faster, more seamless and more scalable process and are a crucial part of building an integrated and interoperable data ecosystem.

An example you might be familiar with is whenever you see the “Login with social media” option to sign into an online account. This is an API which allows that account’s interface to communicate with your social media account. If you’ve ever used something like this, then you’re already using an API!

Utilizing APIs for spatial data offers several advantages:

  • Real-time Data Access: APIs often provide live data feeds, which are crucial for dynamic analysis.
  • Reduced Data Storage: By accessing data directly from APIs, you minimize the need for permanent local storage.
  • Automation: APIs allow for the automation of data retrieval, enabling continuous updates without manual intervention.

What about REST APIs?

Representational State Transfer APIs (REST APIs) have become the spatial industry standard for retrieving, sharing, and managing spatial data in the modern web-based environment. These APIs operate over HTTP, meaning that you make an API request through building a custom URL.

So, what is it about REST APIs in particular that make them such a popular mechanism for retrieving data?

  • Simplicity: REST APIs use standard web protocols (HTTP/HTTPS), making them easy to implement and access without specialized knowledge or advanced coding skills.
  • Statelessness: Each API request is independent, allowing quick, isolated queries for specific geographic data without maintaining a session.
  • Scalability: REST APIs can efficiently handle large numbers of requests, ideal for real-time applications like mapping and location-based services.
  • Interoperability: They support various data formats, such as JSON and GeoJSON, enabling seamless integration into different applications.
  • Wide Adoption: Major spatial data providers like Esri, Google Maps, and OpenStreetMap rely on REST APIs, supported by robust tools and libraries.

Things to consider

So, what’s the catch? While APIs offer numerous benefits, they do come with some challenges. Here’s what you need to watch out for:

  • Complexity: Some APIs can be complex to integrate in your processes, requiring a learning curve, especially for beginners. Many API providers offer extensive documentation and support, making it easier to get started. However, the skill barrier for getting started is always going to be greater than data providers where you can simply bulk downloads.
  • Rate Limits: APIs often have usage limits, which can restrict the amount of data you can access or the frequency of requests.
  • Reliability: API services can sometimes experience downtime or become unavailable which will be outside your control.
  • Versioning Issues: APIs may be updated, potentially breaking your existing integration. To avoid disruptions, developers should monitor API updates. If you require a static snapshot of the data - rather than ongoing live updates - consider saving a permanent copy.

So, now you’re aware of the benefits and challenges of working with APIs, ready to get started?

Getting Started: How to Retrieve Spatial Data Using APIs

Now that you have a solid understanding of what APIs are, as well as the pros and cons of using them, let’s dive into the practical steps for retrieving spatial data using an API. We’ll walk through a basic example of how you can use a REST API to access and use spatial data, even if you’re just starting out.

Step 1: Choose the Right API

The first step is to select an API that provides the spatial data you need. There are a wide range of APIs available, with some popular sources including:

  • OpenStreetMap API: fetch data from the “Wikipedia for maps” topographic database. OpenStreetMap is a fantastic free data resource but - due to its crowdsourced nature - comes with certain challenges; make sure to check out our guide to navigating it!
  • Google Maps API: access a wide range of data from building solar potential to air pollen. You can also leverage the Google Maps Streetview API to automatically generate Streetview images for your map pop-ups - check out our guide to doing this here!
  • USGS Earthquake Hazards Program APIobtain real-time and historical earthquake data from the United States Geological Survey, including data on magnitude, and depth, which is freely accessible and ideal for disaster monitoring.
  • NASA Earthdata API: Access a variety of satellite imagery and earth observation data from NASA, as well as data relating to climate, weather, and environmental monitoring.

Please note that whilst many APIs are open, many require an account or access token. To learn how to use these, you’ll need to…

Step 2: Understand the API documentation

Once you’ve found your API, it’s crucial to read through its documentation. This will provide you with the necessary details on how to structure your requests, what data formats are available, and any limitations or authentication requirements.

While API documentation can often feel overwhelming, many providers are increasingly offering UI functionality to help you easier build your requests. For example, Transport for London’s API allows users to input required parameters into a Swagger wizard, which generates a REST URL for them.

Step 3: Make your first API request

To make a request, you’ll need to construct a URL that includes the base URL, the desired endpoint, and any parameters. Here’s a basic example using a hypothetical API:

   
https://api.example.com/v1/data?type=geospatial&limit=10&format=json
   

Let’s dig into this, piece by piece.

  • Base URL: https://api.example.com – The server where the API is hosted.
  • Endpoint Path: /v1/data – Access the data resource, version 1.
  • Query Parameters: 
    • type=geospatial – Request geospatial data.
    • limit=10 – Limit the response to 10 items.
    • format=json – Receive the response in JSON format.

This is just a taste of what you can achieve with parameters in APIs! You can also select features within a geographic area, break large datasets into smaller sections and set the output spatial reference system.

Check out the far more complex example below, which is tailored to fetch data that is geographically located within a defined rectangular and polygonal area, intersects with a specific geometry, falls within a certain distance, and meets specific time, attribute, and sorting criteria. The results are paginated and formatted according to the specified coordinate system, ensuring a precise and manageable dataset for analysis.

   
https://api.example.com/v1/features?bbox=-125.0,24.396308,-66.93457,49.384358&geometry=POLYGON((-120 40, -120 45, -115 45, -115 40, -120 40))&spatialRel=esriSpatialRelIntersects&distance=5000&units=meters&time=2024-01-01T00:00:00Z,2024-01-31T23:59:59Z&fields=name,location,description&sort=name asc&page=2&limit=50&where=population>1000000&outSR=4326
   

So now you’re starting to get familiar with how REST APIs are structured, you’ll probably want to work out how to integrate these into your existing applications.

Step 4: Integrate the data into your applicatione

After retrieving the data, the next step is to integrate it into your spatial analysis or mapping tool.

In CARTO, we aim to make this really easy and straightforward. In our no-code tool CARTO Workflows, you can leverage the component Import from URL to retrieve data from a URL. You can then treat this as you would any other data source and integrate it into your wider analysis.

A screenshot of CARTO Workflows

Want to give it a try? Check out our brand new tutorial Using data from a REST API for real-time updates on the CARTO Academy. In this tutorial, you’ll be building the map below, which extracts hourly air quality data from the US Current Air Quality ArcGIS REST API.

Not a CARTO user? Sign up for our FREE 14-day trial to give this a go yourself!

What’s Next?

Now that you’ve seen how straightforward it can be to retrieve spatial data using an API, it’s time to put this into practice! APIs in geospatial can be used for so much more than just retrieving data - check out our Documentation for more ideas, from developing custom applications to triggering external processes via API.

If you’re looking for more guidance on how you can build out a more modern geospatial tech stack, request a demo with one of our experts today!