GOAL: Today you will be getting creating an application that utilizes fetch to an image from Giphy. We will be able to request an image from Giphy and then get back a gif to match this query.
- Install the dependencies by running:
npm install
Many website have Application Program Interfaces (APIs) that allow you to request data and return this data back to be used in your applications. Some APIs, like the PokeAPI, return the data so long as you have the URL. However, this can lead to some security issues as the organization does not know who is requested the data.
API Keys allow for applications to authenticate an application or user that is requesting data.
- Go to https://developers.giphy.com/.
- Login or create a developer account.
- Click "Create an App".
- Select the "SDK" Option
- Give your application name (e.g. "Giphy Async Practice") and description (e.g "Learning about async functions") you like.
You should never save sensitive values, like an API key, in a file that could be committed to GitHub. We save these files in a
.env
file as this is ignored typically with our.gitignore
file.
- Copy the API key and save the value in your .env file in the API_KEY variable.
# .env variable
API_KY = "abcd123456789"
Giphy has a very useful API Explorer that lets you preview the values that will be returned from your requested endpoint.
- Navigate the "API Explorer"
- Choose an endpoint of Search
- Type a hardcoded query in the
q REQUIRED
field under Parameters. Notice that the Request URL update theq =
value as you type this.
- Click "Send Request" to see the expected endpoint.
- Notice that the Request URL has the API contained within the URL! Copy the Request URL and save it in a variable called endpoint in your index.js.
- To access an environment variable you simply need to call
process.env.API_KEY
inindex.js
. An example call has already been created for you! - Interpolate this in to your request URL.
const endpoint = `https://api.giphy.com/v1/gifs/search?api_key=${process.env.API_KEY}&q=dogs&limit=25&offset=0&rating=g&lang=en`
GOAL: Create an asynchronous getImages(query)
function that creates a fetch
request to the API endpoint that returns a URL link to a gif that matches the search value.
- Declare an asynchronous function
getImage(query)
. - Create a
fetch
request to the endpoint. Don't forget toawait
the result! - Parse the data using
.json()
. Don't forget toawait
the result! - Save the URL of a single gif image from the JSON object in a variable. Print this value to the console to ensure you get a link that works!
- Refactor the endpoint so that
q=dogs
contains the value you are searching for withquery
. - Return the image from the function.
You now have the power of the entire Giphy library, the opportunities are endless. Here are a few extensions you can explore.
- Instead of getting the first image, get a random image from the response object. There are 25 images in the default response object. Randomly return this gif
- Iterate through the response object and save all 25 images an array.
- NOTE: You will need to create an asynchronous helper function that invokes your getImage so that you can access returned value.
async function helperFunction() { await getImage("dogs"); }
We have not learned about web servers yet so
.env
variables will not work for this part. Simply don't commit your API Key to GitHub for now. You can research how to incorporate secrets, but it is far outside the scope of this practice!
- Create a user interface. One possible way you can do this:
- Create
index.html
andstyle.css
files. - Create and style a button.
- Attach an
addEventListener
to the button that calls thegetImage(query)
function with some hard coded value - Output the returned URL to the screen in an
<img>
tag usingdocument.querySelector()
andinnerHTML
.
- Create
- Incorporate search functionality on your user interface so that a user can search for any image. One possible way to do this:
- Create an HTML form that has an
<input>
element. This will be where the user can type their search. - Research how to get values from a form when they are submitted. The value should be used to call
getImage(query)
withquery
being the searched value. - Output the result of the search to the screen.
- Create an HTML form that has an
- Output all 25 images to the page when they search for them rather than a single image.