Skip to content

Instantly share code, notes, and snippets.

@kevinweber
Created February 19, 2018 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinweber/b3ff455b2ab3e310a6042ea5b22f1356 to your computer and use it in GitHub Desktop.
Save kevinweber/b3ff455b2ab3e310a6042ea5b22f1356 to your computer and use it in GitHub Desktop.
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