Hiring Partners
Segilola Resources Operating LimitedTaviatech TechnologiesFCMBMTN NigeriaAdvans La Fayette Microfinance BankAboki AfricaFirst Bank of NigeriaOPay
โ† Introduction to APIs for Data ยท Lesson 3 of 9

Module Two

๐Ÿ“– Every lesson in this course is free to read right here, no account needed. Create a free account to track your progress, take the exam, and earn your certificate.
3
Module 2 - How APIs Work

MODULE 2: How APIs Work โ€“ Requests, Responses and Endpoints

Welcome to Module 2!

In Module 1, you learned that APIs help different computer applications communicate and exchange information.

But have you ever wondered what actually happens when one application asks another application for information?

How does the application send its question? How does the other application reply? And where does the communication happen?

In this module, you will learn about API requests, API responses and API endpoints.

2.1 What Happens When an Application Uses an API?

Imagine that you are using a school application to check your examination results.

You enter your student identification number and click the button labelled "Check Result".

The application needs to collect your result from the school's computer system.

It sends a request through an API. The school's system receives the request, finds your result and sends the information back.

Your application then displays your examination result.

The API Communication Process

  1. Application: You request your result.
  2. API: Your request is sent to the school system.
  3. Server: The server processes the request.
  4. Response: The server sends your result back.
  5. Application: Your result appears on the screen.
Remember: An API usually works through two important actions: sending a request and receiving a response.

2.2 What Is an API Request?

An API request is a message sent by one application to another application asking it to perform an action or provide information.

Think about sending a text message to your friend. You write a message and send it. Your friend receives it.

An API request works in a similar way, except that the message is sent between computer systems.

Examples of API Requests

  • Asking a banking system for your account balance.
  • Requesting the price of a product from an online store.
  • Asking a school system for a student's examination result.
  • Requesting weather information for Ibadan.

Every request must be sent to the correct API service and must follow the rules expected by that service.

2.3 What Is an API Response?

An API response is the information or result returned after a server receives and processes a request.

For example, imagine requesting the examination result of a student.

The school system may return the following information:

{
    "student_name": "Amina",
    "subject": "Mathematics",
    "score": 85,
    "grade": "A"
}

This information is called a response. The application can read the response and display it to the student.

Easy way to remember: A request is a question or instruction sent to a server. A response is the answer returned by the server.

2.4 Understanding API Endpoints

An API endpoint is a specific web address where an application can send a request to access a particular resource or service.

Think about your home address. If someone wants to visit you, they need to know where you live.

In a similar way, an application needs to know the correct API endpoint before it can request information.

Here is an example of an API endpoint:

https://api.example.com/students

This example represents an endpoint that could provide student information. It is an illustrative address, not a real API service.

Different Endpoints Can Perform Different Tasks

https://api.example.com/students
https://api.example.com/courses
https://api.example.com/results
https://api.example.com/teachers

Each endpoint could be responsible for providing a different type of information.

Endpoint Information
/students Student records
/courses Available courses
/results Examination results
/teachers Teacher information

2.5 Understanding the Server

A server is a computer or computer system that provides information and services to other computers.

Imagine that a school keeps all its students' records in a computer system. When another authorised application requests a student's result, the server can process that request and return the result.

The server plays an important role in API communication because it receives requests, processes them and sends responses.

2.6 A Complete API Example

Let's imagine that you are building an application for students in a Nigerian secondary school.

Your application needs to display the list of courses available on a learning platform.

The application sends a request to this sample endpoint:

https://api.example.com/courses

The server receives the request and returns a response like this:

{
    "courses": [
        {
            "id": 1,
            "name": "Introduction to Cyber Security"
        },
        {
            "id": 2,
            "name": "Data Analytics"
        },
        {
            "id": 3,
            "name": "Web Development"
        }
    ]
}

The application can now display the three courses to students.

This is how an API can help one application collect information from another system.

2.7 Practical Lab: Make Your First API Request

Lab Title: Retrieve Information from a Public API

In this activity, you will use a free testing API to retrieve information about a user.

What you need:

  • A computer or smartphone.
  • An internet connection.
  • A web browser such as Chrome or Edge.

Follow these steps:

  1. Open your web browser.
  2. Visit the address below.
  3. Wait for the information to appear.
  4. Study the information displayed on your screen.
Testing API Endpoint:

https://jsonplaceholder.typicode.com/users/1

Open the API and view the response

Now answer these questions:

  1. What is the name of the user?
  2. What is the user's email address?
  3. What is the name of the user's company?
  4. Which part of the address is the API endpoint?
  5. What do you think the server did after receiving your request?

2.8 Introduction to API Requests Using JavaScript

Programmers can use JavaScript to send API requests from a website. One common tool for doing this is called Fetch.

Look at this simple example:

fetch('https://jsonplaceholder.typicode.com/users/1')
    .then(response => response.json())
    .then(data => {
        console.log(data);
    })
    .catch(error => {
        console.log("Something went wrong:", error);
    });

What Does This Code Do?

  • fetch(): Sends a request to the API.
  • response.json(): Converts the returned JSON response into usable JavaScript data.
  • data: Contains the information returned by the API.
  • console.log(): Displays the information in the browser console.
  • catch(): Handles errors if the request fails.

You do not need to memorise the code yet. The important thing is to understand that JavaScript can send requests and receive responses from APIs.

2.9 What Happens When an API Request Fails?

Sometimes an API request does not work as expected. This can happen for several reasons.

  • The internet connection is unavailable.
  • The API endpoint is incorrect.
  • The server is temporarily unavailable.
  • The application does not have permission to access the information.
  • The request contains incorrect information.

When something goes wrong, the server may return an error response instead of the information requested.

Important: Programmers must learn how to identify and handle API errors so their applications can continue working properly.

2.10 Module Summary

In this module, you learned that:

  • An API request is a message sent to a server.
  • An API response is the information returned by the server.
  • An API endpoint is a specific address used to access an API resource.
  • A server receives and processes API requests.
  • Applications can use API responses to display information.
  • JavaScript Fetch can be used to send API requests.
  • API requests can fail because of connection, permission or server problems.

MODULE 2: KNOWLEDGE ASSESSMENT

Answer the following questions to test your understanding.

Question 1

What is an API request?

  • A. A message sent to an API asking for information or an action
  • B. A computer keyboard
  • C. A type of computer virus
  • D. A printed document

Question 2

What is an API response?

  • A. A computer screen
  • B. Information returned after processing a request
  • C. A website design
  • D. A computer password

Question 3

What is an API endpoint?

  • A. A computer mouse
  • B. A specific address used to access an API resource
  • C. A programming student
  • D. A database table only

Question 4

Which computer system receives and processes API requests?

  • A. Printer
  • B. Monitor
  • C. Server
  • D. Keyboard

Question 5

Which JavaScript function can be used to send an API request?

  • A. print()
  • B. fetch()
  • C. write()
  • D. deleteScreen()

Question 6

Which of these can cause an API request to fail?

  • A. Incorrect endpoint
  • B. Internet connection problems
  • C. Server problems
  • D. All of the above

Question 7

What happens after a server successfully processes a request?

  • A. It sends a response.
  • B. It destroys the application.
  • C. It switches off the internet.
  • D. It deletes all user information.

Congratulations!

You have successfully completed Module 2. You now understand how applications send requests, receive responses and communicate through API endpoints.

Coming Up in Module 3: Understanding HTTP Methods and Status Codes. You will discover how applications use GET, POST, PUT, PATCH and DELETE to communicate with servers.

๐Ÿ† Get Certified

๐Ÿ”’

Earn this certificate

Every lesson is already free to read. Sign up, finish the lessons, pass the exam (โ‚ฆ2,000 or 20,000 coins), and get a verified certificate with your name on it.

๐ŸŽ“ Sign Up to Get Certified
๐Ÿ› ๏ธ Practice Tools
Hands-on simulators & labs - subscription required.
โ†’
๐ŸŽฏ Internship Tasks
Real-world tasks to build your portfolio - unlock them with a subscription.
โ†’