Troubleshooting insufficient permissions error when creating a document in Sanity using a client and token.
21 replies
Last updated: Jul 6, 2022
J
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
K
Are you trying to create a document via the studio or with a script or something?
Jul 5, 2022, 8:08 AM
J
I use react
Jul 5, 2022, 10:43 AM
J
That's my code: const append = async () => { await sanityClient.patch(users._id).append('userArray', user).commit()
}
append()
}
append()
Jul 5, 2022, 11:15 AM
J
That's my code:
const append = async () => {
await sanityClient.patch(users._id).append('userArray', user).commit()
}
append()
Jul 5, 2022, 11:15 AM
K
Okay, with the client. So you need to have a token on your Sanity client to write data.
Jul 5, 2022, 11:41 AM
J
Yes, the token has read and write permissions
Jul 5, 2022, 11:47 AM
J
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'
}
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
apiVersion: '2021-03-25',
useCdn: process.env.NODE_ENV === 'production'
}
Jul 6, 2022, 6:43 PM
J
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
J
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_TOKENyour 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
J
I have NEXT_PUBLIC_SANITY_PROJECT_ID in my env file assigned with the project id
Jul 6, 2022, 7:11 PM
J
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
J
I added the token to the config but its still not working
Jul 6, 2022, 7:25 PM
J
I needed to add NEXT_PUBLIC_ because I call it from client side
Jul 6, 2022, 7:28 PM
J
So now it worked thanks
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.