How to add custom API routes/controllers to Sanity backend?

2 replies
Last updated: Aug 20, 2023
Good evening,I was wondering how to add "controllers/routes" to my sanity backend, or if it is possible. Here is a screenshot of what the directory currently looks like.
AI Update

Based on your question about adding "controllers/routes" to your Sanity backend, I need to clarify an important architectural point about Sanity:

Sanity Studio is a frontend application, not a traditional backend server. It's a React-based content management interface that runs in the browser and communicates with Sanity's hosted Content Lake via APIs. You don't add traditional backend routes or controllers to Studio itself.

However, you have several options depending on what you're trying to accomplish:

If you want custom UI routes in Studio

You can create custom Studio tools that add new top-level views with their own URL routes. These are React components that appear in Studio's navigation:

// sanity.config.ts
import {defineConfig} from 'sanity'

const myCustomTool = () => ({
  title: 'My Custom Tool',
  name: 'my-custom-tool', // This becomes the route: /my-custom-tool
  icon: DashboardIcon,
  component: (props) => <YourCustomComponent />
})

export default defineConfig({
  // ... other config
  tools: [myCustomTool()]
})

If you need backend API endpoints

For actual server-side logic and API endpoints, I'd strongly recommend using Sanity Functions - Sanity's modern serverless compute platform:

Benefits of Sanity Functions:

  • Native integration with your Sanity project
  • No external hosting required
  • Automatic scaling and deployment
  • Built-in event handlers for content changes
  • Better security with direct access to Sanity APIs

You define them in a sanity.blueprint.ts file:

import {defineBlueprint} from 'sanity/blueprint'

export default defineBlueprint({
  functions: [
    {
      name: 'my-api-endpoint',
      handler: async (req, context) => {
        // Your backend logic here
        return new Response(JSON.stringify({data: 'hello'}), {
          headers: {'Content-Type': 'application/json'}
        })
      }
    }
  ]
})

Functions are deployed with sanity functions deploy and become accessible at URLs like https://YOUR-PROJECT-ID.api.sanity.io/v2025-01-01/functions/my-api-endpoint.

Alternative: External API server

If Sanity Functions don't meet your needs, you can create a separate Node.js/Express server and use webhooks to connect it to Sanity, but Functions are the recommended modern approach for most use cases.

What are you trying to build? The best solution depends on your specific use case:

  • Custom Studio UI pages → Custom tools
  • API endpoints, event handlers, automation → Sanity Functions (recommended)
  • Integration with existing backend → Webhooks + external server
Show original thread
2 replies
First, sanity does not provide or use a backend service as server. You get a full frontend studio connected to your datasets. But you can use the desk customization or add tools in your configuration to match some routes of the studio with a custom component or needs.If you really want to use backend server and routes, you should consider a framework around the studio like nextjs
Oh! That explains a lot then. Thank you for the help!

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?