Welcome to Module 7! You have learned how APIs work, how to retrieve information, and how to display API data using JavaScript.
But there is something very important we must understand: security.
Imagine that a school keeps students' examination results on a server. Should everyone on the internet be allowed to view those results? Of course not!
This is why APIs need security.
Course progress: 7 out of 8 modules (87.5%)
API security means protecting an API from people or programs that should not access it, misuse it, or damage the information it handles.
Think about your school.
The school office may keep:
This information should not be available to everybody.
The school needs rules that determine who can access each type of information.
A student should be able to view their own examination result. However, they should not be able to change another student's result or access the school's private financial records.
APIs often connect applications to important information.
Without proper security, attackers may try to:
For example, imagine that a student portal allows anyone to change examination scores without checking their permission.
Someone could change another student's score. This could cause serious problems for the school.
Authentication is the process of checking who someone is.
Think about entering your school. The security officer may ask you to show your school identification card before allowing you into certain areas.
The identification card helps the school recognise you.
Web applications use authentication in a similar way.
A user may need to provide:
When you sign in to CRMNuggets, the platform needs to verify that you are the person who owns the account before giving you access to private account information.
Authorization is the process of deciding what an authenticated user is allowed to do.
Authentication asks:
"Who are you?"
Authorization asks:
"What are you allowed to do?"
Imagine a school with different people:
| Person | Possible Permission |
|---|---|
| Student | View their own results. |
| Teacher | View and enter results for assigned classes. |
| School administrator | Manage school records and user accounts. |
Each person has different permissions.
An API key is a special value used by an API provider to identify an application or control access to an API.
Think of it like an identification code given to an application.
For example, a weather application may need an API key before it can request weather information from a weather service.
A request may look like this:
GET /weather
Header:
X-API-Key: YOUR_API_KEY
The API provider can check the key before processing the request.
API keys may also help providers monitor usage and apply request limits.
An access token is a digital value that an application presents to show that it has permission to access a protected resource.
Imagine that you sign in to an online learning platform.
After your identity is verified, the platform may issue an access token.
Your application can then use the token when requesting information that belongs to your account.
An API request may contain this header:
Authorization: Bearer YOUR_ACCESS_TOKEN
The server checks the token and decides whether to allow the request.
| API Key | Access Token |
|---|---|
| Often identifies an application or API client. | Often represents an authenticated user or authorised access. |
| May be long-lived until changed or revoked. | Often has an expiry time. |
| May be used to control API usage. | May carry information about permissions or access. |
Their exact purpose depends on how the API provider designs its security system.
HTTPS stands for Hypertext Transfer Protocol Secure.
It is the secure version of HTTP.
HTTPS uses encryption to protect information travelling between an application and a server.
Imagine that you send a private letter through the post. You do not want strangers to read the message while it is being delivered.
Encryption helps protect information in a similar way.
| HTTP | HTTPS |
|---|---|
| Does not provide TLS encryption by itself. | Uses TLS to protect communication. |
| Does not provide the same protection against interception. | Helps protect information while it travels across a network. |
| Not suitable for transmitting sensitive information without additional protection. | Should be used for websites and APIs. |
You can recognise an HTTPS address because it begins with:
https://
Encryption is the process of changing readable information into a protected form so that people without the correct key cannot easily understand it.
For example, imagine that you want to protect a private message.
Before encryption, the message may look like:
My examination result is private.
After encryption, the information is transformed into an unreadable form.
A person with the correct decryption key can turn it back into readable information.
Encryption is used to protect:
Passwords are important because they help protect user accounts.
Imagine that a learning platform stores every student's password as ordinary readable text.
If someone gains access to the database, they may be able to read the passwords.
This is why passwords should be stored using secure password hashing.
Hashing transforms a password into a fixed-length value. Proper password hashing algorithms are designed to make it difficult to recover the original password.
Common password hashing algorithms include:
Rate limiting means controlling how many requests an application or user can send to an API within a period of time.
Imagine that a school library has only one librarian. If 5,000 students request books at exactly the same time, the librarian may become overwhelmed.
The library may need a system to manage how many requests it handles at once.
APIs use rate limiting in a similar way.
For example, an API may allow an application to send a maximum of 100 requests per minute.
If the application sends too many requests, the API may respond with:
429 Too Many Requests
Rate limiting helps protect servers from excessive requests and misuse.
Developers must avoid mistakes that can expose information or allow unauthorised access.
| Security Mistake | Why It Is Dangerous |
|---|---|
| Exposing API keys | Unauthorised people may use the credentials. |
| Using HTTP for private data | Information may not be protected in transit. |
| Weak authentication | Attackers may gain access to accounts. |
| Missing permission checks | Users may access information they should not see. |
| Storing readable passwords | A database leak can expose passwords. |
| Allowing unlimited requests | The API may be abused or overloaded. |
| Trusting all incoming data | Unsafe information may enter the application. |
Imagine that you are building a student results application.
The application needs to make sure that a student can only view their own results.
A secure system should:
Let's learn how JavaScript can send an API request with an authorization header.
The following is an illustrative example. It uses a placeholder token and a fictional protected API endpoint. It will not work until you replace them with credentials and an endpoint supplied by an API provider.
async function getPrivateData() {
const token = "YOUR_ACCESS_TOKEN";
try {
const response = await fetch(
"https://api.example.com/profile",
{
method: "GET",
headers: {
"Accept": "application/json",
"Authorization": "Bearer " + token
}
}
);
if (!response.ok) {
throw new Error(
"Access denied or request failed"
);
}
const data = await response.json();
console.log(data);
} catch (error) {
console.log("Error:", error.message);
}
}
getPrivateData();
Read each situation and identify the security problem.
A developer uploads a file containing a private API key to a public GitHub repository.
Question: What is the security problem?
Answer: The private API key has been exposed and may be used by unauthorised people.
A student signs in to a school portal and changes a number in the URL to view another student's examination result. The server returns the result without checking ownership.
Question: What is the security problem?
Answer: The server failed to check whether the student was authorised to access that record.
A website sends private login information through an ordinary HTTP connection.
Question: What should the developer do?
Answer: Use HTTPS to protect information while it travels between the browser and server.
Here are important rules every developer should follow:
In this module, you learned:
Choose the correct answer for each question.
What is API security?
What does authentication do?
What does authorization determine?
What is an API key commonly used for?
Which HTTP header commonly carries an access token?
What does HTTPS help protect?
Which method should be used to protect stored user passwords?
What does rate limiting control?
What should a developer do with a private API key?
Why should an API check user permissions on the server?
Imagine that you are developing a student portal for a secondary school in Ibadan.
Write a short security plan explaining how you would protect the portal.
Your plan should answer these questions:
Write your answers in a document or notebook.
You have completed Module 7: API Security, Authentication and Data Protection.
You now understand some of the important methods developers use to protect APIs and private information.
Building a Complete API-Powered Application โ combine everything you have learned to plan and build a simple application that communicates with an API.
CRMNuggets
Learn. Practice. Get Certified.