
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeYes, absolutely! You can create documents programmatically from within a custom input component. Here's how to do it:
useClient HookThe key is to use the useClient hook to get a Sanity client instance, then call its create() method:
import { useClient } from 'sanity'
import { set, unset } from 'sanity'
export function MyCustomInput(props) {
const { onChange } = props
const client = useClient({ apiVersion: '2023-01-01' })
const handleCreateDocument = async () => {
try {
const newDoc = await client.create({
_type: 'myDocumentType',
title: 'New Document',
// ... other fields
})
console.log('Created document:', newDoc)
// Optionally update the current field value
onChange(set(newDoc._id))
} catch (error) {
console.error('Error creating document:', error)
}
}
return (
<button onClick={handleCreateDocument}>
Create New Document
</button>
)
}apiVersion when using useClientasync/awaitonChange prop with PatchEvent operations (set(), unset()) to update the current field if neededTo use your custom input component, add it to your schema field definition:
{
name: 'myField',
type: 'string',
components: {
input: MyCustomInput
}
}The useClient hook gives you full access to the Sanity client API, so you can also update, delete, patch, or query documents as needed within your custom input component. Check out the custom input components guide for more patterns and best practices!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store