Media Library

Upload assets

Programatically upload assets to your Media Library.

Users can upload assets to Media Library directly from the app or from a configured Studio. Developers can upload assets with the CLI and the Media Library HTTP API. In this guide, you'll learn to upload an asset to your library programatically.

Upload an asset

Option 1: use the CLI

The Sanity CLI includes the media import command that standardizes the process for uploading batches of assets and simultaneously assigning aspect information. More detail is available in our guide for importing assets.

Option 2: use the HTTP API

Prerequisites:

  • Node.js v21.0 or later to run the built-in fetch API, or a compatible request library.
  • A personal authentication token with read/write access to your organization's Media Library.
  • The mediaLibraryId.

Make a POST request to the /media-libraries/<mediaLibraryId>/upload endpoint. In the code below, replace mediaLibraryId with your library's ID, and token with your personal authentication token.

// Define your library ID.
const mediaLibraryId = '<your-library-id>'

// Define your personal auth token.
const token = '<personal-auth-token>'

// Read the contents of a file.
const asset = fs.readFileSync('assets/spring-launch-promo.jpg')

// POST a request to the HTTP API with the file as the body
await fetch(`https://api.sanity.io/v2025-02-19/media-libraries/${mediaLibraryId}/upload`, {
  method: 'POST',
  body: asset,
  headers: {
    'Authorization': `Bearer ${token}`,
  }
})

Uploading an asset without additional parameters will let Media Library generate the asset title and infer the filename.

See the Media Library HTTP API reference for all available upload options and parameters.

Was this page helpful?