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

2 repliesLast updated: Nov 29, 2025

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:

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:

Show original thread
2 replies

Was this answer helpful?

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.

Related contributions