<?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: flugelg</title>
    <description>The latest articles on DEV Community by flugelg (@flugelg).</description>
    <link>https://dev.to/flugelg</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%2F805149%2F0e699615-4153-44ee-b67f-80f977727022.png</url>
      <title>DEV Community: flugelg</title>
      <link>https://dev.to/flugelg</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/flugelg"/>
    <language>en</language>
    <item>
      <title>Creating My First API Javascript Webpage</title>
      <dc:creator>flugelg</dc:creator>
      <pubDate>Mon, 07 Feb 2022 19:39:05 +0000</pubDate>
      <link>https://dev.to/flugelg/creating-my-first-api-javascript-webpage-l0b</link>
      <guid>https://dev.to/flugelg/creating-my-first-api-javascript-webpage-l0b</guid>
      <description>&lt;p&gt;The first task of any project is to figure out what you want to do it on. I was searching through public APIs but wasn't getting anywhere. I just couldn't find one that interested me or I wasn't sure how I would use them. As I was aimlessly searching I had an idea, maybe there was an api that returns something random. Since I was searching randomly, I thought, why don't I create a page that returns a random task or activity. I then found &lt;a href="//boredapi.com"&gt;boredapi&lt;/a&gt;, which does exactly what I was looking for. It would give a random activity to do, you can have different variables like the amount of people the activity involves or the price of the activity. You would use a query like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http://www.boredapi.com/api/activity/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to output the random activity. I had made a simple page based on this. You would press a button and it would just display the activity, however I couldn't figure out how to make this look better or more functional. I also didn't really like the activities it displayed because I wouldn't like to do most of them so I went back on the search for a different API. I thought, what can be useful to me? Everyday during or before work I wonder what I should eat for lunch, so I decided to create a webpage that would give me options on food to make for lunch. I found a nice API, &lt;a href="https://developer.edamam.com/edamam-docs-recipe-api" rel="noopener noreferrer"&gt;Adamam&lt;/a&gt;, that would give me a lot of information on recipes, everything from images, ingredients, dietary information, as well as other info. &lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2F6cm61457hxr9irzcackt.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%2Fuploads%2Farticles%2F6cm61457hxr9irzcackt.png" alt="recipe information"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I thought this would be a good API to use. From there I started to get working on remaking my project. I started with a simple search box where you can put in any food or ingredient and would output a recipe containing your desired food and the ingredients for the recipe. Since the ingredients are an array I used a forEach() method to iterate the array and display it on an unordered list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ingredientsArray.forEach((ingr) =&amp;gt;{ //iterate array for left ingredients
        let li = document.createElement("li");
        li.textContent = ingr.text;
        ingredientsLeft.appendChild(li);
    })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&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%2Fuploads%2Farticles%2F6dhsg5toywuu0d98tki9.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%2Fuploads%2Farticles%2F6dhsg5toywuu0d98tki9.png" alt="Ingredients"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I thought that the page looked a little plain and it would be a little more functional if you had more than one option, so since the API gave 20 results I decided to display 2 results instead of just one. These results would just be rendered side by side. I would display the food label, an image, and the ingredients list. I could display a lot more information but I wanted it to look simple and I could expand on the idea later if I pleased. If I wanted to save the food I had to make a button to favorite it. I placed that button above the image and below the label, to simulate backend I used &lt;a href="https://www.npmjs.com/package/json-server" rel="noopener noreferrer"&gt;json-server&lt;/a&gt;. Favoriting the food would put the food name into a json object. To do this I would just create an event listener that would make an array out of the current food label. This array would be passed to a fetch request that would then add the label to a db.json file inside the directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelector('#favorite').addEventListener('click', function(){
        console.log(document.querySelector('#foodLabel1').textContent);
        let favorited1 = [document.querySelector('#foodLabel1').textContent];
        favoriteFoods(favorited1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function favoriteFoods(favorited){
    fetch("http://localhost:3000/favorites", {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
        },
        body: JSON.stringify(favorited)
    })
    .then(res =&amp;gt;res.json())
    .then(food =&amp;gt; console.log(food))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created my own logo to put over the search bar to make it look a little less plain. As a little add-on I added a vegan checkbox, since my girlfriend is vegan a vegan option would be nice. This would just change the API query a little to only search for vegan options based on the food inputed. The only thing left to do is to add to my CSS so it would all align how I wanted it. After that was done I completed my first API javascript project. It wasn't the most complicated project but I think it turned out nice. There are ways I can add-on to this project in the future to make it more functional and give more options to the user if I wished. But, for my first full project I considered it a success. &lt;/p&gt;

&lt;p&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%2Fuploads%2Farticles%2Ftbofrmcxlfemftbn3m2t.gif" 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%2Fuploads%2Farticles%2Ftbofrmcxlfemftbn3m2t.gif" alt="Page gif"&gt;&lt;/a&gt;&lt;br&gt;
For my next project I hope to use lessons learned from this one, like trying different methods for the same results, asking questions when stuck, and just thinking about how I would like to use the product if I was the user.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>api</category>
    </item>
  </channel>
</rss>
