๐Ÿ‘€ See Sanity in action: Watch product demo now โ†’

Upload An Image From a URL

By RD Pennell

Fetch an image from a URL and upload to Sanity it via the JS Client

Warning

This schema is for an older version of Sanity Studio (v2), which is deprecated.

Learn how to migrate to the new Studio v3 โ†’

uploadImageFromUrl.js

import client from 'part:@sanity/base/client';
import axios from 'axios';

const studioClient = client.withConfig({ apiVersion: '2021-10-21' });

const uploadImageFromUrl = async url => {
  const imageBuffer = await axios
    .get(url, { responseType: 'arraybuffer' })
    .then(res => Buffer.from(res.data, 'utf-8'));

  studioClient.assets
    .upload('image', imageBuffer)
    .then(img => console.log(`Image successfully uploaded: ${img.url}`))
    .catch(err => console.log(err));
};

The client.assets.upload method only accepts a File, Blob, Buffer, or Nodestream. Here's a method for fetching an image from a url, converting it to a Buffer, then uploading to your dataset.

Contributor

Other schemas by author