What's the best way to download and store Twitch profile pictures locally?

I’m working on a Twitch chat widget and need help with image handling. Right now, I’m fetching user profile pictures from the Twitch API. Here’s a simplified version of what I’m doing:

function getProfilePic(username) {
  fetch(`https://api.twitch.tv/helix/users?login=${username}`, {
    headers: { 'Client-ID': 'my-client-id' }
  })
  .then(response => response.json())
  .then(data => {
    let picUrl = data.data[0].profile_image_url || 'default-pic-url';
    displayProfilePic(picUrl);
  });
}

function displayProfilePic(url) {
  let img = document.createElement('img');
  img.src = url;
  document.body.appendChild(img);
}

This works, but I want to save these images locally to reduce API calls. How can I:

  1. Check if we already have the image saved
  2. If not, download and save it with the username as the filename
  3. Use the local file instead of the API URL

Any tips on implementing this in JavaScript?

hey, i’ve dealt with this before. u could use the file system API to save images locally. check if file exists, if not download it. somethin like:

if (!fs.existsSync(`${username}.jpg`)) {
  downloadImage(url, `${username}.jpg`);
}

the use the local path insted of the API url. hope this helps!