
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeLooking at your code, the issue is almost certainly that your SANITY_API_TOKEN_COMMENT environment variable isn't configured in your production deployment environment. The code works locally because you have it set up in your local .env file, but environment variables need to be separately configured in your hosting platform.
Here are the steps to fix this:
Depending on where you're deploying (Vercel, Netlify, etc.), you need to add SANITY_API_TOKEN_COMMENT to your production environment variables:
Make sure to use the same token value you have locally.
Your API route has a problem - it's not awaiting the Sanity client's create() method and not sending a proper response. This could cause silent failures:
export default async function comment(req, res) {
try {
const { name, text, email, _id } = JSON.parse(req.body)
await client.config({
token: process.env.SANITY_API_TOKEN_COMMENT
}).create({
_type: 'comment',
name,
comment: text,
email,
post: {
_type: 'reference',
_ref: _id
}
})
return res.status(200).json({ message: 'Comment submitted' })
} catch (error) {
console.error(error)
return res.status(500).json({ error: 'Failed to submit comment' })
}
}Make sure your token has:
Look at your serverless function logs in your hosting platform's dashboard. You'll likely see errors that reveal whether:
After adding the environment variable, you may need to trigger a new deployment for it to take effect (some platforms require this).
The most common cause of "works locally but not in production" for Sanity mutations is missing environment variables in the deployment platform. Once you add SANITY_API_TOKEN_COMMENT to your hosting platform's environment variables and fix the await issue, your comments should start working in production.
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