How to build Headers in GET with API Authentication Types

Hi fellow dashdashers :wave:

With our GET and POST functions, you can talk to any API. An API (short for application programming interface) is a shortcut way to communicate with a desired service. Most APIs require authentication.

You usually use the Header parameter for authentication. Here are the most common examples on how to build them correctly in dashdash depending on the authentication type :man_teacher::woman_teacher:

  1. API key
  2. Bearer Token
  3. Basic Auth (Base64)
  4. OAuth or OAuth 2.0

:one: API key
The easiest method for API authentication is done via API keys. Here you pass the key directly in the URL, so you don’t need a Header parameter. The key aka token is usually found in your personal account to the service you are trying to integrate. It is merely attached to the API URL as another parameter ?key=[YOUR KEY] :star_struck:

Let’s use our Google Maps Geocoding example to clarify:

A1 = API key
A2 = address
A3 = GET("https://maps.googleapis.com/maps/api/geocode/json?key="&A1&"&address="&A2&"")

:two: Bearer Authentication
The Bearer Token is an authentication method based on HTTP Protocol. With your API request you are basically sending along an ‘Authorization’ header bearing your authentication token (e.g. API key). Let’s break this down :wink:

A header has a JSON format and looks like this:
Header structure: '{"[PARAMETER_1]":"[PARAMETER_2 ]"}'
Header example: '{"Authorization": "Bearer [YOUR API KEY]"}'

The easiest way to avoid getting some space, quote or apostrophe wrong is using our PAIR2JSON function. In our Mattermark example, this looks like this:

A1 = API key
A2 = object id
A3 = "Authoritation"
A4 = "Bearer "&A1
A5 = PAIR2JSON(A3,A4)
A6 = GET("https://api.mattermark.com/companies/"&A2&"/people",A5)

If you want to build the JSON manually, you can do that, too, as follows:

A1 = API key
A2 = object id
A3 = GET("https://api.mattermark.com/companies/"&A2&"/people",'{"Authorization ": "Bearer '&A1&'"}')

:three: Basic Authentication (Base64)
This method also uses HTTP headers; but here you need to pass your authentication token as a base64-encoded string containing username:password. Syntax is slightly different, but the scheme is the same. So, as easy as ABC by now :muscle::triumph:

Header example_1: '{"Authorization":"Basic '&BASE64("username:password")&' "}'
Header example_2: '{"Authorization":"Basic '&BASE64([API KEY]":")&' "}'

Sometimes you are only required to pass the API key as a base64-encoded header without a password (see header example_2 above). Other times, you might be required to pass your API key as a username including a password, as in our Shopify case :female_detective::male_detective:

A1 = username
A2 = password
A3 = GET("https://[subdomain].myshopify.com/admin/orders.json",'{"Authorization":"Basic '&BASE64(A1&":"&A2)&'"}')

The alternative using PAIR2JSON would be this:

A3 = GET("https://[subdomain].myshopify.com/admin/orders.json",PAIR2JSON("Authorization","Basic "&BASE64(A1&":"&A2)))

:four: OAuth 2.0
OAuth 2.0 is a bit trickier as it requires a couple of steps before accessing the API with GET/POST requests. Also, as this process can vary from between APIs, we would recommend first diving into quick guide on OAuth 2.0 (super simple guide and easily understood :relieved::+1:).

Once you understand the principle, you just need to get an authorization token with a first GET or POST function, and then pass this token as a parameter with a second GET or POST function.

You can also always ask us for help :wink:

4 Likes

This is gold @guven2.0! Could you just help to figure out what would be the structure for Algolia (see doc here for the Analytics REST API)?

Hey @jprcoliveira :vulcan_salute:

I just got an Algolia account and tested with the Get Top Searches endpoint. The API returns an empty JSON for me, which is expected as I don’t have any data stored in Algolia.

Here’s what I did:

= GET(“https://analytics.algolia.com/2/searches?index=INDEX_NAME”,'{ "X-Algolia-API-Key":"API_KEY", "X-Algolia-Application-Id":"APPLICATION_ID" }’)

Pro tip :muscle::nerd_face:
If you need to create headers to pass api key’s or tokens use PAIR2JSON(). Here’s how you can do it:

A1 = X-Algolia-API-Key
A2 = 10b8689cb46cb6a491....
B1 = X-Algolia-Application-Id
B2 = C1F6CXJ...
C1 = PAIR2JSON(A1,A2,B1,B2)
C2 = GET("https://analytics.algolia.com/2/searches?index=INDEX_NAME", C1)

Let me know if you have any further questions!

Happy building :construction_worker_man::construction_worker_woman::building_construction:

2 Likes