Welcome to Module 5! In this lesson, you will learn how websites and applications use REST APIs to exchange information. You will also learn how to read API documentation and understand instructions provided by API developers.
Course progress: 5 out of 8 modules (62.5%)
Imagine that your school has a website where students can check their examination results.
Instead of storing every student's result on the student's phone, the school keeps the information on a server. The school website sends a request to the server whenever a student wants to see a result.
The system that helps the website communicate with the server can use a REST API.
REST stands for Representational State Transfer.
A REST API is a way for applications to communicate over a network using standard web rules, commonly HTTP.
You open the CRMNuggets website and click on "My Courses". The application requests your course information from the server. The server sends the information back, and the website displays your courses.
Imagine a school office where students request different information:
A REST API works in a similar way. An application makes a request, the server processes it, and the server returns a response.
In a REST API, a resource is an item or type of information that an application can access.
For example, a school management application may have these resources:
A CRMNuggets learning platform may also have resources such as:
An endpoint is a specific URL that an application uses to access a resource or perform an operation through an API.
For example, imagine a school API has this endpoint:
https://school.example.com/api/students
This endpoint represents the students resource in our imaginary school API.
Another endpoint might be:
https://school.example.com/api/courses
This one represents courses.
The domain and endpoint path work together to identify where the API request should go.
| Endpoint | Purpose |
|---|---|
| /api/students | Access the students resource |
| /api/teachers | Access the teachers resource |
| /api/courses | Access the courses resource |
| /api/results | Access examination results |
These are imaginary school API examples. A real API must provide its own valid endpoints.
In Module 3, you learned about HTTP methods. REST APIs commonly use these methods to tell the server what an application wants to do.
| HTTP Method | Meaning | Example |
|---|---|---|
| GET | Retrieve information | View all courses |
| POST | Create new information | Register a new student |
| PUT | Replace an existing resource | Replace a student's profile |
| PATCH | Update part of a resource | Change a student's phone number |
| DELETE | Remove a resource | Delete a course record |
If you want to see all available courses on CRMNuggets, the application may send a GET request to a courses endpoint.
GET /api/courses
Sometimes, an application does not want all the information in a resource. It wants one particular record.
For example, imagine that a school has 500 students. A student wants to view the record belonging to student number 25.
The API may use an endpoint like this:
https://school.example.com/api/students/25
The number 25 is a path parameter. It identifies the particular student record being requested.
Another example:
/api/courses/12
Here, 12 identifies a particular course.
Query parameters help an application filter, search, sort, or control the information it requests.
Imagine that a school has students in different classes. You want to see only students in JSS2.
The API may accept this URL:
https://school.example.com/api/students?class=JSS2
The question mark (?) introduces the query parameters.
In this example:
You can also have more than one query parameter:
https://school.example.com/api/students?class=JSS2&limit=10
This imaginary request asks for JSS2 students and limits the results to 10 records, if the API supports those parameters.
| Path Parameter | Query Parameter |
|---|---|
| /students/25 | /students?class=JSS2 |
| Identifies a specific resource. | Filters or controls the returned information. |
| Usually appears in the URL path. | Appears after a question mark. |
Headers provide additional information about an API request or response.
Think of headers like information written on the outside of a parcel. The information helps the receiver understand how to handle the parcel.
For example, an API request may include a header telling the server that the application expects JSON data.
Accept: application/json
Another common header is:
Content-Type: application/json
This tells the server that the request body contains JSON data.
| Header | Purpose |
|---|---|
| Accept | Indicates the response format the client can accept. |
| Content-Type | Indicates the format of the request body. |
| Authorization | Carries authentication information. |
Some APIs are public and allow people to access certain information without signing in.
Other APIs contain private information. They require the application to prove that it has permission to access the data.
This process is called authentication.
For example, a student may need to sign in before viewing their examination results.
An API may use an API key or an access token to identify an authorised application or user.
Authorization: Bearer YOUR_ACCESS_TOKEN
The word Bearer is commonly used before an access token in this type of authorization header.
API documentation is a guide that explains how developers should use an API.
Imagine that you buy a new television. The manufacturer provides a manual explaining how to connect and operate it.
API documentation works in a similar way. It explains how to communicate with an API correctly.
Good API documentation usually explains:
Imagine that CRMNuggets provides an API for displaying available courses.
| Documentation Item | Example |
|---|---|
| API Name | CRMNuggets Course API |
| Base URL | https://api.example.com |
| Endpoint | /courses |
| Method | GET |
| Authentication | Not required in this imaginary example |
| Response Format | JSON |
When you combine the base URL and endpoint, you get:
https://api.example.com/courses
The application can send a GET request to retrieve the courses if this API has actually been created and made available.
When you open API documentation, follow these steps:
The base URL is the main address used to access the API.
Look for the specific resource or action you want to access.
Find out whether the endpoint uses GET, POST, PUT, PATCH, or DELETE.
Look for any required path parameters or query parameters.
Find out whether the API expects JSON, an access token, or other request information.
Check what information the server will return.
Look for possible errors such as 400 Bad Request, 401 Unauthorized, and 404 Not Found.
Now let's make a real request to a public sample API.
We will use JSONPlaceholder, a free testing API that provides sample data for learning and practice.
Our task is to retrieve information about a sample user.
API endpoint:
https://jsonplaceholder.typicode.com/users/1
async function getUser() {
try {
const response = await fetch(
"https://jsonplaceholder.typicode.com/users/1"
);
if (!response.ok) {
throw new Error("Could not retrieve user information");
}
const user = await response.json();
console.log("User Name:", user.name);
console.log("Email:", user.email);
console.log("City:", user.address.city);
} catch (error) {
console.log("Error:", error.message);
}
}
getUser();
If the request succeeds, the browser console should display information similar to:
User Name: Leanne Graham
Email: Sincere@april.biz
City: Gwenborough
These are sample records supplied by JSONPlaceholder, not real student information.
Study this example:
https://example.com/api/students/25?class=JSS2
Answer the following questions:
Sometimes, an API request does not work as expected. Here are some common problems:
| Problem | Possible Explanation |
|---|---|
| 404 Not Found | The endpoint or requested resource may not exist. |
| 401 Unauthorized | Valid authentication information may be missing or incorrect. |
| 403 Forbidden | The application does not have permission to access the resource. |
| 400 Bad Request | The request may contain incorrect or missing information. |
| 429 Too Many Requests | The application may have sent too many requests in a short period. |
| 500 Internal Server Error | The server encountered an unexpected problem. |
Remember that a failed API request does not always mean your JavaScript code is wrong. The problem may be on the server, with the network, or with the request details.
In this module, you learned that:
Test what you have learned. Select the best answer for each question.
What does REST stand for?
What is a resource in a REST API?
What is an API endpoint?
Which HTTP method is commonly used to retrieve information?
In the URL /students/25, what does 25 represent?
Which symbol introduces query parameters in a URL?
What is the main purpose of API documentation?
Which header can carry an access token?
Which HTTP status code means Not Found?
Which JavaScript function can send an HTTP request to an API?
Imagine that you are building a school management application for a secondary school in Ibadan.
Create a simple API plan that includes:
Write your answers in a document or notebook. Explain what each endpoint is expected to do.
You have completed Module 5: Working with REST APIs and API Documentation.
You now understand how applications communicate with APIs, how endpoints are structured, and how developers use documentation to connect their applications to data.
Working with API Data Using JavaScript โ learn how to retrieve, display, filter, and use API data in real applications.
CRMNuggets
Learn. Practice. Get Certified.