Revolutionising visual content creation with Sanity.io and OpenAI

Revolutionising visual content creation with Sanity.io and OpenAI

How to add AI image generation to Sanity Studio using OpenAI's API, from the original DALL-E plugin to the newer options available today.


We originally wrote this in January 2023, when DALL-E 2 was the only game in town and sticking it inside Sanity Studio felt like a neat trick. The landscape has shifted quite a bit since then, so we've updated this with what actually matters now.

The core idea hasn't changed: let editors generate images without leaving the CMS. The tools have.

The original approach: sanity-plugin-asset-source-openai

We built sanity-plugin-asset-source-openai as a quick way to wire DALL-E into Sanity Studio. It adds an "OpenAI" option to any image field, lets you type a prompt, and uploads the result as a Sanity asset.

Installation

pnpm add sanity-plugin-asset-source-openai

Config

import { defineConfig } from 'sanity'
import { openaiImageAsset } from 'sanity-plugin-asset-source-openai'

export default defineConfig({
  // ...
  plugins: [
    openaiImageAsset({
      API_KEY: process.env.SANITY_STUDIO_OPENAI_API_KEY,
    }),
  ],
})

Store your API key in an environment variable prefixed with SANITY_STUDIO_. Add it to .env.local and keep that file in .gitignore.

Schema

Any standard image field works. Nothing special needed:

export default {
  name: 'post',
  type: 'document',
  title: 'Post',
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Title',
    },
    {
      name: 'image',
      type: 'image',
      title: 'Image',
    },
  ],
}

Using it

Click the image field, select "Open AI Image" from the asset source dropdown:

ai-plugin

Type your prompt and hit Generate:

prompt-dialog

If you like the result, click Confirm to save it as a Sanity asset. If not, generate again until you get something usable.

ai-image

Here's a demo of the full flow:

https://www.loom.com/share/267fed477cf4458394eb2c2e4c964efc

No spam, only good stuff

Subscribe, for more hot takes

Only god knows why anybody would purposefully subscribe themselves to a newsletter that moans about development. These poor souls did though
Profile 1
Profile 2
Profile 3
Profile 4
Profile 5

What's changed since 2023

Quite a lot, actually.

DALL-E 2 and 3 are being retired

OpenAI is shutting down both DALL-E 2 and DALL-E 3 on May 12, 2026. The replacements are gpt-image-1 (released April 2025) and gpt-image-1.5 (December 2025), which are built on the GPT-4o architecture. They're better at following instructions, rendering text in images, and producing photorealistic output. Pricing moved to a per-token model, starting around $0.02 per image at low quality.

If you're using the plugin above, it will need updating before the deprecation deadline.

Sanity has native AI image generation now

This is the bigger story. Since Sanity 3.26, the official @sanity/assist plugin includes built-in image generation. You configure options.aiAssist.imageInstructionField on an image field and your editors get prompt-based generation without any third-party plugin.

Agent Actions takes it further, generating images as part of whole-document creation. If you're on Sanity's Growth plan or above, this is probably where you should start now rather than wiring up your own OpenAI key.

Other plugins worth knowing about

  • sanity-plugin-gemini-ai-images uses Google's Gemini 2.5 Flash for text-to-image and image-to-image editing, including background swaps and lighting changes.
  • sanity-plugin-image-gen (also ours) routes through Next.js API routes and Replicate, which gives you access to models like Flux and Stable Diffusion.

Which approach should you use?

If you're starting fresh today, use Sanity AI Assist. It's first-party, maintained, and doesn't require managing your own API keys.

If you need a specific model (say you prefer Flux's aesthetic over what Sanity's built-in option produces), one of the third-party plugins gives you that control.

The sanity-plugin-asset-source-openai approach from this original post still works, but you'll want to make sure it's updated for gpt-image-1 before May 2026. We're evaluating whether to update it or deprecate it in favour of our Replicate-based plugin, which is more model-agnostic.

The point remains the same as when we first wrote this: editors shouldn't have to leave their CMS to get an image. How you wire that up is just an implementation detail.

About the Authors

Hrithik Prasad
Hrithik Prasad

Senior Full-stack Developer

Senior Full-stack Developer with expertise in React, Next.js, and Sanity CMS. Loves building performant web applications and sharing knowledge through technical content.

Sameer Singh
Sameer Singh

Design Engineer

Design Engineer bridging the gap between design and code. Turns pixel-perfect concepts into polished, interactive experiences with a keen eye for detail and motion.

Sne Tripathi
Sne Tripathi

Account Executive

Account Executive at Roboto Studio, bridging the gap between client needs and technical solutions. Ensures every project delivers real business value.

Get in touch

Book a meeting with us to discuss how we can help or fill out a form to get in touch