Setting Headers

When do I use an API?

Every time you send a request to an endpoint it takes time and bandwidth. From your perspective, as an app developer you want to minimise wait times in your app. From the perspective of your API developer you want to keep your bandwidth usage as low as possible - especially if it's something they're paying for and you're using for free. Thankfully these issues are two sides of the same coin and
By the end of this lesson you'll understand how to:
  1. 1.
    Maximise app speeds while minimising API calls.
  2. 2.
    Set WebAPI headers.
​

Overview

According to their docs, icanhazdadjoke.com can be used as an API for fetching a random joke, a specific joke, or searching for jokes in a variety of formats.

Setting API Headers

The API allows us to get jokes in HTML, JSON or plain text format. In order for us to specify which format we would like the response in we need to use a header. In the WebAPI drawer, there are four different properties we can set; Body, Headers, QueryParameters and URL. Going back to the API docs, we are asked to specify an Accept header and a User-Agent header. To request a response in JSON format from the API we set the value of the Accept header to application/json. The user agent header is used as a way for us to identify ourselves to the API developer. Recall that this is a free service and we don't want to take advantage of the generosity of the developer. In this case I've used an email address since it gives the developer a way to contact me directly if there are too many calls being made.

GET Jokes

The API request is quite similar to the one we saw in the previous tutorial. After specifying the URL and the Headers we send a Get request to the API. The if error then do block is for basic error handling and should not be omitted. To display the text of the joke itself we get the joke property from the response object.
get
https://icanhazdadjoke.com/
Fetching a random joke as JSON
​

Read the Joke

Once the joke is displayed on screen we can temporarily "deactivate" the Get Joke button. This serves two purposes. First of all, it gives the user a moment to read (and appreciate) the joke and secondly it prevents users from making multiple requests to the API per second by repeatedly clicking on the button. In the example below we add a pause of 5 seconds, which means that at most a user can request 12 jokes per minute. This still a good number of jokes, and a lot better than not having any pause at all.
​

Ba Dum Tss

Finally, for a bit of visual interest, we add in a random emoji reaction. This is obviously a pretty simple thing to do, but you can easily expand on this by adding some sound effects or a laugh track to your app.

Conclusion

In this section we've looks at why it's a good idea to use a proper API, how to set the URL and make a GET request, how to implement basic error handling, how to convert a JSON response to an Object and pull information out of that object and finally we learned how to set API headers and implement some basic rate limiting.
In the next section we'll increase the complexity slightly and learn how to use APIs that require some basic auth or an access token before we can perform any other operations.
​

Questions?

If you have any questions about this tutorial, please post them in this topic over in the Thunkable Community
​
​
​
Copy link
On this page
When do I use an API?
Overview
Setting API Headers
GET Jokes
get
Fetching a random joke as JSON
Read the Joke
Ba Dum Tss
Conclusion
Questions?