How can I consume custom APIs with dashdash?

New to APIs? Never used web requests in your life? You think only developers can do this? :grimacing: :persevere:
Wrong! You shouldn’t be afraid any longer. With dashdash, it’s simple. :fireworks: Here’s how:

What is an API?
API is the acronym for Application Programming Interface. Essentially, these are interfaces through which online services exchange data. APIs have endpoints that structure data in a particular way. If you ask the API for data or if you send it data in this particular way, you can interact with it. This is done by web requests, such as GET and POST. Most APIs have good documentation that explain you how to interact with it.

If you need to grab or send data, and you think there should be an online service to do it, there probably is. Just try googling API for X and you likely find what you need and you can click to read through the API documentation. Again, this might be hosted on developers.xxx.com, but this is not only for devs. Now, it’s also for you! :raised_hands:

How can I use APIs with dashdash?
dashdash currently supports GET and POST as formulas for generic web requests.

You need to find out:

  1. What API you want
  2. How to send the request
  3. What the response looks like
  4. How to authenticate your request
  5. How to copy everything into the dashdash formula

Let’s find out using an example

1. What API you want
Let’s say we want to find the address of businesses in our city. So we google API for finding a business address. Among the first hits are the Google Places and Google Maps Geocoding APIs. There are many more, but let’s try those (you actually find out after one click that the Google Places API is just an umbrella for many different ones).

2. How to send the request
So we are in the Google Maps Geocoding API documentation, and we are expecting that we can request something like Apple NYC and get an address of a business called “Apple” in “NYC” back from the API server.
In this part of the documentation, you find how to send the request. It looks like this:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

3. What the response looks like
If we send such a request to this API, it will send back a response as a JSON file. You find the example right below the request example. It looks like this:


{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

Learn how to organize this neatly into cells with PARSE here.

4. How to authenticate your request
You probably noticed that last part of the request example: ...key=YOUR_API_KEY. This is because of API authentication. Some APIs don’t require any authentication but most do. You can simply look for Authentication or Get API Key in the documentation or in your account. In this example, there’s a Get API Key link on the left-side menu. You can simply get yours there.

5. How to copy everything into the dashdash formula
Finally, you can copy everything together in your formula. It will look like this:

=GET("https://maps.googleapis.com/maps/api/geocode/json?address=Apple+NYC&key=AIzaSyCxAbPzVrPCNG8X2pDaXXXXXX-XXXXXXXX")

Of course, you can now decide to write your business search term in another cell, say in A1, and you reference the request dynamically to it in the way you already know how to do it with spreadsheets.

=GET("https://maps.googleapis.com/maps/api/geocode/json?address="&A1&"&key=AIzaSyCxAbPzVrPCNG8X2pDaXXXXXX-XXXXXXXX")

The content of your cell will now show the JSON. And if you know how to use PARSE it, you can easily grab the address, e.g. in this case with

=PARSE(B1,"results.[0].formatted_address")

Congratulations! That’s it! You now learned how to interact with APIs using dashdash! Get business addresses, company information, stock prices, send emails or SMS? You name it. There are thousands and thousands of APIs waiting for you on the web. :raised_hands::raised_hands::raised_hands:

And we are working hard to make this easier and easier. Stay tuned!

1 Like