<?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: Ibrahim Abdullahi Aliyu</title>
    <description>The latest articles on DEV Community by Ibrahim Abdullahi Aliyu (@aybee5).</description>
    <link>https://dev.to/aybee5</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%2F247056%2F15617054-c832-4fbc-907a-4ececc086735.jpeg</url>
      <title>DEV Community: Ibrahim Abdullahi Aliyu</title>
      <link>https://dev.to/aybee5</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aybee5"/>
    <language>en</language>
    <item>
      <title>What is an API for a beginner with an example. </title>
      <dc:creator>Ibrahim Abdullahi Aliyu</dc:creator>
      <pubDate>Wed, 15 Jul 2020 17:19:13 +0000</pubDate>
      <link>https://dev.to/aybee5/what-is-an-api-for-a-beginner-with-an-example-2e6i</link>
      <guid>https://dev.to/aybee5/what-is-an-api-for-a-beginner-with-an-example-2e6i</guid>
      <description>&lt;p&gt;Some years ago while I was trying to learn "What an API is", I come across many tutorials talking about interfaces and coffee shops which confuses me even more.&lt;br&gt;
So today, I will try to explain to you what I understand by API, and later we'll see an example of how to work with an API.&lt;br&gt;
So API (which stands for Application Programming Interface) can simply be referred to someone's code that you want to use in your application/website, without worrying how the code is written(that what the 'i' in the API means). Say we wanted to design a software that displays weather information based on the location a user searches(this is what we will be building later), but instead of writing our own code that will fetch the information from NASA's NOAA weather satellite or some stations, we will use an API from someone that already done that.&lt;br&gt;
The API which usually comes in the form of URL can be accessed by sending a request to the API and in response, the API will give us back information that we can use.&lt;/p&gt;
&lt;h3&gt;
  
  
  Some API Keywords
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;API URL&lt;/strong&gt;: 
This is the base URL we are sending the request to can be both get or post request.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Key&lt;/strong&gt;:
Some API contains some confidential information as such there is a need for authentication before accessing it, while some are paid services. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Endpoint&lt;/strong&gt;: 
Some API URL has many different resources to access, the endpoints differentiate what the API responds with.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;API Queries&lt;/strong&gt;:
These are the piece of data that you attached during your API request.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  API Example:
&lt;/h2&gt;

&lt;p&gt;As mentioned above, we are going to create a web application that will make an API request for weather information. The API will be gotten from &lt;a href="https://www.openweathermap.org" rel="noopener noreferrer"&gt;Open Weather Map&lt;/a&gt;, after making a request to the API, we will receive a response in JSON form and then we can use data return and display it the application.&lt;br&gt;
To save time, I'm not going to explain all the markup and the styling, but if you want the explanation, you can watch this youtube, the only difference is that the video was done with &lt;a href="https://www.vuejs.org" rel="noopener noreferrer"&gt;Vuejs&lt;/a&gt;.&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/JLc-hWsPTUY"&gt;
&lt;/iframe&gt;
&lt;br&gt;
We will be working with 3 files &lt;code&gt;index.html&lt;/code&gt;, &lt;code&gt;script.js&lt;/code&gt;, and &lt;code&gt;style.css&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Markup:
&lt;/h3&gt;

&lt;p&gt;our &lt;code&gt;index.html&lt;/code&gt; will contain the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;h3&gt;
  
  
  Style:
&lt;/h3&gt;

&lt;p&gt;Our &lt;code&gt;style.css&lt;/code&gt; will contain the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;h3&gt;
  
  
  Script:
&lt;/h3&gt;

&lt;p&gt;Finally, our &lt;code&gt;script.js&lt;/code&gt; will contain the following code:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Here we declare some variables and get the element of the ids that we will later fill in with the response we will get back, and a function that checks for &lt;code&gt;Enter&lt;/code&gt; key event to make the API call.&lt;br&gt;
So the next thing to do is going to &lt;a href="https://www.openweathermap.org" rel="noopener noreferrer"&gt;Open Weather Map&lt;/a&gt; and signup to be able to generate an API key and use the API.&lt;br&gt;
After getting the API key, we will then replace the empty api_key variable with the one we generate, my &lt;code&gt;API Key&lt;/code&gt; is '3a2905bb552cfde3a564bd0548d594a9'. The &lt;code&gt;API URL&lt;/code&gt; is &lt;code&gt;https://api.openweathermap.org/data/2.5/&lt;/code&gt;, so replace the empty &lt;code&gt;api_url&lt;/code&gt; with that.&lt;br&gt;
Tada 🎉 we are done with creating our UI it's now time to make our API request.&lt;br&gt;
For the API request, we are going to make use of &lt;a href="https://developer.mozilla.org/en/docs/Web/API/Fetch_API" rel="noopener noreferrer"&gt;Fetch API&lt;/a&gt;(Yes, this is another. We make use of different APIs every day) to make the request. We are going to write the method inside the apiFetch inside the if block.
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;apiFetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Enter&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="c1"&gt;// make API request&lt;/span&gt;
    &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;base_url&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;weather?q=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;searchKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;&amp;amp;units=metric&amp;amp;APPID=&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;api_key&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;})&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;We call the &lt;code&gt;fetch()&lt;/code&gt; with an &lt;code&gt;API endpoint&lt;/code&gt; &lt;code&gt;/weather&lt;/code&gt; and with the following &lt;code&gt;API queries&lt;/code&gt;:&lt;br&gt;
i. q=${searchKey.value}: which is the value of what the user input in the search box.&lt;br&gt;
ii. units=metric: which is the unit we want to retrieve back our weather value, you can learn more on the &lt;a href="https://www.openweathermap.org" rel="noopener noreferrer"&gt;weather Map&lt;/a&gt; website.&lt;br&gt;
iii. APPID=${api_key} which is our &lt;code&gt;API key&lt;/code&gt;&lt;br&gt;
We then wait for a response and when we got the response, we convert it to JSON. Here is a sample of our JSON&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxjw1l335vxyop163qds9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fxjw1l335vxyop163qds9.png" alt="json response"&gt;&lt;/a&gt;&lt;br&gt;
Finally, we are going to do is extract the values we want from the &lt;code&gt;jsonResponse&lt;/code&gt; and display it to the user. We should add the following to the code&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resJSON&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;countryName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;, &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="nx"&gt;countryCode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;country&lt;/span&gt;
   &lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;°c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;resJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;weather&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;
   &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resJSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;main&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;warm&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;className&lt;/span&gt; &lt;span class="o"&gt;=&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;that last &lt;code&gt;if statement&lt;/code&gt; is to apply a class to the app based on the data temperature.&lt;br&gt;
Congratulations, we've created a weather app and consume an API.&lt;br&gt;
You can find a list of public API to use in this &lt;a href="https://github.com/public-apis/public-apis" rel="noopener noreferrer"&gt;repo&lt;/a&gt;&lt;br&gt;
If you like to see the source code, you can find it on GitHub at &lt;/p&gt;
&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Aybee5" rel="noopener noreferrer"&gt;
        Aybee5
      &lt;/a&gt; / &lt;a href="https://github.com/Aybee5/api-tutorial" rel="noopener noreferrer"&gt;
        api-tutorial
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      The Codebase for API tutorial
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;api-tutorial&lt;/h1&gt;

&lt;/div&gt;
&lt;p&gt;The Codebase for API tutorial&lt;/p&gt;
&lt;/div&gt;



&lt;/div&gt;
&lt;br&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Aybee5/api-tutorial" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;br&gt;
&lt;/div&gt;
&lt;br&gt;


</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
