Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

Patching a document in Sanity client component without API key

6 replies
Last updated: Aug 14, 2024
Hey all, is there anyway to patch a document within a client component in sanity? I've looked on github and the docs but couldnt find anything, I guess I need to use sanity/migrate ? This seems to be more for cli use
Aug 14, 2024, 6:50 AM
I tried
sanity/migrate
and yeah this doesnt look like it will work. The only other idea I have is to use the HTTP API. Has anyone done mutations within the studio before?
Aug 14, 2024, 4:37 PM
The problem here is that the API key is needed for authentication, it would mean making the API key public on the client side which isnt ideal
Aug 14, 2024, 4:55 PM
ohhhh cool:
Aug 14, 2024, 5:01 PM
ohh cool:
await fetch(      `https://${process.env.NEXT_PUBLIC_SANITY_PROJECT_ID}.<http://api.sanity.io/v2021-06-07/data/mutate/${process.env.NEXT_PUBLIC_SANITY_DATASET}|api.sanity.io/v2021-06-07/data/mutate/${process.env.NEXT_PUBLIC_SANITY_DATASET}>`,

      {
        method: 'POST',
        mode: 'cors',
        credentials: 'include',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(titleMutation),
      },
    )
Aug 14, 2024, 5:01 PM
Dont need to send auth tokens šŸ¤˜
Aug 14, 2024, 5:01 PM
Awesome!
Aug 14, 2024, 5:03 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?