
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeFor OG images in Sanity, you should use the url field from the image asset, which returns the cdn.sanity.io URL. However, the best approach is to use the @sanity/image-url package to build your image URLs, as it automatically handles crop and hotspot settings while giving you control over transformations.
When querying for OG images, use this pattern:
*[_type == 'page']{
"ogImageUrl": ogImage.asset->url
}This gets you the base cdn.sanity.io URL. However, for better control and optimization, use the image URL builder:
import { createImageUrlBuilder } from '@sanity/image-url'
import { client } from './client'
const builder = createImageUrlBuilder(client)
// For OG images, you typically want specific dimensions
const ogImageUrl = builder
.image(ogImage)
.width(1200)
.height(630)
.url()publicUrlThe publicUrl field you're seeing is likely related to Media Library assets. For most use cases, stick with the standard url field and use the image URL builder for transformations.
?auto=format to serve optimal formatsexport function getOgImageUrl(image) {
return builder
.image(image)
.width(1200)
.height(630)
.auto('format') // Serve optimal format
.url()
}The key takeaway: while asset->url works, using @sanity/image-url gives you better control and respects editorial decisions about cropping and focal points. For OG images specifically, you'll want to ensure they're sized to 1200×630 pixels for optimal social media display.
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