What is REST API and how it works

What is an API?

API means, Application Programming Interface. Developers use these APIs when building software applications. (Simply what it does is, send requests to the server and bring back the response to the client. It works as an intermediate carrier. ) When you are using Facebook, Instagram, Checking weather report, sending a text message, play a song on mobile, you are using an API in all these occasions.

The most popular example to explain the behavior of API is, Just think you are going to a restaurant. You order a burger and the waiter serves the burger. Here you are the client, kitchen is the server and the waiter acts as the API. Simple as that.

There are different types of APIs such as Private APIs, Public APIs, Data APIs etc. We can use the API based on our requirements.

Eg: Facebook is a Public API. We don’t need any special authorization to get the public data. But everyone can’t access the APIs in your company projects. It needs some special authorization.

If you already know about APIs, I’m pretty sure that you heard about REST and SOAP? Isn’t it?

Nowadays most of the developers prefer to use REST APIs instead of SOAP since the REST made in response to SOAP shortcomings.

It can work with different languages such as XML, HTML and JSON. It doesn’t need more bandwidth when sending requests. Also it’s more efficient and effective.

REST is an architectural style which includes following constraints.

Client server, Cacheable, Uniform interface, Layered system and code on demand(optional).

REST API is only geared towards web applications. And mostly it deals with HTTP requests and responses.

Hope now you have some basic understanding about REST API.

Today I’m going to do some demonstration how the API works. Here I”ll show how to send a simple text message using an API.

So to accomplish this, I’m using a platform called Twilio. What is Twilio? Twilio (https://www.twilio.com/) is an American cloud communication platform. It allows programmers to make phone calls, send text messages and other communication stuff using its web service APIs. It has some nice documentation too.

First create an account (You can sign in to the trial account) and then click “send or receive SMS”.

It generates a trial phone number, account ID(SID) and Auth Token for you. You can see these values on the dashboard.

Dashboard (Image 1)
Image 1

Then go to programmable messaging and let’s try to send a text using this service. (https://www.twilio.com/console/sms/getting-started)

Image 2

You can add your number in the screen and enter some text in the body. You can see how the request body changes in console.

So when you click “Make request”, after a while your phone should receive the SMS text.

Now I’m going to do this scenario using Postman with this web service API. You need an account SID, Auth Token and the trial phone number generated here to do this testing in Postman.

First you have to download and install Postman on your machine. We use Postman to send external API requests and to retrieve the response data from web servers. As you know there are many request types such as GET, POST, DELETE, PUT etc in Postman. Based on your requirement, you can use those types.

In Postman,

Go to new and create a collection(Twilio). Then create a folder name “SMS”.

Then create a GET request and name it as “Message log”.

Go to the Image 2 page again and copy the url. Remember, you should use your account ID(which was created in Twilio account) after “accounts/” in this url and remove the current one.

Eg: https://api.twilio.com/2010-04-01/Accounts/{{ Your ID }}/Messages.json

Then go to the “Twilio” edit section and change the variables/their values as follows.

Image 3

After that, add username and password here and complete the authorization. We use variables here since it’s sensitive data.

Image 4

Now all good. But I made some changes in the URL as follows. Since I assigned the account ID to a variable, I can use it in the url instead of ID directly. Check the below screenshot.

Image 5

Now send the request. You can view the response. What happened was, we (client) sent the GET request and the server sent those requested data as a response. This process was done via API.

Next create a “POST” request and name it “create a text message”. Copy and paste the url here as well. Then add some keys in the body as follows. Because I need to send the text message to my number. So I should add those values.

Image 6

Then send the request. You can view the response below and check your phone. Hope you got the text message. My one is here.

Image 7

Here , what we have done is, added some data and sent a POST request. I added my number and a text message in the body including a media url. So the server sends back the response and delivers the text message to my number. Because we use the Twilio cloud platform. We sent the text message using their web service API.

You can send a GET request again and retrieve all the details regarding this above POST request (Because we use same URLS).

If you see the status codes in above scenarios, those are different. When you send a POST a request, it’s “201 CREATED”. When you send a GET request, it’s “200 OK”. I hope you can understand why those 2 types of requests get different status codes. Because we create new data or update the current one in POST requests. In GET requests, we just send requests and retrieve the data only.

One more important thing. Do the Authorization correctly. Otherwise you will always get the error codes when doing testing.

I hope you were able to learn something from my article regarding APIs. Always welcome your comments. Will meet with another article soon.

Thank you!




Quality Engineer | Blogger | Tech Enthusiast