Exercise: JS Fetch API
Objective#
Your objective is to display a gif to the DOM based on a user search. You will need to create a giphy account and call their developer api endpoint. Let's play around with API calls to Giphy!
Getting Started#
Fork and Close the Exercise Repo to get started: JavaScript Fetch API
Prerequisites#
If you do have not a Giphy Developer account, visit developers.giphy.com, and create an account.
Steps#
The JavaScript Fetch API exercise has multiple steps:
- Exercise 1: Inspect and Select Elements
- Exercise 2: Use the Fetch API
- Exercise 3: Consume the API
- Exercise 4: Update the DOM
Exercise 1: Inspect and Select Elements#
Inspect the index.html file in Visual Studio code to see the starter code. Once you get a feel for the elements you'll be using in this exercise, head to your app.js
- Select the search button element
- Select the search input element
- Select the image element
- Select the feedback paragraph element. This element may need to be created (in HTML or JS)
Exercise 2: Use the Fetch API#
Use the Fetch API to request a gif based on the user's input term when the user clicks a button.
- Add a click event listener to the search button element
- The event handler function should:
- Call
fetch - Pass in the url: https://api.giphy.com/v1/gifs/translate
- Interpolate query parameters to the url
- all query parameters follow
name=valuesyntax api_key, should be the api key found on your Giphy Developer Dashboards, should be the value from the search input element
- all query parameters follow
- Interpolate query parameters to the url
- Call
Documentation on how to use Giphy's Translate endpoint can be found here.
An example url with query parameters would resemble:
Query parameters start after a ?, and each query parameter is separated by an &.
Exercise 3: Consume the API#
Use the result from the Fetch API with promise consumers.
- From the result of calling
fetch, call thethenpromise consumer method and pass in a callback function- The callback function should receive the resolved value as the parameter
- Return the result of calling
.jsonon the response- This will parse the response body to JSON, and return the promise result for the next promise consumer
- From the result of the first
thenpromise consumer, chain anotherthenpromise consumer method and pass in a callback function- The callback function should receive the resolved response body parsed to JSON as the parameter
- Start out just logging the result to console to check if it is the value you expect
- From the result of the
thenpromise consumer method, chain acatchmethod consumer and pass in a callback function- The callback function should receive the resolved value as the parameter
- Start out just logging the result to console to check if it is the value you expect
Exercise 4: Update the DOM#
- Replace the
console.logstatement in the secondthenmethod callback parameter to- Update the image element's
srcattribute tores.data.images.original.url- The giphy response returns many urls, so feel free to learn more about the others
- Reset the value of the search input element
- Reset the feedback
p's text content- This will hide previous errors on subsequent successful fetch requests by the user
- Update the image element's
- Below the
console.errorstatement in thecatchmethod- display the resolved failure object's
messageproperty as the text content of the selected feedbackpfrom the DOM
- display the resolved failure object's
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: