API for Beginners

API for Beginners
Image by Danni Liu adapted from FG Trade/ Canva

For a long time, the API acronym meant one thing to me: Australian Pharmaceutical Industries. It is a health and beauty company in Australia involved in pharmaceutical distribution, retailing and manufacturing. When I worked in pharmacies, I used to order stock from them all the time. The API now dominates in my mind changed when I met my ex. He introduced me to Application Programming Interface. I can't recall the event that led to the topic, but I do remember the example he used to explain what it is to me.

Several years have passed since then. The only knowledge of API in the computing world I carried in my head was that example until recently. If you've been following my blogs, you'd note that my recent blogs are linked to web scraping. Needless to say, I've spent many hours nerdying over this topic. During the course, API made several appearances in my readings, which sent me to explore API and inspired this blog.

In this blog, I'll cover the following:

  • What is an API?
  • How API is used
  • What does API have to do with web scraping?
  • How API works
  • Types of APIs
  • How to Interact with APIs
  • Who may need to understand APIs

What is an API?

API stands for Application Programming Interface. It is a set of rules that specifies how two software programs should communicate with each other. A program can call another program's API to get access to data or functionality of the other program. Does it sound abstract? Let me share with you the non-technical example my ex shared with me.

Picture yourself at a restaurant. A waiter approaches and takes your order, and lets the kitchen know of your order. The kitchen then prepares your order, and the waiter returns with your order. In this example, you are one program, and the kitchen is the second program. The waiter represents the API used to receive requests and return something.

How API is Used

APIs are pretty ubiquitous. It can be used in a variety of ways, such as:

  • allowing different parts of a system to communicate with each other
  • exposing the functionality of an application to other developers so they can build their applications on top of it
  • enabling integration between different systems and platforms
  • allowing others to access and use data or functions

Here are some examples of APIs:

  • Twitter API- allows developers to access Twitter data such as tweets, users, and trends for use in their applications
  • Google Maps API- allows developers to add maps and location-based functionality to their websites and app
  • OpenWeather API- provides current weather data and forecasts for any location on Earth
  • Spotify Web API- allows developers to access music Spotify's music catalogue, user data, and playlists for use in their applications

What does API have to do with Web Scraping?

API is an alternative to web scraping to extract data from websites. With API, we can request specific data from a website using a specific URL and a set of parameters. The website will then return the requested data in a structured format, often in JSON, which stands for JavaScript Object Notation. It is easier to work with than web scraping because it provides a consistent and predictable way to access data.

With web scraping, we extract data from websites by programmatically accessing the website's HTML code and extracting the relevant data. The data returned from web scraping is typically unstructured, and we must write code to parse the data and extract the relevant information.

It is recommended that an API be used when the website we are trying to extract data have one available. Web scraping can be used when the data we need is unavailable through an API or if the website we are trying to access does not have one.

How API Works

When an application, such as a website or mobile app, wants to retrieve or manipulate data from another system, it requests that system's API using a specific set of commands. The other system's API then processes the request, returns the requested data, or performs the desired action. This process allows for different systems to communicate and share data with each other without having to share their entire codebase.

Below is an adorable illustration I came across on Rapid that explains this. Rapid API is a popular API marketplace that allows developers to discover and connect to thousands of APIs.

What is an API definition
Definition of API by Rapid

Types of APIs

There are several types of APIs:

  • Private APIs- these are internal to a company and are used for connecting systems and data within the business.
  • Public APIs- these are available to anyone to use
  • Partner APIs- these are available to authorise external developers to aid business-to-business partnerships
  • Composite APIs- these are a combination of two or more APIs to address complex system requirements

How to Interact with APIs

There are several ways to interact with APIs. Here are some common ways to interact with APIs:

  • Using a browser- some APIs allow you to make requests and view responses directly in your web browser by accessing the API's endpoint URL. An example of this is Spotify API.
  • Using a Terminal/ Command Prompt- some APIs can be accessed using a command-line tool such as Terminal in Mac and Command Prompt in Windows. This allows you to make requests and view responses from a Terminal or Command Prompt. Command line is a text-based interface that allows you to communicate with the computer; it's that ugly scary text screen I believe many of you would have seen. I'm scared to play in it. I feel that one wrong move will mean the end of my computer, even though I've been told it's incredibly safe.
  • Using an API client- an API client is a piece of software that allows you to interact easily with an API. It's more user-friendly than using a browser or command line tool. A popular API client is, Postman.
  • Using a programming language- you can interact with an API by writing code in programming languages like Python, Java, or JavaScript.

It's worth noting that many API providers will provide documentation with examples of how to interact with the API using different methodologies. Here is one you can check out: Spotify Search API Documentation.

Who May Need to Understand APIs

Several groups of people may need to understand APIs:

  • Developers- Developers who work on building and integrating software applications will need to know how to use APIs to access data or functionality from other systems. They will need to know how to make requests to an API, handle responses from an API, and integrate the data or functionality provided by the API into their application.
  • Project Managers- Project managers responsible for building software applications that rely on APIs need to understand how APIs work to plan and manage projects effectively. They need to know how to use APIs to identify dependencies on external systems and plan to integrate those systems into the project.
  • IT Professionals- IT professionals who work on maintaining and troubleshooting software systems will need to understand how APIs work to diagnose and fix problems that may arise when integrating different systems. They will also need to understand how to monitor and secure APIs to ensure that they are functioning properly and that the data they provide is secure.
  • Business Professionals- Business professionals responsible for using data and functionality provided by APIs to make decisions and drive business growth will need to understand how APIs work to effectively utilise the data and functionality provided by the APIs. I fit into this category.

There is a lot more to API. I think this is sufficient for where I am in my journey. I'm glad I spent the time delving into API. It's opened my eyes to worlds of possibilities that weren't known to me previously. I hope I was able to provide you with a glimpse of just how amazing API is and its potential.