APIs: What A Concept!

APIs: What A Concept!

A Comprehensive Breakdown of APIs

Introduction

APIs are very important in the work of virtually every developer. A well-written API can provide significant long-term advantages. It is therefore essential to know what exactly APIs are and what they are used for. In this article, I will attempt to shed more light on APIs using a very simple example.

What is an API?

API is an acronym for "Application Programming Interface", which serves as a software interface that allows for communication between two applications. In other words, an API can be said to be a messenger, because it delivers your request to the provider you have requested it from and sends the response back to you. Most large companies have built APIs either for free use by their customers or for internal use. You might be wondering why these companies even bother building APIs. Why are APIs important?

Importance of APIs

It is important to build or use APIs for the following reasons:

  • APIs provide an official way of accessing any application data.

  • APIs allow content to be embedded from any site or application more easily. This guarantees more fluid information delivery and an integrated user experience.

  • They can be reusable.

  • They increase developer productivity.

  • They enable developers to build apps with speed.

  • Developers don't have to write code from scratch.

How does an API work?

The most popular analogy used for explaining the concept of APIs is the "Restaurant Analogy".

13198.jpg

Imagine you are a customer in a restaurant, sitting at a table with a menu to order from. There is a waiter going around to attend to people one at a time and take their orders and finally, there is a chef in the kitchen who is preparing the orders.

You need a way to communicate your order to the chef in the kitchen and then to deliver your order back to you. The chef who is busy in the kitchen is not available to do that, so there has to be some other way for you, the customer who is ordering the food, and the chef who prepares it, to connect. This is where the waiter comes in.

The waiter serves as the API and takes your order(request), delivers it to the chef, telling the chef what to prepare. S(he) then delivers the food(response) back to you.

If the customer makes an order that is not found on the menu, the customer gets a 404 response - resource(food) not found!

So in summary, APIs serve as interfaces to communicate between two applications (i.e the customer and the chef). An API works by taking requests from clients (e.g your application) and sending the requests to servers and then delivering the response back to the clients.

Companies that provide APIs for use often have documentation. API documentation is a specialized content deliverable, containing information about how to viably use and integrate with an API. When building APIs, it is important to write the documentation so that users can know the appropriate way to make use of the API.

Example of API

We will be building a very simple application using a simple API called "The Official Joke API". This is an API that you can use to request a random joke or multiple jokes depending on the endpoint called. An endpoint, simply put, is the point at which an API connects with your application. Some of the endpoints of the Official joke API are: Grab a random joke! official-joke-api.appspot.com/jokes/random

Grab ten random jokes! official-joke-api.appspot.com/random_ten

We will use the second endpoint to generate ten random jokes to display on a webpage.

If you paste the endpoint into a browser, you get something like this:

Screenshot 2021-04-02 at 16.37.06.png

The response from this API has a JSON format. Some APIs return their response in an XML format. Note: Your result may look compressed compared to mine. If you would like it to look similar to what is shown above, you can install the "JSON formattter" Chrome extension

Now let's write some code:

With this very simple example, we can see how we can use JavaScript to get data from an API to display some information on a webpage.

Conclusion

APIs can go from simple to complex, but most of the concepts remain the same - reading the documentation, calling the endpoint and integrating the API. Having knowledge of APIs and how to incorporate them is a great skill to have and makes one a better Software Developer overall.

References

Dufuna CodeCamp

People vector created by pch.vector - www.freepik.com