Application Programming Interfaces(APIs)

Tushar Rajpoot
3 min readJan 27, 2021

What is an API?

An API is a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system.

API main components

  • Endpoints
  • Paths
  • Parameters
  • Authentication

API Endpoints

Let’s look at a simple API

https://api.kanye.rest/

This url is endpoint.
An endpoint is one end of a communication channel. When an API interacts with another system, the touchpoints of this communication are considered endpoints. For APIs, an endpoint can include a URL of a server or service.

The place that APIs send requests and where the resource lives, is called an endpoint.

API Paths

A specific path after the endpoint which can be choosed by you. It’s just like a branch.
A Path is a unit of a REST API that you can call. A Path comprises an HTTP verb and a URL path that, when exposed, is combined with the base path of the API. By configuring the Path, you define how the API is exposed to your developers.

API Parameters

API parameters are options that can be passed with the endpoint to influence the response.
parameters got at the end of the URL, after a question mark and then there’s a key value pair.
When you want to specify a parameter, remember that you need symbols in the URL

like this

https://v2.jokeapi.dev/joke/Programming?contains=hello

API Authentication

When we come to data that’s more monetizable, or allows developers to build more complex applications that might be used by hundreds or thousands of users, then these web sites have to start thinking very carefully about how to either monetize your use of their data or how to limit your use to a threshold. And the way that they would do that is through authentication.
So every time you make a request through the API, they have to be able to identify you as the developer,
and they have to keep track of how often you’re using their server to get data, and then charge you, or limit you, accordingly. In order to illustrate this concept of authentication.

When we make our request to the OpenWeatherMap servers, the data that we get back is in something called a JSON format.

What exactly is a JSON format?

How it looks like:

{
"error": false,
"category": "Programming",
"type": "twopart",
"setup": "Hey, wanna hear a joke?",
"delivery": "Parsing HTML with regex.",
"flags": {
"nsfw": false,
"religious": false,
"political": false,
"racist": false,
"sexist": false,
"explicit": false
},
"id": 10,
"safe": true,
"lang": "en"
}

A JSON format stands for JavaScript Object Notation.

JSON is not the only format that we can receive data from APIs. So in addition to JSON, you’ll often find a format called XML, which is extensible markup language, or it could also come back as simple HTML, hypertext markup language.

Making a get request to external server in node js

There’re a lot of ways. 5 Ways to Make HTTP Requests in Node.js

Request module

HTTP — the Standard Library

There is a default HTTP module in the standard library. With this module, you can just plug and go without having to install external dependencies. The downside is that it isn’t very user friendly compared to other solutions.

First require http module in node js

const https = require("https");

We will use the get method

https.get(url[, options][, callback])

Example code:

https.get('https://encrypted.google.com/', (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
}).on('error', (e) => {
console.error(e);
});

How to parse JSON?

We passed a url to the http get method and added a callback function for getting some responce.

res.on('data', (data) => {
const wdata = JSON.parse(data);
});

The JSON.stringify() method

Converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

Postman

when we’re testing APIs, we’ll use a tool called Postman.

--

--