
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeFor sending email and SMS notifications to clients after they make an order in your e-commerce store, webhooks are the stable, production-ready approach that Sanity has offered for years. Webhooks automatically send HTTP requests to your endpoints when content changes occur, making them perfect for triggering order notifications.
Webhooks are GROQ-powered HTTP callbacks that trigger when documents are created, updated, or deleted in your Content Lake. Here's how to set them up for e-commerce order notifications:
First, create an endpoint on your server (or serverless function) that will receive webhook requests and send the actual email/SMS. This could be:
/api/send-order-notification)You can create webhooks through the Sanity Manage console:
https://yoursite.com/api/send-order-notification)production)_type == "order"{_id, customerEmail, customerPhone, orderTotal, items}The projection uses GROQ to shape exactly what data gets sent to your endpoint, keeping payloads lean and focused.
Here's an example Next.js API route that receives the webhook and sends notifications:
// app/api/send-order-notification/route.ts
import { NextRequest, NextResponse } from 'next/server'
import sgMail from '@sendgrid/mail'
import twilio from 'twilio'
sgMail.setApiKey(process.env.SENDGRID_API_KEY!)
const twilioClient = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
)
export async function POST(request: NextRequest) {
try {
const order = await request.json()
// Send email
await sgMail.send({
to: order.customerEmail,
from: 'orders@yourstore.com',
subject: 'Order Confirmation',
html: `<h1>Thank you for your order!</h1><p>Order total: $${order.orderTotal}</p>`
})
// Send SMS
if (order.customerPhone) {
await twilioClient.messages.create({
body: `Thank you for your order! Total: $${order.orderTotal}. We'll send updates soon.`,
from: process.env.TWILIO_PHONE_NUMBER,
to: order.customerPhone
})
}
return NextResponse.json({ success: true })
} catch (error) {
console.error('Notification error:', error)
return NextResponse.json({ error: 'Failed to send notifications' }, { status: 500 })
}
}Add webhook signature verification to ensure requests are actually from Sanity:
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook'
const secret = process.env.SANITY_WEBHOOK_SECRET!
const signature = request.headers.get(SIGNATURE_HEADER_NAME)!
const body = await request.text()
if (!isValidSignature(body, signature, secret)) {
return NextResponse.json({ error: 'Invalid signature' }, { status: 401 })
}You'll find the webhook secret in the Sanity Manage console after creating your webhook.
You'll need accounts with notification providers:
Sanity also offers Functions, a newer serverless compute option that runs directly on Sanity's infrastructure. However, Functions are currently experimental with APIs subject to change. For production e-commerce systems where reliable order notifications are critical, webhooks are the stable, battle-tested choice.
You might consider Functions in the future once they reach general availability, but for now, webhooks give you:
Before going live, test your webhook with tools like:
Check out the full webhooks documentation and best practices guide for more details on filters, projections, and advanced configurations!
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