Issue with sending comments to Sanity studio from deployed version
Looking 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:
1. Add the environment variable to your hosting platform
Depending on where you're deploying (Vercel, Netlify, etc.), you need to add SANITY_API_TOKEN_COMMENT to your production environment variables:
- Vercel: Go to your project → Settings → Environment Variables
- Netlify: Go to Site settings → Build & deploy → Environment variables
- Other platforms: Look for environment variables or config settings
Make sure to use the same token value you have locally.
2. Fix a critical bug in your API route
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' })
}
}3. Verify the token has correct permissions
Make sure your token has:
- Editor or Contributor permissions (which you mentioned you have)
- No IP restrictions that might block your hosting provider's IPs
4. Check your deployment logs
Look at your serverless function logs in your hosting platform's dashboard. You'll likely see errors that reveal whether:
- The environment variable is undefined
- There's a network/CORS issue
- The token is invalid
5. Redeploy after adding environment variables
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 – Build the way you think, not the way your CMS thinks
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.