<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Samer Bibi on Medium]]></title>
        <description><![CDATA[Stories by Samer Bibi on Medium]]></description>
        <link>https://medium.com/@samerbibi?source=rss-6b2c4a51c1b7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*McsbrciF7Z3tHw-4mJwCDA.jpeg</url>
            <title>Stories by Samer Bibi on Medium</title>
            <link>https://medium.com/@samerbibi?source=rss-6b2c4a51c1b7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 07:33:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samerbibi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Making your first request to a RESTful API]]></title>
            <link>https://medium.com/@samerbibi/making-your-first-request-to-an-external-restful-api-7c62a218bddc?source=rss-6b2c4a51c1b7------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c62a218bddc</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[food]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[restful-api]]></category>
            <category><![CDATA[weather]]></category>
            <dc:creator><![CDATA[Samer Bibi]]></dc:creator>
            <pubDate>Sat, 11 Aug 2018 19:54:29 GMT</pubDate>
            <atom:updated>2018-08-12T16:11:09.014Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jy7jgnkUpJsW5qFKYpfZaA.png" /><figcaption>REST easy</figcaption></figure><p>Before getting into development, I was always fascinated by the world of APIs. These modern gatekeepers to the web’s data seemed like mystical entities that were beyond the common folk. However, after familiarizing myself with APIs, I learned they are easier to interact with than I expected. In this guide, I’ll walk through two examples of the minimum steps required to retrieve data from a third-party REST API. Both examples assume at least a basic understanding of JavaScript.</p><p>In the first example, we’ll be querying OpenWeatherMap for city weather data using the fetch API (that’s right, we will be using an API to query an API)! In the second example, we’ll make a slightly more complex request to Edamam’s API to retrieve some food recipes, this time using axios. The first method will be a little quicker to get up and running whereas the second requires some additional configuration. Upon retrieving our data, we will visually display it in the browser.</p><p><strong>Requesting weather data from the OpenWeatherMap API</strong></p><p>Before we can start flinging requests at their server, we’ll need an API key. The API key is mostly to identify who’s making the requests. To obtain this magical key we’ll need to <a href="https://openweathermap.org/appid">create an account</a> and wait a whole 10 minutes. Okay great, so we have our API key and apparently have authorization to make requests. So how do we do that?</p><p>A good place to typically get started when working with any API is the <a href="https://openweathermap.org/current">documentation</a>. In fact before we even write any code, we can check that our key is working and that we can indeed query OpenWeatherMap for weather data. There’s a bunch of different ways to specify what type of weather data we want, but for simplicity, we’re going to make a request for the current weather of a given city. Sifting through the docs, it seems that we should be able to make a request for Chicago’s current weather by entering the following into the browser’s URL bar.</p><pre>http://api.openweathermap.org/data/2.5/weather?q=chicago&amp;appid=YOUR-API-KEY</pre><p>Don’t forget to replace YOUR-API-KEY with your actual API key. If the request succeeded, you should see an ugly looking JSON object in your browser (if you don’t already have one, I’d recommend installing a JSON viewer to more easily digest the response data). If everything worked, you should see something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*2FGb74C71V34BjA5V2ypVA.png" /></figure><p>Before we move on, let’s take a quick look at the API request. The stuff we’re interested in starts with the ? symbol after weather. The ? indicates that is where the query string begins, which is essentially a way to specify things we want using parameters. Parameters are just name-value pairs where the = symbol separates the name from the value. The &amp; symbol indicates additional query parameters. Here’s a helpful graphic that breaks it all down:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/653/0*R4MyvQ66uKapjwrv" /><figcaption><a href="https://howto.caspio.com/parameters/parameters-as-query-string-values/">Source</a></figcaption></figure><p>So in our example we’re asking OpenWeatherMap to provide us the weather for Chicago q=chicago, in addition to providing our API keyappid=YOUR-API-KEY. Okay, now that we can make successful API requests, let’s plug it into an application so that we don’t have to manually type a long URL each time we want the weather.</p><p>In order to host our application and make AJAX requests, we’re going to need a server, even if that server only has one root path. For this tutorial we’ll be using <a href="https://webpack.js.org/guides/development/">webpack-dev-server</a>, a handy development server.</p><p>Setting up webpack-dev-server is a quick process that only only requires a few installs and minimal configuration. Since we’re starting from scratch let’s create a directory for this application using the CLI and navigate there.</p><pre>$ mkdir demo<br>$ cd demo/</pre><p>Next, let’s initialize a <em>package.json</em> file.</p><pre>$ npm init -y</pre><p>Let’s install what we’ll need for webpack-dev-server by running the following command:</p><pre>$ npm install --save-dev webpack webpack-cli webpack-dev-server</pre><p>Great, now that we have those installed, let’s go ahead and set up a <em>webpack.config.js</em> file.</p><pre>$ touch webpack.config.js</pre><p>We’ll need to tell webpack-dev-server where to obtain and serve our html file from, which we’ll specify within the devServer property. Since our html file is going to live in the root directory that’s where we’ll indicate to find it in the <em>webpack.config.js</em> file.</p><pre>module.exports = {<br>  mode: &quot;development&quot;,<br>  entry: &quot;./weather.js&quot;,<br>  output: {<br>    path: __dirname,<br>    filename: &quot;bundle.js&quot;<br>  },<br>  <strong>devServer</strong>: {<br>    contentBase: &quot;./&quot;<br>  }<br>};</pre><p>Next, let’s create a script in our <em>package.json:</em></p><pre>&quot;scripts&quot;: {<br>  &quot;start&quot;: &quot;webpack-dev-server --open&quot;<br>},</pre><p>Last but not least, we’ll need an <em>index.html</em> and <em>weather.js</em> file where we’ll implement the functionality to make AJAX requests. I’m also going to add a CSS file so that the Chrome console doesn’t yell at me.</p><pre>$ touch index.html weather.js main.css</pre><p>Don’t forget to set up at least a bare-bones HTML document and specify <em>weather.js</em> in the script tag!</p><p>In <em>weather.js</em>, we’ll write a simple function to retrieve data from the OpenWeatherMap API for us.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4babbdecc3eb71c2d0ed8eb696b590d1/href">https://medium.com/media/4babbdecc3eb71c2d0ed8eb696b590d1/href</a></iframe><p>On line 5 we specify the full API URL and pass it as an argument to fetch, which makes an AJAX request to the server. Since fetch() returns a promise, we use .then() to convert the response to a JSON object using the .json() method. Finally we log the JSON object to the console and append certain properties to an HTML table. Note that the function definition that performs this operation on line 10 has been omitted for brevity.</p><p>Now, for the final test. Let’s boot up our app and see if we’re successfully requesting our data from OpenWeatherMap.</p><pre>$ npm start</pre><p>If everything worked correctly, you should at a minimum see the response object in the console and a table with the data if you’ve added that in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/0*HZoP1_RbleKqXB9R" /></figure><p>Notice that the temperature might look abnormally high. Fret not, the world isn’t melting just yet. OpenWeatherMap happens to use Kelvin as its default unit if that parameter isn’t specified (i’ll leave that one for you). And there we have it! We’ve successfully retrieved some basic weather data from the OpenWeatherMap API.</p><p><strong>Bonus Round — requesting recipes from the Edamam API</strong></p><p>Most of the steps required here will be similar to those listed above so instead I’ll just focus on what’s different. Since we’re going to be importing axios, we’ll need to use webpack and babel together.</p><p>Let’s go ahead and install everything we’ll need.</p><pre>$ npm i --save-dev axios babel-core babel-preset-env</pre><p>Next let’s create a <em>.babelrc</em> file</p><pre>$ touch .babelrc</pre><p>and specify the presets in that file:</p><pre>{<br>   &quot;presets&quot;: [&quot;env&quot;]<br>}</pre><p>Now let’s change the source path of our javascript file in our <em>index.html</em> script tag to reference webpack’s bundled output file.</p><pre>&lt;script <em>src</em>=&quot;bundle.js&quot;&gt;&lt;/script&gt;</pre><p>Lastly, let’s change the entry point in our<em> webpack.config.js</em> file to reference the <em>recipes.js</em> file we’ll be creating.</p><pre>module.exports = {<br>  mode: &quot;development&quot;,<br>  <strong>entry</strong>: &quot;./recipes.js&quot;,<br>  output: {<br>    path: __dirname,<br>    filename: &quot;bundle.js&quot;<br>  },<br>  devServer: {<br>    contentBase: &quot;./&quot;<br>  }<br>};</pre><p>Okay, we’re finally ready to create a <em>recipes.js</em> file to obtain our recipes from the Edamam API using axios. Let’s import axios and modify the fetchWeather function we used to make a slightly more involved request for some recipes.</p><p>Rather than fetching recipes based on one sole food input, we’re going to build a more specific query where we’ll search recipes based on multiple food items. We’re not quite ready to go master chef though so let’s also cap our recipes to no longer than 30 minutes cooking time and a maximum of 10 ingredients.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23ff2aa5cc841759585d5c8d6e69e197/href">https://medium.com/media/23ff2aa5cc841759585d5c8d6e69e197/href</a></iframe><p>Let’s walk through this code. For the most part, it’s employing the same technique we used earlier to fetch our weather data, but has a little bit more going on.</p><p>The main differences here compared to the fetchWeather function are the following:</p><ul><li>The fetchRecipes function takes any number of arguments.</li><li>The …ingredients notation on line 9 is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest parameter syntax</a>, which allows for any number of arguments to be represented as an array. This is great for using array methods right off the bat.</li><li>On lines 10–18 we’re manipulating these arguments to create a string that separates food items with a + symbol since that is the syntax the Edamam API expects for a query with multiple food items.</li><li>We’ve added some parameters to our API query and added constraints to those parameters.</li><li>Time can be no greater than 30 minutes (line 6).</li><li>Ingredients can be no greater than 10 food items (line 7).</li><li>We’re using axios.get() instead of fetch().</li><li>We’re using an asynchronous function through the use of async/await.</li><li>Edamam requires an API key <strong>and</strong> an API id.</li></ul><p>Note that the order we specify certain parameters matters. For example, If we mistakenly swap the order of appId and appKey, the server will respond with a 404 status.</p><p>Let’s run our app and take a look at the response object in the console. We can see that each recipe (found in the hits property) includes <strong>a lot</strong><em> </em>of information. This time instead of populating a table, for the first 4 recipes, I’ve grabbed the image of the recipe and its corresponding link to the website where the full recipe can be found. As in the previous example, I’ve omitted this function definition for brevity.</p><p>If everything worked out, you might see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*t8kynX1U21STaF7o5oRX4w.png" /></figure><p>Who who have thought zucchini, broccoli and carrots could look so tasty!</p><p>And that’s a wrap! We’ve learned how to make queries of varying complexities to third-party RESTful APIs. It’s easy to imagine the endless possibilities of what can be created when working with APIs. For example rather than hard-coding the inputs as arguments into the function, we could add an input field onto the page to allow users to search weather by area code or recipes by ingredients and diet constraints. The options are vast and there’s no reason to stop here! I hope this tutorial helped you better understand how to work with RESTful APIs. Thanks for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c62a218bddc" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>