Visual Editing

Create Studio edit intent links

Learn how to construct URLs that open specific documents and fields in Sanity Studio for custom editorial interfaces and preview environments.

Edit intent links are URLs that open specific documents and fields in Sanity Studio. They are useful for building custom editorial interfaces or adding edit buttons to your preview environments.

When combined with Content Source Maps or steganography, you can automatically generate these URLs based on source map data for fully automated visual editing experiences. This article covers the manual approach for cases where you need direct control over the links. The resolveEditUrl helper covers the automatic approach, for cases where you have a Content Source Map but not the document ID.

Edit intent URL format

The basic format for an edit intent URL is:

Constructing edit URLs programmatically

You can construct edit intent URLs programmatically. The following helper function builds URLs for documents, specific fields, and nested field paths:

Adding edit buttons to a preview interface

You can use edit intent URLs to add edit buttons to your preview interface. Here are examples for document-level and field-level edit links:

Resolve edit URLs from a Content Source Map

When you render query results, you don't always have the document ID and type at hand. A slug used to build a URL, for example, never appears on the page. resolveEditUrl from @sanity/client/csm takes a path into a query result and resolves the source document, type, and field path from the query's Content Source Map.

Alpha API

Two settings are required before you can resolve a URL:

  • Set resultSourceMap: 'withKeyArraySelector' in the client config. Plain true also returns a source map, but links to array items break when the array is reordered.
  • Pass filterResponse: false to client.fetch(). The default response contains the result only, without the source map.

The resolved URL carries the intent parameters as a path segment and repeats them as a query string:

Differences from a manually built link

Links from resolveEditUrl differ from the ones you build yourself:

  • The URL always sets mode=presentation, so it opens the document in the Presentation tool rather than the default document editor.
  • A resultPath is required. The underlying createEditUrl throws path is required, so you can't build a document-only link this way.
  • The field path is percent-encoded, and perspective=published is appended when the source document is published.
  • Unresolvable paths return undefined. A value computed in the query maps to no document field, for example. Check the return value before you render a link.

Related resources

Was this page helpful?