Getting Started With API

Hi! Happy to see you here. Why you’re here? Oh! obviously to know something about API. Many of us think that using API is kinda difficult. Really??? No, the moment you understand concepts of it, you will start enjoying it.

API, what’s that? API is just short hand for Application Programming Interface. It’s how an application or service or software product makes some parts of it’s functionality. Suppose you’re building an app to show weather reports, Earthquake information or records of your own user’s information, it could be anything where you need to fetch data from API or you need to update existing information or adding new information.

When you see apps posting to Twitter or Facebook or Instagram or live streaming video, online payments they’re all probably using APIs.

Terminology

API Key :- When an API request through a header to recognize who is requesting, the authorized key passed into the request is said to be an API Key.

GET : The Restful API’s HTTP method of obtaining resources is called GET. In simple terms when we need data from API we use GET method.

POST : It is the Restful API’s HTTP method for building resources. In simple terms POST requests are used to send data to the API server to create or update a resource.

PUT : It is used to update an existing record in the data source. PUT is used to replace the entire resource.The difference between POST and PUT is that PUT requests are idempotent .

DELETE : The delete method is exactly as it sounds. It delete the resources from the database.

JSON : JavaScript Object Notion is said to be a data format used for APIs request parameters and response body.

Working of API

You made a request to API using (GET/ PUT/ POST/ DELETE ). API communicates with server regarding your request , it will connect with database to access required information/data and send to the app as Response.

Let’s take a example to understand. You went to a restaurant, called a waiter and then you ordered “Burger”. Waiter communicates with chef regarding your order, after sometime waiter serves you a burger.

Now, there is two thing you can do, you can eat there or you can request to pack your Burger. Why I’m talking about packing my order is that, i want to eat at home while watching movie. You’re like “ what I’m talking about? what does it means? ”. I have already told you about JSON, it is like packing my order and enjoying at home. Yes, API’s gives you response in JSON, XML or CSV formats and using JSON format is kinda easy.

Understand Structure of JSON

API : — https://randomuser.me/api/?format=json open this URL and see the data.

Simple Structure of JSON

Let’s See the code of web page layout .

Let’s see the code for fetching data from API to our layout design.

I’m skipping all the HTML and CSS part you can see the full code here. Let me explain the code. First store the URL of API also mention format “JSON”. Now, pass the URL to fetch. This method returns a Promise that you can use to retrieve the response of the request.

We got the result now with json.results we are accessing results object.Here results value is array having one element so we can directly access using index. We can iterate through array of results if there are more than one. Now with the help of result reference we can access it’s object or array or any key’s value just like i did in code from Line 16 to 23.

Yeah! We got all the information of random user using API. Here I’m not focusing more on design part. I know you can do much better than this.

Now, I can say that you have good understanding of API and you can use any API according to your need and also you can parse the data from JSON. Here I used JavaScript but you can do all this in your favorite language. In other language only syntax will be different but the core understanding of parsing JSON is same.

I hope it was worthy to read. Have a wonderful time!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rahul Kumar

Rahul Kumar

4 Followers

Final year BE student. Trying to giving back to the dev community.