Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
require('dotenv').config();
require("dotenv").config();

// Print out value of API key stored in .env file
console.log(process.env.API_KEY)
async function getImage(query) {
try {
const response = await fetch(
`https://api.giphy.com/v1/gifs/search?api_key=gfMs7vGi9QVQkckQYuOcXeOqMxU3hFbv&q=${query}&limit=25&offset=0&rating=g&lang=en&bundle=messaging_non_clips`
);
const data = await response.json();

const randomNum = Math.floor(Math.random() * 26); //pick random number between 0 and 25

if (data.data[randomNum]) {
//if the object at index randomNum exists
const url = data.data[randomNum].images.original.url;
console.log(url);
return url;
} else {
console.warn("This data does not exist.");
return null;
}
} catch (err) {
console.error("Ran into an error: ", err);
}
}

getImage("spongebob");