Troubleshooting insufficient permissions error when creating a document in Sanity using a client and token.

21 replies
Last updated: Jul 6, 2022
Hi, I want to create a document but I get a error that says 'Client Error: Insufficient permissions; permission "create" required'. Anybody can help?
AI Update

This error means you don't have the necessary permissions to create documents in your Sanity project. Here's how to fix it:

Check your role and permissions:

The most common cause is that your user account or API token doesn't have the "create" permission. In Sanity, permissions are controlled through roles and access control.

If you're working in Sanity Studio:

  1. Go to your project's Manage page (access it from the Studio menu or run npx sanity@latest manage)
  2. Navigate to the Members section
  3. Check what role you're assigned - you need at least Editor or Administrator role to create documents
  4. If you're not an admin, ask a project administrator to grant you the appropriate permissions

If you're using an API token:

When making programmatic requests to Sanity's API, you need an API token with sufficient permissions:

  1. Go to ManageAPI tab
  2. Check your existing tokens or create a new one
  3. Make sure the token is assigned a role with "create" permissions (typically Editor or a custom role with create permissions)
  4. If you created a new token, update it in your environment variables:
    SANITY_API_TOKEN="your-new-token-here"

Custom roles:

If your project uses custom roles (available on Enterprise plans), the role assigned to you or your token might have restricted permissions. An administrator needs to update the role definition to include create permissions for the specific document types or content resources you need to work with.

Quick test:

If you're an administrator and need to quickly verify this is a permissions issue, try temporarily assigning yourself or your token the Administrator role, which has full permissions. If document creation works, then you know it's a role/permission configuration issue that needs to be adjusted.

Remember that roles in Sanity are additive - if you have multiple roles, you get the combined permissions of all of them.

Are you trying to create a document via the studio or with a script or something?
I use react
That's my code: const append = async () => { await sanityClient.patch(users._id).append('userArray', user).commit()
}

append()
That's my code:
const append = async () => {

await sanityClient.patch(users._id).append('userArray', user).commit()

}


append()
Okay, with the client. So you need to have a token on your Sanity client to write data.
Yes, the token has read and write permissions
How is your client configured? Are you sure that it has access to your token?
My config: export const config = { dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
apiVersion: '2021-03-25',
useCdn: process.env.NODE_ENV === 'production'
}
My config:
export const config = {

dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',

projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,

apiVersion: '2021-03-25',

useCdn: process.env.NODE_ENV === 'production'

}
Client code:
import sanityClient from '@sanity/client'

import { config } from './sanity'


export default sanityClient(config)
I save the token in my env file under SANITY_API_TOKEN
I'm guessing you're not picking up on your env vars.
Where should I put it then?
You can pick things up from an env, but they need to have the correct naming. If you have it as
SANITY_API_TOKEN
your frontend won't be able to pick it up with your current configuration because it's looking for
NEXT_PUBLIC_SANITY_PROJECT_ID
.
I have NEXT_PUBLIC_SANITY_PROJECT_ID in my env file assigned with the project id
Ah, I mistyped. You don't have that token specified in your client.
How can I do that?
If it's in your env as
SANITY_API_TOKEN
, then in your client add:
token: process.env.SANITY_API_TOKEN
I added the token to the config but its still not working
Did you recompile your frontend after adding it?
I needed to add NEXT_PUBLIC_ because I call it from client side
So now it worked thanks
Nice. Glad it's working.

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.

Was this answer helpful?