Get a peek at our latest innovations at Sanity Product Day on Dec 8th →

Image URL

Official(made by Sanity team)

By Bjørge Næss & Espen Hovlandsdal

Helper library to generate URLs for image transformation


Quickly generate image urls from Sanity image records.

This helper will by default respect any crops/hotspots specified in the Sanity content provided to it. The most typical use case for this is to give it a sanity image and specify a width, height or both and get a nice, cropped and resized image according to the wishes of the content editor and the specifications of the front end developer.

In addition to the core use case, this library provides a handy builder to access the rich selection of processing options available in the Sanity image pipeline.

Getting started

npm install --save @sanity/image-url


The most common way to use this library in your project is to configure it by passing it your configured sanityClient. That way it will automatically be preconfigured to your current project and dataset:

import React from 'react'
import myConfiguredSanityClient from './sanityClient'
import imageUrlBuilder from '@sanity/image-url'

const builder = imageUrlBuilder(myConfiguredSanityClient)

function urlFor(source) {
  return builder.image(source)

Then you can use the handy builder syntax to generate your urls:


This will ensure that the author image is alway 200 pixels wide, automatically applying any crop specified by the editor and cropping towards the hot-spot she drew. You can specify both width and height like this:

  <img src={urlFor(movie.poster).width(500).height(300).url()}>

There are a huge number of useful options you can specify, like e.g. blur:

  <img src={urlFor(mysteryPerson.mugshot).width(200).height(200).blur(50).url()}>

Note that the url() function needs to be the final one in order to output the url as a string.

Builder methods


Specify the image to be rendered. Accepts either a Sanity image record, an asset record, or just the asset id as a string. In order for hotspot/crop processing to be applied, the image record must be supplied, as well as both width and height.

dataset(dataset), projectId(projectId)

Usually you should preconfigure your builder with dataset and project id, but even when you did, these let you temporarily override them if you need to render assets from other projects or datasets.


Specify the width of the rendered image in pixels.


Specify the height of the rendered image in pixels.

size(width, height)

Specify width and height in one go.

focalPoint(x, y)

Specify a center point to focus on when cropping the image. Values from 0.0 to 1.0 in fractions of the image dimensions. When specified, overrides any crop or hotspot in the image record.

minWidth(pixels), maxWidth(pixels), minHeight(pixels), maxHeight(pixels)

Specifies min/max dimensions when cropping

blur(amount), sharpen(amount), invert()

Apply image processing.

rect(left, top, width, height)

Specify the crop in pixels. Overrides any crop/hotspot in the image record.


Specify the image format of the image. 'jpg', 'pjpg', 'png', 'webp'


Specify transformations to automatically apply based on browser capabilities. Supported values:

  • format - Automatically uses WebP if supported


Rotation in degrees. Acceptable values: 0, 90, 180, 270


Compression quality, where applicable. 0-100


Make this an url to download the image. Specify the file name that will be suggested to the user.

flipHorizontal(), flipVertical()

Flips the image.


Specifies how to crop the image. When specified, overrides any crop or hotspot in the image record. See the documentation for details.


Configures the fit mode. See the documentation for details.


Specifies device pixel ratio scaling factor. From 1 to 3.


Adjusts the saturation of the image. Currently the only supported value is -100 - meaning it grayscales the image.


Ignore any specifications from the image record (i.e. crop and hotspot).

url(), toString()

Return the url as a string.


Other plugins by authors

Asset source for Unsplash
- has 16 likes

Official(made by Sanity team)
v3 Ready

Search photos on Unsplash and insert them directly inside of your Sanity Studio.

Go to Asset source for Unsplash

Mux input
- has 7 likes

v3 Ready

Upload videos to Mux directly from the studio.

Go to Mux input

Code Input
- has 12 likes

Official(made by Sanity team)
v3 Ready

Syntax highlighted editor for code.

Bjørge Næss
Go to Code Input