This guide explains how to use the "Sanity Schema Mapper" node to prepare your data for Sanity's Content Lake.
The Sanity Schema Mapper is a transformation node. Its single purpose is to take data from any source and reshape it into a perfectly formatted document that matches your Sanity.io schema.
It intelligently handles Sanity's special field types, such as slug
, reference
, and portableText
, so you don't have to build complex JSON objects manually.
After adding the node to your workflow, you will see two main configuration panels.
This is the most important field. The node uses this schema to understand how to transform your data.
How to get the schema:
In your Sanity Studio's source code, find the schema definition file for the document type you want to create (e.g., post.js
or product.ts
).
This file exports a JavaScript object. You need to convert this object into valid JSON. You can use an online converter or simply copy the object and manually ensure it follows JSON syntax (e.g., double quotes on all keys).
Action: Paste the complete JSON schema object into this field.
Example Schema:
{
"name": "post",
"title": "Post",
"type": "document",
"fields": [
{ "name": "title", "title": "Title", "type": "string" },
{ "name": "slug", "title": "Slug", "type": "slug" },
{ "name": "body", "title": "Body", "type": "array", "of": [{ "type": "block" }] }
]
}
This is where you tell the node how to map your incoming data to the Sanity fields defined in your schema.
Sanity Field Path: The destination field in your Sanity document. You can use dot notation for nested fields.
Example: title
Example: slug
(Note: you don't need to specify .current
, the node handles this)
Example: author
(for a reference field)
Input Value: The data you want to place in the Sanity field. You will almost always use an n8n expression here to get data from a previous node.
Example: {{ $json.postTitle }}
Example: {{ $json.id_from_another_system }}
The power of this node is in its ability to automatically format special Sanity types.
If your Schema Field is... | And you provide an Input Value like... | The Node will automatically create... |
---|---|---|
slug | "my-first-post" | { "_type": "slug", "current": "my-first-post" } |
reference | "userId123" | { "_type": "reference", "_ref": "userId123" } |
image | "image-asset-abc" | { "_type": "image", "asset": { "_type": "reference", "_ref": "image-asset-abc" } } |
array of block (Portable Text) | "This is the body of my post." | [{"_type":"block", "style":"normal", ...}] |
Here is a typical use case for creating a new blog post in Sanity from a webhook.
Workflow: Webhook
-> Sanity Schema Mapper
-> Sanity Mutation
Webhook Node: Receives incoming data.
{ "title": "My New Post", "content": "Hello world!", "slug": "my-new-post" }
Sanity Schema Mapper Node:
Sanity Document Schema: Paste the JSON for your post
schema.
Field Mappings:
title
-> {{ $json.body.title }}
slug
-> {{ $json.body.slug }}
body
-> {{ $json.body.content }}
Sanity Mutation Node:
Credentials: Select your Sanity API credentials.
Operation: Create
Document (JSON): The output from the Mapper node is already perfectly formatted. You can simply use an expression to pass it along: {{ $json }}
The workflow is now complete. When the webhook receives data, a new, correctly formatted post will be created in your Sanity Content Lake.