Auto-generate slug in Sanity HTTP API mutation without manual creation

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 :)
AI Update

Great question! Unfortunately, the slug generation "Generate" button you see in Sanity Studio is a Studio-only feature and doesn't work automatically when creating documents via the HTTP API.

When you submit mutations through the HTTP API, you need to manually create the slug object yourself. The auto-generation functionality is part of the Studio interface, not something the API provides as an option.

Create Your Own Slugification Function

You'll need to implement slug generation in your Nuxt app before sending the mutation. Sanity Studio itself uses the speakingurl library internally, so you could use that or any other slugification library:

// Install: npm install speakingurl
import slugify from 'speakingurl'

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

// Generate the slug from the title
const slug = {
  _type: 'slug',
  current: slugify(myThingyObjectDataGatheredFromTheForm.title)
  // This will produce: "hello-world-how-are-you"
}

const mutations = [
  {
    createOrReplace: {
      _type: 'mythingy',
      ...myThingyObjectDataGatheredFromTheForm,
      slug: slug
    },
  },
]

Alternative Libraries

If you don't want to use speakingurl, there are other options:

  • slugify (npm package)
  • @sindresorhus/slugify
  • Or write your own simple function:
function createSlug(text) {
  return text
    .toLowerCase()
    .replace(/[^\w\s-]/g, '') // Remove special characters
    .replace(/\s+/g, '-')      // Replace spaces with hyphens
    .replace(/-+/g, '-')       // Replace multiple hyphens with single
    .trim()
}

Why It Works This Way

As explained in this answer about HTTP API slug generation, the slug generation logic lives in the Studio UI layer, not in the Content Lake API itself. This means any external system (like your Nuxt form) needs to handle slugification on its own.

The good news is that once you set up the slugification function, it's pretty straightforward to use! And you have full control over the slug generation logic, which can be helpful for custom requirements like handling special characters or creating custom URL patterns based on your specific needs.

Show original thread
3 replies
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?
🙂
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!
Thanks, that's awesome to hear! Do bother us all you need 🙂

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?