What is an API?
API is short for “Application Programming Interface”
Imagine you make a really nice and useful application (think Soundcloud, Gmail, Youtube, Github) and you want other developers and people who are generally interested to be able to interact with your app either by getting information from it, sending information to it or both. The thing to do is to create a set of rules that tells them how to interact with the information in your application. This set of rules will show the other interested parties how to use your software. This is what an API is. Because the term ‘API’ can mean many things and even the definitions in this post are only streamlined to a certain context.
But basically, an API is set of rules and protocols that allows software interact with other software. There are so many APIs interacting on our phones and computers that we’re unaware of but they’re happening behind the scenes all the time. Think of APIs as contracts between separate sets of code. Think of them as the rules that help them interact with each other. The best way to know how to use an API for a certain application is to read the documentation for the API, as rules differ from API to API. Although, there are the common things that we should know if we’re going to be working with APIs. Like “HTTP”.
So what is HTTP?
HTTP is short for “Hypertext Transfer Protocol”.
This sounds really nice and all but what does it mean, still?
Firstly “hypertext” is text that has a link embedded in it. And ‘Transfer Protocol’ is the procedures or systems that can allow us transfer a thing. If you look in your web browsers, you’d see “http” before the web address of every page that you’re on. ‘Http’ is like the web language that helps us find things on the internet. So when you make an http request, you’re asking the internet to please help you find something from its servers and when it finds it (or not), it then sends the information back to you as a response.
The four verbs of HTTP.
So imagine that the things on the servers, all those bits of information on the internet, imagine that they’re nouns. Like Google, or Twitter or Youtube. And that we can access them through the medium “http” using the HTTP verbs (action words) — GET, POST, PUT and DELETE.
GET
The GET method is used to retrieve or get the information from the given server using a given URL. In REST CRUD, it performs the read operation.
‘GET’ is the most commonly used HTTP verb out of all verbs. Here’s an example:
fetch(URL)
.then(resp => resp.json())
.then(data => console.log(data))
POST
The POST method is used for sending data to the server such as uploading a file or transferring some data. In a simple sentence, we can say that the POST method is used for inserting new items in the backend server. In REST CRUD operation it performs the create operation.
fetch(URL, {
method: “POST”,
headers: {“Content-Type”, “Application/json”},
body: JSON.stringify({
/* JavaScript Object */
})
})
.then(resp => resp.json())
.then(data => console.log(data))
PUT
The PUT method is most often used to update an existing resource. The resource is first identified by the URL and if it exists, then it is updated, otherwise, a new resource is created. In simple terms, we can say that if the resource exists then update, else create a new resource.
fetch(URL/id, {
method: “PUT”,
headers: {“Content-Type”, “Application/json”},
body: JSON.stringify({
/* JavaScript Object */
})
})
.then(resp => resp.json())
.then(data => console.log(data))
DELETE
This one is pretty straight foward. The DELETE method is used to delete a resource specified by its URL.
fetch(URL, {
method: “DELETE”
})
.then(resp => resp.json())
.then(data => console.log(data))
Restful API
A Restful API is an API that follows the REST principles. “REST” is short for “Representational State Transfer”. By simply clicking a link in a webpage and being taken to another link (like being transferred from the home page to the about page) you’re following the REST principles. Any API that follows the Rest principle is called a Restful API. When the client sends a request to the server and receives a response, the response is simply some data in JSON or XML format.