0
I am a beginner in programming and am doing some projects and studying to build a portfolio. My current problem is with an image API (Unsplash). I first tested by Somnia I made a GET request to https://api.unsplash.com/photos/random
, there I provided the API Client-ID in the Authorization header and successfully received a JSON with the image link and other information. My bottleneck is time to codify this. I tried to use fetch as in the code below:
function setWallpaper() {
fetch(`https://api.unsplash.com/photos/random`).then(response => {
return response.json();
})
.then(data => {
const { id } = data.id
console.log(id);
})
}
I hope to receive the image ID, but on the console nothing appears. Maybe it’s because we still have to put the Client-ID in the Authorization header, but I don’t know where to put this information in the code.
In
const { id } = data.id
actually you create an object and throw it away. Tryconst id = data.id
and your code should work.– bfavaretto