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

Module Five

๐Ÿ“– 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.
6
Module 5 - Working with REST APIs and API Documentation
MODULE 5 OF 8

Working with REST APIs and API Documentation

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%)

What you will learn:
  • What REST APIs are.
  • How REST APIs organise information into resources.
  • How API endpoints work.
  • The difference between path parameters and query parameters.
  • What API headers are used for.
  • How to read API documentation.
  • How to make a request to a REST API using JavaScript.

1. What Is a REST API?

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.

Simple example:

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.

Think of REST API Like a School Office

Imagine a school office where students request different information:

  • A student requests their examination result.
  • The office checks the school's records.
  • The office returns the correct result.

A REST API works in a similar way. An application makes a request, the server processes it, and the server returns a response.

2. What Is a Resource?

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:

  • Students
  • Teachers
  • Courses
  • Examinations
  • Results

A CRMNuggets learning platform may also have resources such as:

  • Users
  • Courses
  • Lessons
  • Certificates
  • Examination records
Remember: A resource represents the information that an API allows an application to access or manage.

3. Understanding API Endpoints

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.

Example of Different Endpoints

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.

4. How HTTP Methods Work with REST APIs

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
Example:

If you want to see all available courses on CRMNuggets, the application may send a GET request to a courses endpoint.

GET /api/courses

5. What Are Path Parameters?

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.

Remember: A path parameter is included directly in the URL path to identify a particular resource.

6. What Are Query Parameters?

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:

  • class is the parameter name.
  • JSS2 is the parameter value.

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 Parameters vs Query 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.

7. What Are API Headers?

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.

Common API Headers

Header Purpose
Accept Indicates the response format the client can accept.
Content-Type Indicates the format of the request body.
Authorization Carries authentication information.

8. Understanding API Authentication

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.

Important security lesson: Never publish private API keys or access tokens in public websites, GitHub repositories, screenshots, or social media posts. Keep secret credentials on a secure server and protect them properly.

9. What Is API Documentation?

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:

  • The API base URL.
  • Available endpoints.
  • Supported HTTP methods.
  • Required parameters.
  • Request headers.
  • Authentication requirements.
  • Example requests.
  • Example responses.
  • Possible error messages.

Example of Simple API Documentation

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.

10. How to Read API Documentation

When you open API documentation, follow these steps:

Step 1: Find the Base URL

The base URL is the main address used to access the API.

Step 2: Find the Endpoint

Look for the specific resource or action you want to access.

Step 3: Check the HTTP Method

Find out whether the endpoint uses GET, POST, PUT, PATCH, or DELETE.

Step 4: Check the Parameters

Look for any required path parameters or query parameters.

Step 5: Check the Headers

Find out whether the API expects JSON, an access token, or other request information.

Step 6: Study the Example Response

Check what information the server will return.

Step 7: Understand the Errors

Look for possible errors such as 400 Bad Request, 401 Unauthorized, and 404 Not Found.

Developer tip: Do not guess how an API works. Read its documentation first. A correct endpoint with the wrong method or missing authentication may still fail.

11. Practical Lab: Connect to a REST API

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

JavaScript Example

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();

Understanding the Code

  • async function: Creates a function that can handle asynchronous operations.
  • fetch(): Sends a request to the API.
  • response: Stores the server's response.
  • response.ok: Checks whether the HTTP response indicates success.
  • response.json(): Converts the response body into a JavaScript object.
  • console.log(): Displays information in the browser console.
  • catch: Handles errors that occur during the request or processing.

What Should You See?

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.

12. Practical Lab: Understanding an API URL

Study this example:

https://example.com/api/students/25?class=JSS2

Answer the following questions:

  1. What is the domain name?
  2. What is the resource?
  3. What is the path parameter?
  4. What is the query parameter?
  5. What is the value of the query parameter?
Answers for self-check:
  • Domain: example.com
  • Resource: students
  • Path parameter: 25
  • Query parameter name: class
  • Query parameter value: JSS2

13. Common REST API Problems

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.

14. Module Summary

In this module, you learned that:

  • REST APIs help applications communicate with servers.
  • Resources represent information such as students, courses, and results.
  • Endpoints are URLs used to access API resources or operations.
  • HTTP methods describe the action an application wants to perform.
  • Path parameters identify specific resources.
  • Query parameters help filter or control returned information.
  • Headers provide additional information about requests and responses.
  • Authentication helps protect private API resources.
  • API documentation explains how to use an API correctly.
  • JavaScript Fetch can be used to communicate with REST APIs.

15. Knowledge Assessment

Test what you have learned. Select the best answer for each question.

Question 1:

What does REST stand for?




Question 2:

What is a resource in a REST API?




Question 3:

What is an API endpoint?




Question 4:

Which HTTP method is commonly used to retrieve information?




Question 5:

In the URL /students/25, what does 25 represent?




Question 6:

Which symbol introduces query parameters in a URL?




Question 7:

What is the main purpose of API documentation?




Question 8:

Which header can carry an access token?




Question 9:

Which HTTP status code means Not Found?




Question 10:

Which JavaScript function can send an HTTP request to an API?




16. Practical Assignment

Your task:

Imagine that you are building a school management application for a secondary school in Ibadan.

Create a simple API plan that includes:

  1. An endpoint for retrieving student records.
  2. An endpoint for retrieving course information.
  3. An endpoint for retrieving one student's examination result.
  4. The HTTP method you would use for each endpoint.
  5. One example of a path parameter.
  6. One example of a query parameter.

Write your answers in a document or notebook. Explain what each endpoint is expected to do.

Congratulations!

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.

Coming Next: Module 6

Working with API Data Using JavaScript โ€” learn how to retrieve, display, filter, and use API data in real applications.

CRMNuggets

Learn. Practice. Get Certified.

๐Ÿ† 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.
โ†’