🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

How to generate slugs when using the Sanity HTTP API

3 replies
Last updated: Feb 19, 2021
Hello 🙂I am playing for the first time with the http-api. So far I only had to to queries and I have used groq queries including sanity in my nuxt project but now I am working on a form that would be used by authorized admins to submit new documents to my dataset (hope it makes sense so far hehe).
It looks like the way to go for this is using mutations within the http-api and I was acutally able to set up a simple test that would "write" a document, passing only a couple of strings and numbers (say title or price etc...).

I am sure I will struggle when I have to deal with more complex things with references hehe but I already have a question about something apparently simpler:

From sanity studio there is an easy way to generate slugs by just clicking on the "generate" button.
I was wondering if I work with a mutation like the following:

const myThingyObjectDataGatheredFromTheForm = {
                                                  title: "Hello World! How are you?",
                                                   price: 222
                                              }

const mutations = [
        {
          createOrReplace: {
            _type: 'mythingy',
            ...myThingyObjectDataGatheredFromTheForm,
          },
        },
      ]
Is there a way to tell sanity to auto generate slug when I post this?
Given that the slug is an object like


"slug": {
    "_type": "slug",
    "current": "hello-world-how-are-you"
  }
Do I need to create such object before I post the values or it can be somehow generated automatically?

Hope this makes sense.
Thank you in advance for any help :)
Feb 17, 2021, 11:10 PM
Hi User, great to see your progress using the HTTP API 🙂 Unfortunately, the slug generation function is part of the studio itself and only works there. It gives you a 'Generate' button that you can use to generate a slug from a source field (e.g.
title
).
To generate slugs when using the HTTP API, you will need to replicate the functionality of the slug field. Our own slug function uses 
speakingurl
 under the hood: https://github.com/pid/speakingurl . Here's the function itself as an example: https://github.com/sanity-io/sanity/blob/ebd72b17236d29f6dad636c17e3ddd1b301e0853/[…]ackages/%40sanity/form-builder/src/inputs/Slug/utils/slugify.ts
Hope that clarifies things?
🙂
Feb 19, 2021, 2:20 PM
Hello User,yes it does indeed! I actually wanted to find out what you guys used to slugify there so that I could apply the same so it helps a lot
🙂
I will keep working on it now with more complex fields and I hope I won't need to bother here too much but I must say heads up to this community here on slack! You guys are really helpful!
Feb 19, 2021, 3:18 PM
Thanks, that's awesome to hear! Do bother us all you need 🙂
Feb 19, 2021, 3:36 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?