Skip to content

Instantly share code, notes, and snippets.

@kevinweber kevinweber/giphy.js
Created Feb 19, 2018

Embed
What would you like to do?
Giphy API Example
import {
checkStatus,
parseJSON
} from './request';
/**
* Request data Giphy API
* https://developers.giphy.com/explorer/
*/
const endpoint = 'https://api.giphy.com/v1/gifs/search';
export default function requestGif({
apiKey = '', // <-- provide this!
lang = 'en',
limit = '1',
offset = '0',
rating = 'G',
term = 'shrug',
} = {}) {
const queryUri = `?api_key=${apiKey}&q=${term}&limit=${limit}&offset=${offset}&rating=${rating}&lang=${lang}`;
return fetch(endpoint + queryUri)
.then(checkStatus)
.then(parseJSON);
}
import requestGif from './giphy';
requestGif({
apiKey: CONFIG.giphyApiKey,
term: CONFIG.giphyTerm,
}).then((response) => {
const newImage = document.createElement('img');
newImage.src = response.data[0].images.fixed_width.url;
// Insert new image element into some DOM element with `data-image="giphy"`
const target = document.querySelector('[data-image="giphy"]');
target.appendChild(newImage);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.