How I used Agent API to generate photos for my family’s recipes
Written by Jarod Reyes
Hi folks, Jarod here. I recently joined Sanity as Head of Developer Experience and Community and as part of my onboarding I wanted to play around with some of the newer AI features released inside of Sanity. This post came from that exploration.
I’ve been on a meal plan since earlier this winter, created by my nutritional coach. It’s been great, I’ve lost 25 lbs, but it’s also been complex because I’m the one who cooks and it can be hard to shape a strict meal plan for a family. I wanted an app that could take the PDFs my coach sends, turn them into structured content in Sanity, and support workflows like scaled ingredients for my wife and kids, calculated weights, and search (we use Sanity + Algolia for that).
One thing was missing: pictures. When my family picked dinner or the kids chose a weekend breakfast, there was nothing to look at. So when I learned that Sanity shipped an Agent API action for generating images, one that reads your schema and uses your actual content to drive the prompt, I decided I needed to give it a spin. It did a phenomenal job. Here’s how to do it on an existing schema, with code and a couple of gotchas.
Prerequisites
- A Sanity project (which you can spin up here) with a document type that has an image (or image array) field.
Note on cost: Each generate call uses about 2 AI credits. Every Sanity org gets 100 free credits per month, so a batch of 50 recipes will use most of your free allotment. Check usage in Manage → Settings.
Generating an image with Sanity Agent API in Typescript
Here's the complete script, ready to copy and run. It uses getCliClient(), which pulls your project config from sanity.cli.ts and your local authenticated session so there's no manual token management or hardcoded project IDs. To run it:
A few things worth knowing before you run it:
- It targets drafts only. The Agent API writes to the draft version of a document. You'll need to publish after (more on that below).
- It uses
async: true, which means the script queues the generation and returns immediately — it doesn't wait for the image to be ready. Check Studio after a minute or two to see results. - It's safe to re-run. The GROQ filter
images == nullmeans it only processes recipes without images, so running it again won't duplicate or overwrite anything.