Sanity exec script fails with "Insufficient permissions" despite administrator role
The "Insufficient permissions" error you're encountering when running sanity exec xyz.js is almost certainly because you need to add the --with-user-token flag to your command.
By default, when you run sanity exec, the script doesn't automatically use your authenticated user credentials. Even though you have the administrator role on your account, the script needs explicit permission to use your user token to perform mutations (create, update, or delete operations) on your dataset.
The Solution
Run your script with the --with-user-token flag:
sanity exec xyz.js --with-user-tokenThis flag provides your script with an authenticated client that has the same permissions as your logged-in user account. Without it, the script runs with limited or no permissions, which is why you're getting the 403 Forbidden error with "Insufficient permissions; permission 'update' required."
Verify Your Login
If adding the flag doesn't resolve the issue, you can verify which account you're currently logged in with using:
sanity whoamiThis will show you the current authenticated user and help confirm you're logged in with the correct account that has administrator privileges.
Why This Happens
The --with-user-token flag is a security feature. It prevents scripts from accidentally or maliciously performing operations without explicit authorization. This is especially important when running scripts that modify your production dataset, which is why it's always recommended to export your dataset before running any mutation scripts:
sanity dataset exportAs shown in the community thread about this exact issue, adding the --with-user-token flag resolves this permissions error. The error you're seeing is a security measure to ensure scripts have explicit authorization before performing write operations on your content.
Show original thread8 replies
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.