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?
Jul 4, 2022, 5:35 PM
Are you trying to create a document via the studio or with a script or something?
Jul 5, 2022, 8:08 AM
I use react
Jul 5, 2022, 10:43 AM
That's my code: const append = async () => { await sanityClient.patch(users._id).append('userArray', user).commit()
}

append()
Jul 5, 2022, 11:15 AM
That's my code:
const append = async () => {

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

}


append()
Jul 5, 2022, 11:15 AM
Okay, with the client. So you need to have a token on your Sanity client to write data.
Jul 5, 2022, 11:41 AM
Yes, the token has read and write permissions
Jul 5, 2022, 11:47 AM
How is your client configured? Are you sure that it has access to your token?
Jul 6, 2022, 4:18 PM
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'
}
Jul 6, 2022, 6:43 PM
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
Jul 6, 2022, 6:45 PM
I'm guessing you're not picking up on your env vars.
Jul 6, 2022, 6:46 PM
Where should I put it then?
Jul 6, 2022, 6:48 PM
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
.
Jul 6, 2022, 7:04 PM
I have NEXT_PUBLIC_SANITY_PROJECT_ID in my env file assigned with the project id
Jul 6, 2022, 7:11 PM
Ah, I mistyped. You don't have that token specified in your client.
Jul 6, 2022, 7:14 PM
How can I do that?
Jul 6, 2022, 7:18 PM
If it's in your env as
SANITY_API_TOKEN
, then in your client add:
token: process.env.SANITY_API_TOKEN
Jul 6, 2022, 7:20 PM
I added the token to the config but its still not working
Jul 6, 2022, 7:25 PM
Did you recompile your frontend after adding it?
Jul 6, 2022, 7:26 PM
I needed to add NEXT_PUBLIC_ because I call it from client side
Jul 6, 2022, 7:28 PM
So now it worked thanks
Jul 6, 2022, 7:29 PM
Nice. Glad it's working.
Jul 6, 2022, 7:29 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?