Skip to content

Instantly share code, notes, and snippets.

@dvbeelen
Last active April 9, 2019 14:54
Show Gist options
  • Save dvbeelen/4693c221a165433840ae25f58a87e4cb to your computer and use it in GitHub Desktop.
Save dvbeelen/4693c221a165433840ae25f58a87e4cb to your computer and use it in GitHub Desktop.
How To: Connect to the Unsplash Source API
//With the code below, you can connect to the Unsplash Source API. This API contains a lot of high-end images to use for free.
//I uploaded this here because I couldn't find how to do this anywhere online. Enjoy!
const imageWidth = 200; //Here you set how many pixels wide you want the recieved picture to be
const imageHeight = 200; //Here you set how many high you want the recieved picture to be
const searchTerm = ["house"]; //Here you set the term you want the API to search for.
const image = document.getElementsById("yourImage"); //Here you define which image you want to change.
fetch(`https://source.unsplash.com/${imageWidth}x${imageHeight}/?${searchTerm}`)
.then(data => fetchUnsplash(data))
function fetchUnsplash(data){
images.src = data.url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment