Unlock seamless workflows and faster delivery with our latest releases - Join the deep dive

How to get a configured client in a Sanity custom tool.

2 replies
Last updated: Sep 25, 2023
Hi there, I've looked already at many threads mentioning custom tools, but I still failed to understand how to get a sanity client instance. I created a function that returns a tool definition:
import { UnpublishIcon } from '@sanity/icons'
import { Card, Text } from '@sanity/ui'

const xlsxImport = () => {

    return {
        title: 'XLSX Importer',
        name: 'xlsx-import', // localhost:3333/my-custom-tool
        icon: UnpublishIcon,
        component: (props: any, other:any) => {
            console.log(props, other)
            return <Card padding={4} >
                <Text>Import XLSX</ Text >
            </Card>
        },
    }
}

export default xlsxImport
and added the tool in my
sanity.config.ts
:
export default defineConfig({
  basePath: '/studio',
  projectId,
  dataset,
  schema,
  plugins: [
    deskTool({
      structure: myStructure
    }),
    visionTool({defaultApiVersion: apiVersion}),
  ],
  tools: [
    xlsxImport()
  ]
})
I can see my tool in sanity studio, but how do I get a configured client in my tool so I can make changes? I read
https://www.sanity.io/docs/tool-api-reference#91832d1f6f69 (Tool Context Properties) and there is a mention of a "getClient" function, but I don't know where to call that. Can anyone help me out?
Sep 25, 2023, 12:25 PM
omg, that's so simple. I guess that should be in the Tool documentation.Thanks a lot
user P
!
Sep 25, 2023, 1:38 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?