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.
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.
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.
Every request must be sent to the correct API service and must follow the rules expected by that service.
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.
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:
This example represents an endpoint that could provide student information. It is an illustrative address, not a real API service.
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 |
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.
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:
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.
In this activity, you will use a free testing API to retrieve information about a user.
What you need:
Follow these steps:
https://jsonplaceholder.typicode.com/users/1
Open the API and view the responseNow answer these questions:
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);
});
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.
Sometimes an API request does not work as expected. This can happen for several reasons.
When something goes wrong, the server may return an error response instead of the information requested.
In this module, you learned that:
Answer the following questions to test your understanding.
What is an API request?
What is an API response?
What is an API endpoint?
Which computer system receives and processes API requests?
Which JavaScript function can be used to send an API request?
Which of these can cause an API request to fail?
What happens after a server successfully processes a request?
You have successfully completed Module 2. You now understand how applications send requests, receive responses and communicate through API endpoints.