<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Chintan Palan</title>
    <description>The latest articles on DEV Community by Chintan Palan (@chintanpalan_53).</description>
    <link>https://dev.to/chintanpalan_53</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F265857%2F0b89909d-0f22-4276-bbcb-a4561b52f702.jpeg</url>
      <title>DEV Community: Chintan Palan</title>
      <link>https://dev.to/chintanpalan_53</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chintanpalan_53"/>
    <language>en</language>
    <item>
      <title>What is an API and how to call an API in JavaScript</title>
      <dc:creator>Chintan Palan</dc:creator>
      <pubDate>Sun, 10 May 2020 11:12:51 +0000</pubDate>
      <link>https://dev.to/chintanpalan_53/what-is-an-api-and-how-to-call-an-api-in-javascript-ii</link>
      <guid>https://dev.to/chintanpalan_53/what-is-an-api-and-how-to-call-an-api-in-javascript-ii</guid>
      <description>&lt;p&gt;Have you ever wondered what the heck is this term "API" that everyone keeps talking about? Or did your boss just throw another one of those "We might want to integrate XYZ Api into this project" and you are like, &lt;em&gt;What exactly is an API? Can someone please explain it to me in English?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Don’t worry, I can try, and I bet you already know what it is in some vague way, but let's get it clear.&lt;/p&gt;

&lt;p&gt;Let us first look at the word &lt;em&gt;API&lt;/em&gt; itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1zDZ2WJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ciw09c34yhf9frig6k1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1zDZ2WJJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8ciw09c34yhf9frig6k1.jpg" alt="API"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;APIs stands for &lt;strong&gt;A&lt;/strong&gt;pplication &lt;strong&gt;P&lt;/strong&gt;rogramming &lt;strong&gt;I&lt;/strong&gt;nterface&lt;br&gt;
Lets first break up the word and understand all components individually&lt;/p&gt;

&lt;p&gt;Application - An application that we are wanting to communicate with&lt;/p&gt;

&lt;p&gt;Programming - Controlling or commanding&lt;/p&gt;

&lt;p&gt;Interface - A way to communicate - (like switchboard at homes)&lt;/p&gt;
&lt;h3&gt;
  
  
  Interface
&lt;/h3&gt;

&lt;p&gt;Let's look a bit at &lt;strong&gt;interface&lt;/strong&gt;, that is the main keyword over here. So interface is basically a predefined way to communicate or a medium to communicate between two parties. &lt;/p&gt;

&lt;p&gt;Let us take a simple example of a lightbulb. It is an object that needs to do something, it needs to illuminate the room, but it cannot do that whenever or however it wants to do. We need to tell it when we want it to be on or when we want it to be off. For that, we have an interface called &lt;em&gt;switch&lt;/em&gt; which can help us tell what we want to the bulb in a way that both we and bulb understand. &lt;/p&gt;

&lt;p&gt;So the switch is an &lt;strong&gt;interface&lt;/strong&gt; between us and bulb.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KIeoiQyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k2irjjvid855zatplh80.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KIeoiQyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/k2irjjvid855zatplh80.jpg" alt="Switch is an interface between us and bulb."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Similarly, APIs are just interfaces from which we can control some applications. Usually, in software development, this is called programming (verb - to configure / control), so the interface is called an Application Programming Interface.&lt;/p&gt;

&lt;p&gt;So, in simple terms, &lt;em&gt;&lt;em&gt;APIs are a way to communicate with an application&lt;/em&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In fact, in programming, we are continuously using APIs without even realizing that we are. Remember the first JavaScript "console" statement that we wrote, it was an API from the browser!&lt;/p&gt;

&lt;p&gt;A web browser is an application that has a console. And we might feel that we are writing something to the console using console.log, but the truth is that we are never actually given direct access to the console. It is the browser's property and we are given an input in which we can write commands to say browser to print something to console. Basically, we use the console API to tell the browser what to write.&lt;/p&gt;

&lt;p&gt;So hurray! 😃, you have already used an API, that should make you feel much more confident about yourself.&lt;/p&gt;

&lt;p&gt;Now let's come back to the point why you are reading this article in the first place - To use an API in javascript.&lt;/p&gt;

&lt;p&gt;Currently, COVID-19 is very much a hot topic so let's find some API for that and try to show its data on our page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats"&gt;Here is one such API (General Stats)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the browser, we have a function called fetch with which we can call external APIs via a network.&lt;/p&gt;

&lt;p&gt;So the first step would be to fetch the data from API, so that is what we will tell the browser to do using the fetch function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The fetch function returns a &lt;a href="https://web.dev/promises/"&gt;promise&lt;/a&gt; which can resolve to response details or be rejected if some error occurs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://corona-virus-stats.herokuapp.com/api/v1/cases/general-stats&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="c1"&gt;//Response resolves to a readable stream, &lt;/span&gt;
            &lt;span class="c1"&gt;//so this statement helps us convert it into &lt;/span&gt;
            &lt;span class="c1"&gt;//a static object&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; 
            &lt;span class="c1"&gt;//Now that we have the data, let us see what it looks like in console&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseData&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The output of the above code in a console would look like below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;data&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;total_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4,100,726&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;recovery_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1,441,474&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;death_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;280,431&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;last_update&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;May, 10 2020, 01:29, UTC&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;currently_infected&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2,378,821&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cases_with_outcome&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1,721,905&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mild_condition_active_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2,331,138&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;critical_condition_active_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;47,683&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;recovered_closed_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1,441,474&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;death_closed_cases&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;280,431&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;closed_cases_recovered_percentage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;84.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;closed_cases_death_percentage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;16.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active_cases_mild_percentage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;98.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;active_cases_critical_percentage&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2.0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;general_death_rate&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;7.000000000000001&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;status&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;success&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The data that we receive is in JSON format so javascript makes an object from it (for this case and wherever the API returns a JSON response, but it might change depending on the API we use).&lt;br&gt;
So we can use this response data as we use any other object, and this is a way to take output total cases on the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;total_cases&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great, now we have learned how to get data and display it on the console from an API. So next time you hear the word API, just remember that you already know how to work with it, it is just a friendly URL with which your application can talk to either retrieve some data or send data or commands.&lt;/p&gt;

&lt;p&gt;I have not covered any extra things like showing the data on the web page, that should be fairly easy if you know even the basics of javascript.&lt;/p&gt;

&lt;p&gt;Now go explore some other APIs and add that to your resume :P&lt;/p&gt;

&lt;p&gt;The fetch API is available from browsers natively, but there are also other ways to call an API like using third party libraries/helpers like &lt;a href="https://github.com/axios/axios"&gt;axios&lt;/a&gt;, &lt;a href="https://api.jquery.com/jquery.ajax/"&gt;jQuery Ajax&lt;/a&gt;, etc. Since you know the concepts now, they should not be hard to implement, just take a look at their documentation. You can also &lt;a href="https://blog.bitsrc.io/comparing-http-request-libraries-for-2019-7bedb1089c83"&gt;read a detailed comparison of libraries&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Apart from this, we still need to send data over to APIs and also need to &lt;em&gt;authorize ourselves&lt;/em&gt; as a client to a lot of APIs. &lt;br&gt;
We would cover those in upcoming articles, for now, play around with some open APIs available to get data like &lt;a href="https://jsonplaceholder.typicode.com/"&gt;JSON Typicode&lt;/a&gt;&lt;br&gt;
You can find &lt;a href="https://github.com/public-apis/public-apis"&gt;a good list of open APIs here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;PS. This was my first article and I wrote it to explain a friend about APIs and I am looking forward to creating more content like this.&lt;br&gt;
Do let me know if you found this article helpful and what else would you like to read about.&lt;/p&gt;

&lt;p&gt;Credits:&lt;br&gt;
Photo of switchboard by &lt;a href="https://unsplash.com/@twinsfisch"&gt;twinsfisch&lt;/a&gt; on &lt;a href="https://unsplash.com/"&gt;Unsplash&lt;/a&gt;&lt;br&gt;
Images edited using &lt;a href="https://canva.com/"&gt;Canva&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading till the end!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>layman</category>
    </item>
  </channel>
</rss>
