How to reference a Sanity account user as an "author" for a document.
Great question! Referencing Sanity account users as authors is a common need. Since Sanity account users aren't stored as documents in your Content Lake (they exist at the project/organization level), you can't create traditional document references to them directly. Here's the recommended approach:
Create Separate Author Documents (Recommended)
For most content-driven projects, creating a dedicated author document type gives you the most flexibility and is the standard approach:
defineType({
name: 'author',
type: 'document',
fields: [
defineField({name: 'name', type: 'string'}),
defineField({name: 'email', type: 'string'}),
defineField({name: 'userId', type: 'string', title: 'Sanity User ID'}),
defineField({name: 'bio', type: 'text'}),
defineField({name: 'photo', type: 'image'}),
defineField({name: 'socialLinks', type: 'array', of: [{type: 'url'}]})
]
})Then use a standard reference in your content documents:
defineField({
name: 'author',
type: 'reference',
to: [{type: 'author'}]
})Why this approach works best:
- Full control over author information and presentation
- Can add bios, photos, social links, and other content-specific metadata
- Easy to query with GROQ
- Authors can be managed through Studio like any other content
- Works seamlessly with Studio's reference UI
- Authors become part of your content model, which is useful for public-facing sites
Automatically Capture Current User Info
If you want to automatically populate author information when a document is created, you can use initial value templates. The currentUser object is available in the template context:
// In your sanity.config.ts
export default defineConfig({
// ... other config
document: {
newDocumentOptions: (prev, context) => {
return [
...prev,
{
id: 'article-with-author',
title: 'Article',
schemaType: 'article',
value: () => ({
authorName: context.currentUser?.name,
authorEmail: context.currentUser?.email,
authorId: context.currentUser?.id
})
}
]
}
}
})In your schema, define fields to store this information:
defineField({
name: 'authorId',
type: 'string',
title: 'Author ID',
readOnly: true
}),
defineField({
name: 'authorName',
type: 'string',
title: 'Author Name',
readOnly: true
})Pros: Automatic capture of who created the document, minimal setup
Cons: Values are snapshots at creation time - if a user changes their name later, existing documents won't update automatically; limited to basic user info (name, email, ID)
Hybrid Approach
You can combine both methods by storing the userId in your author documents to maintain a link to Sanity account users while still having rich author profiles:
defineType({
name: 'author',
type: 'document',
fields: [
defineField({
name: 'sanityUserId',
type: 'string',
title: 'Sanity User ID',
description: 'Links this author to their Sanity account'
}),
// ... other author fields
]
})This gives you the best of both worlds - rich author content plus a connection to actual Sanity users for permissions and workflows.
For most blogs, news sites, or content platforms where authors are displayed publicly, the separate author documents approach (first option) is definitely the way to go!
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.