How to test an API?

Table of Contents

In our day-to-day life, there are so many digital tasks that we perform. Let it be signing up for the account through your Gmail or getting the digital payment confirmation on your form submission portal. API is used widely over the internet by applications and websites and all sorts. When you think of adopting certain functionality for your app or website, you might need to integrate one such API for it. Thus, to give you an idea of how one can test it, we bring you how to test an API.

In today’s post, we are going to check what is API, REST API methods, how to test an API, and API development services. Without further due, let’s begin it.

What is an API anyway?

API stands for Application Programming Interface, it is a concept that lets one device sends information to another device to build a meaningful conclusion to the transmission and vice versa. The best thing about API is that it is cross-platform too. Meaning, the developer can build API for once and use it on multiple devices regardless of its OS.

An example of it is, thinking of you visiting a website that asks you to sign up to get to a certain page of that website, and when you reach the sign-up page, it lets you sign up using your Facebook, Google, or Microsoft account, etc. This is basically API using your profile details to input into the database and get you into the webpage. How easy are you to sign up and sign in? well, thanks to the public API.

The API is divided into 2 main categories, one is SOAP API and another one is REST API. SOAP API is sort of an old tech now, where the data is used to pull as XML. However, tech companies and new ventures are preferring REST API as it is widely used. REST API gets the data in the form of JSON format. We don’t necessarily need to go in-depth about it because there are various APIs depending upon the requirement such as private, public, and partner APIs.

Before we get into the next part here is a brief about the types of API. Private API would be something the company uses within their organization, public API would be open to using, and partner API would be between 2 business partners such as Apple and Microsoft and so an so forth.

REST API methods

We would be testing REST API in this blog, and thus just to clear things up we need to know its methods. REST API has various methods, which are used per scenario but 4 common ones are as above.

How to test API

Let’s get into this, we have created our own API and we would test it using various methods. You can also create API locally using NodeJS as it has the best JSON server module to create the API and test it along. However, before testing locally created APIs, we would also take some public APIs which are free to use and would test them through a PHP script.

1) Test API using PHP script

In this section, we are going to check the simple PHP script for testing the Endpoint. This public API would require an API key which you can get from within their portal. In this section, we are going to use weather API which you can find here.

Step 1)

Login to your cPanel account using your hosting ip:2083. The ideal URL would be http://hosting-IP:2083 or http://your-domain.com:2083 and navigate to the domain section. When you are in the domain section, click on the document root of the domain you wish to test.

Step 2)

When you are in the right document root, create a new PHP file and name it anything. I am naming it api-test.php and editing it. Now paste the following code on the created api-test.php file.

<?php

// Replace 'YOUR_API_ENDPOINT' with the actual API endpoint you want to test
$apiEndpoint = 'YOUR_API_ENDPOINT';

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $apiEndpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl);

// Check if cURL request was successful
if ($response === false) {
die('Error: Unable to connect to the API.');
}

$httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if ($httpStatus !== 200) {
die('Error: API returned an invalid response with HTTP status code ' . $httpStatus);
}

curl_close($curl);

// If the script reaches this point, the API is working
echo 'API is working correctly!';
?>

Step 3)

Make sure to replace YOUR_API_ENDPOINT with your API URL. After doing this, call the domain name with the file which you created. Usually, this would be your-domain.com/test-api.php, post calling this file, check what output it gives you. If the API is working fine then you would get a response as “API is working correctly!”.

2) Test API using Postman

Postman is a famous API tester tool that helps you do manual API testing. This tool has almost everything which is required for API. Postman tool is avaialble in Windows, MacOS and Linux. You would need to download the application from the link here or you can use a web version of it from the link here. If you are using a locally created API to test like we are going to do, then you would need to ensure you have installed the Postman application on your machine from the link here.

Step 2.1)

Once you are signed in, you would be presented with a welcome screen. The point to note here is that you must create a workspace in order to get started. Tap on the Workspace on the top left side of the page and click and create the workspace name. You can leave it as it is if you wish to.

Step 2.2)

Next is to create a Collection name so you have it saved for the next time you check back to review how was it. Click on Create a Collection and name it per your choice. Next to it you would see a new request under the Collection you created, create a new request and name it.

Step 2.3)

After following the steps till previous, we can add our locally created endpoint in the field bar. When you have pasted the endpoint, you click on send button to get results. On the bottom side, you would see HTTP status codes. 200 OK means it is successfully retrieved the data from that endpoint. Now following are the test cases per REST API methods.

A) Get employee data by ID

Let’s suppose you wish to get the data by the ID of a particular entry of employee in this API, then we can use the API by the API ID, URL/api_id. We wanted the data for ID 3 so we checked our endpoint as shared in the above image.

B) Post employee data, create new data

Request payload
{

"firstName": "Pranav",
"lastName": "Chaudhari",
"position": "Hosting Tech Lead",
"email": "waytopranav22@gmail.com",
"date-joined": "12-3-2019",
"project": "spoiler-alert"

}

Moving on, we can add the employee data to the existing API that we are testing. We can do so using the POST method. After pasting the API endpoint in the field, navigate to >> Body tab, and from the selector select the >> Json option and add the content in JSON that you wish to add in the API. We have used the entry data.  Note that you need not input any ID as it would create a default ID. Hit the send button, and to verify if it is updated, again do a get request as per the first step.

C) Update the API with put a request on a particular ID

The next is to update the existing entry of API, this can be achieved using the PUT method. Select the PUT method and add the id at the end to make changes to a particular ID entry. We wanted to update the 2nd entry so used the endpoint as http://localhost:3000/employee/2 and then go to Body >> select json same as in the previous step C and hit the send button. To verify, do a get request as in step 2.3.

D) Remove the API entry by ID

The last we have is to remove an entry, you guessed right this is done using the DELETE method of REST API. You need to input the entry point by ID which you wish to delete and hit the Send button. We want to get rid of  1st entry which has ID 1. So we would use it as http://localhost:3000/employee/1 and hit the send button, and then verify again and do the GET request as in step 2.3 above.

Conclusion

API is proved to be an easygoing and helpful technology to which users are exposed without them knowing it. IT industry is growing like never before and thus we generally should be aware of IT concepts that are widely used within. This is in fact Techdecipher’s mission and we would come up with something awesome next week. This concludes how to test an API. Do you use API for your projects and do you have any other methods of testing it? do let us know in the comments section below. If you need any help or have any suggestions to make, then do reach us via the contact page here. Happy International Youth Day!

Pranav Chaudhari
Pranav Chaudhari
I am a tech enthusiast who began a career in the hosting field. I like to help folks with their tech-related concerns and bring the best of it.

Leave a Reply

Your email address will not be published. Required fields are marked *

Send Us A Message

More Posts

This website used cookies to ensure you get the best experience in our website.