
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeAlright, let me break this down into simple steps for you! You're building an awesome customizable e-commerce shop, and you want to collect customer customization data (like text they want on products) and save it to Sanity. Let me explain this like you're five, then give you the technical details.
Think of it like this: When someone orders a custom birthday cake, they need to tell the baker what text to write on it. You need to:
You have two main approaches:
This is what the community answer you found suggests. Here's the flow:
The key insight from that thread: You can add custom data to your product object before adding it to cart, like:
const [options, setOptions] = useState({optionOne: '', optionTwo: ''});
product.options = options; // Attach custom data to productSince you mentioned you're new to backend stuff, I should tell you about Sanity Functions - these are serverless functions that run on Sanity's infrastructure, so you don't need to manage your own server! This is actually easier than webhooks for your use case.
Here's what you need to do:
Step 1: Add form fields to collect custom data
On your product page, add text inputs:
<div>
<label>Custom Text Line 1</label>
<input
type='text'
onChange={(e) => setCustomText({...customText, line1: e.target.value})}
/>
<label>Custom Text Line 2</label>
<input
type='text'
onChange={(e) => setCustomText({...customText, line2: e.target.value})}
/>
</div>Step 2: Attach this data to your cart items
When adding to cart, include the custom data with the product.
Step 3: Set up a Stripe webhook
Stripe webhooks notify your server when payments succeed. You'll need to:
/api/stripe-webhook)checkout.session.completed eventStep 4: Create an order document in Sanity
When Stripe confirms payment, use the Sanity Client to create a document:
import {createClient} from '@sanity/client'
const client = createClient({
projectId: 'your-project-id',
dataset: 'production',
token: 'your-write-token', // Keep this secret!
apiVersion: '2024-01-01',
})
// In your webhook handler
await client.create({
_type: 'order',
customerEmail: session.customer_email,
items: session.line_items,
customizations: customData,
stripeSessionId: session.id,
status: 'paid'
})Never expose write tokens in your frontend code! The token that lets you create documents in Sanity must stay on your server. This is why you need the webhook approach - it runs on your server (or in a serverless function) where the token is safe.
You can create a token with write permissions in Sanity Manage under the API tab. Store it in environment variables.
In your Sanity Studio, create a schema for orders:
export default {
name: 'order',
type: 'document',
title: 'Order',
fields: [
{name: 'customerEmail', type: 'string'},
{name: 'stripeSessionId', type: 'string'},
{
name: 'customizations',
type: 'object',
fields: [
{name: 'line1', type: 'string'},
{name: 'line2', type: 'string'},
]
},
{name: 'status', type: 'string'},
// Add other fields as needed
]
}If managing a webhook endpoint sounds complicated, consider using Sanity Functions. However, for Stripe integration specifically, webhooks are still the standard approach since Stripe needs to call YOUR endpoint when payment succeeds.
You're doing great tackling this as your first React project! The learning curve is steep but you're asking all the right questions. Good luck! 🎉
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store