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!
Fork and Close the Exercise Repo to get started: JavaScript Fetch API
If you do have not a Giphy Developer account, visit developers.giphy.com, and create an account.
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
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
Use the Fetch API to request a gif based on the user's input term when the user clicks a button.
- Add an 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:
https://url.com?apiKey=abc&s=abcQuery parameters start after a ?, and each query parameter is separated by an &.
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
- Reset the feedback
p's text content- This will hide previous errors on subsequent successful fetch requests by the user
- 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
- 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
- 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
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: