Get a random GIF from GIPHY

Function JavaScript 1 revisions 252 a year ago a year ago
const axios = require('axios');

function getGIF(tag, rating) {
	const params = {
		api_key: API_KEY,
		format: 'json',
		limit: 1,

	return axios.get('https://api.giphy.com/v1/gifs/random', {params})
		.then(response => response.data.data.image_url);

This function gets a random GIF from GIPHY and returns its URL. You need to sign up as dev for GIPHY and get an API key beforehand.

getGIF().then(console.log); // https://media0.giphy.com/media/10bVAYmdzCiPqU/giphy.gif

You can getGIF(tags) to add some tags, and you can also use getGIF(tags, rating) if you want to provide a rating (r, g, etc.)

You can use anything other than axios if you wish, but I didn't use request because async/await ftw.