What is an API
Application Programming Interfaces (APIs) are constructs made available in programming languages to allow developers to create complex functionality more easily. They allow you to provide simple syntax instead of long and complex code to retrieve data.
When you use any application or online service, the application connects to internet and sends data to the server. The server performs the necessary actions and sends back the data to your phone to see it in readable way. all of this happens via API.
Imagine you are sitting in a restaurant. The waiter take your order (data) to the kitchen (server) and bring back the food (response). The API in this case is the waiter.
Example of API
There is so many restaurants that make online service for their costumers. You can use their own websites to make your orders. A second option is to use service that provide all the menus of the restaurants and allow you to order from any one of them. In the first case you have a direct access to the data by using the restaurant’s own website. However, the service that allow you to view all the restaurants interacts with the restaurant’s API. The API is the interface that, like your helpful waiter, can be asked by that online food service to get information from the restaurant’s database to order your food. The API then takes the restaurant’s response to your request and delivers it right back to the online food service, which then shows you the most updated, relevant information.
Calling APIs in Node.js
You can use APIs in almost every language but i will use JavaScript for our example. Apps use HTTP requests, for example, GET and POST, to communicate with APIs. I will use Axios to perform this commands easier. Copying the following commands in your cmd is enough to install axios.
Get Muhannad’s stories in your inbox
Join Medium for free to get updates from this writer.
Using npm
npm install axiosUsing yarn
yarn install axiosimport axios to your project
import axios from 'axios';performing GET requests
GET is one of the most common HTTP methods to request data from a specified resource.
// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
//Handle errors
.catch(function (error) {
console.log(error);
})Performing a POST requests
POST is used to send data to a server to create/update a resource.
//Send user name
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
//Handle errors
.catch(function (error) {
console.log(error);
});Conclusion
Now you know what is an API and how to use it in Node.js. Using APIs will allow you to make very amazing apps and will save you a lot of effort.





