How to create a Sanity dashboard widget with GROQ query and display data?

10 replies
Last updated: Jan 27, 2026
Hi there, I'm looking to build a dashboard widget to display some content within the sanity studio of a website I'm building, rather than the front end.I'm looking for some boilerplate code where I can run a groq query and then output some data. A simple example could be fetch all of the titles of all of the posts and display them inside of an unordered list within the dashboard widget. Is anyone able to point me in the right direction?
AI Update

Great question! You're looking to build a custom dashboard widget in Sanity Studio. Here's how to get started:

Using the Example Dashboard Widget

The best approach is to use the example-dashboard-widget-document-count as your boilerplate. This is an official Sanity example that shows exactly how to run GROQ queries and display data in a dashboard widget.

Basic Setup

First, you'll need to install the dashboard plugin:

npm install @sanity/dashboard

Then configure it in your sanity.config.ts (or sanity.config.js):

import {defineConfig} from 'sanity'
import {dashboardTool} from '@sanity/dashboard'

export default defineConfig({
  // ... other config
  plugins: [
    dashboardTool({
      widgets: [
        // Your custom widgets go here
      ]
    })
  ]
})

Creating Your Custom Widget

For your use case (listing all post titles), you'd create a custom widget component. Here's a simple example structure:

import React, {useEffect, useState} from 'react'
import {Card, Stack, Text} from '@sanity/ui'
import {useClient} from 'sanity'

export function PostListWidget() {
  const client = useClient({apiVersion: '2023-01-01'})
  const [posts, setPosts] = useState([])

  useEffect(() => {
    // Your GROQ query - fetch all post titles
    client.fetch('*[_type == "post"]{title}')
      .then(data => setPosts(data))
  }, [client])

  return (
    <Card padding={4}>
      <Stack space={3}>
        <Text size={2} weight="bold">All Posts</Text>
        <ul>
          {posts.map((post, index) => (
            <li key={index}>{post.title}</li>
          ))}
        </ul>
      </Stack>
    </Card>
  )
}

The key parts here are:

  • useClient hook: This gives you access to the Sanity client to run GROQ queries within Studio
  • GROQ query: Replace *[_type == "post"]{title} with whatever query fits your needs
  • Sanity UI: Use these components to style your widget consistently with Studio

Important Resources

The example widget uses simple GROQ methods like count(*), but you can use any GROQ query you need for more complex data fetching and transformations.

Show original thread
10 replies

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?