How to Integrate ImageKit with Your Headless CMS
Connect ImageKit to your headless CMS so every published product, article, or landing page image can be transformed, tagged, delivered through a CDN, and reused across web, mobile, and apps.
What is ImageKit?
ImageKit is a DAM and media delivery platform for images and videos. Teams use it for uploads, URL-based transformations, format conversion, responsive delivery, CDN-backed assets, media search, metadata, and asset organization. It’s commonly used by ecommerce, publishing, marketplace, and SaaS teams that need fast media delivery without hand-editing every image size.
Why integrate ImageKit with a headless CMS?
Media breaks down fast when content and assets live in separate systems. A product editor updates a hero image in one place, a developer copies the asset URL into another, and the frontend still needs separate logic for resizing, WebP or AVIF delivery, alt text, and cache behavior. Multiply that by 4 channels, 20 locales, and hundreds of SKUs, and you get stale images, missing metadata, and slow pages.
Architecture overview
A typical Sanity and ImageKit integration starts when an editor publishes content in Sanity Studio. The document and its image references are written to the Content Lake. A GROQ-powered webhook listens for the document types you care about, such as product, article, landingPage, or campaign. The webhook sends the changed document ID to a Sanity Function or your own API route. That server-side code uses @sanity/client to fetch the full document with a GROQ query, including joined image asset URLs and referenced fields like category names or campaign slugs. Then it calls ImageKit’s Upload API through the ImageKit SDK, passing the Sanity image URL as the file, plus a fileName, folder, tags, and customMetadata. ImageKit ingests the remote image, stores it in its media library, applies delivery settings, and returns a CDN URL. Your frontend can then render ImageKit URLs directly, or you can write the returned ImageKit fileId and URL back to Sanity if editors need to see the delivery asset in Sanity Studio. For end users, the final page loads media from ImageKit’s CDN with URL parameters for width, crop, quality, and format, while structured content still comes from Sanity.
Common use cases
Product image delivery
Sync product images, SKU metadata, category tags, and alt text from Sanity to ImageKit so PDPs, PLPs, and mobile apps use the same approved assets.
Editorial thumbnails at scale
Generate article cards, social preview images, author portraits, and hero crops from one source image using ImageKit URL transformations.
Localized campaign media
Push locale, market, and campaign metadata from Sanity into ImageKit folders and tags so regional teams can find the right assets quickly.
Responsive media for frontend teams
Serve different widths, formats, and crops from ImageKit while Sanity keeps the structured content, references, and publish workflow.
Step-by-step integration
- 1
Create your ImageKit project
Sign in to ImageKit, create or select a media library, and copy your public key, private key, and URL endpoint from the developer settings. Decide your folder pattern early, such as /sanity/products, /sanity/articles, or /sanity/campaigns.
- 2
Install the SDKs
Install ImageKit’s Node.js SDK and the Sanity client in the server-side environment that will receive webhooks: npm install imagekit @sanity/client.
- 3
Model media context in Sanity Studio
Add image fields with alt text, caption, usage rights, focal point notes, and any routing fields your frontend needs. For products, include fields like sku, slug, category references, and mainImage. For articles, include title, slug, heroImage, author, and topic references.
- 4
Create a publish trigger
Set up a Sanity webhook filtered to the document types you want to sync, or use a Sanity Function if you want the sync logic to run inside Sanity’s event system. Send the document ID and mutation type when content is published, updated, or deleted.
- 5
Call ImageKit from server-side code
Use @sanity/client to fetch the document with GROQ, then call imagekit.upload with the Sanity asset URL as the file. Pass folder, tags, and customMetadata so ImageKit search and organization reflect the content structure.
- 6
Test the frontend path
Publish one test document, confirm the asset appears in ImageKit, check tags and custom metadata, and render the returned ImageKit URL with transformation parameters like tr=w-800,f-auto. Test cache behavior, missing images, unpublished content, and deleted documents before rolling it out.
How Sanity + ImageKit works
Build your ImageKit integration on Sanity
Sanity gives you the structured content foundation, real-time event system, and flexible APIs to connect ImageKit to the publishing workflows your team already uses.
Start building free →CMS approaches to ImageKit
| Capability | Traditional CMS | Sanity |
|---|---|---|
| Media metadata for DAM sync | Image context is often mixed into page fields or HTML, so syncing alt text, SKU, campaign, and category data takes custom parsing. | Typed JSON in the Content Lake lets you send ImageKit clean metadata from image fields, product fields, and referenced documents. |
| Publish-time ImageKit updates | Teams often rely on manual uploads, scheduled exports, or plugin-specific behavior that can drift from editorial publishing. | Webhooks trigger on content events, and Functions can run the ImageKit sync logic server-side without extra infrastructure. |
| Field-level control for upload payloads | The integration may receive too much page data or too little media context, depending on the plugin model. | GROQ can fetch the exact fields ImageKit needs in one query, including joins across references. |
| Frontend image delivery | Pages often depend on built-in media URLs, which can limit how teams handle responsive crops, formats, and CDN parameters. | Structured content can reference ImageKit delivery URLs while Sanity’s Presentation Tool helps editors see content in context. |
| Multi-channel reuse | Media and content are frequently tied to web pages, so reuse in apps, kiosks, and AI agents takes extra mapping. | One structured back end can feed websites, mobile apps, ImageKit, and AI agents through APIs and Agent Context. |
Keep building
Explore related integrations to complete your content stack.
Sanity + Cloudinary
Connect structured content with Cloudinary for advanced media transformations, asset delivery, and image workflows.
Sanity + Imgix
Serve responsive images from structured Sanity content using Imgix URL parameters for sizing, cropping, and format changes.
Sanity + Mux
Pair Sanity content models with Mux video hosting, playback, thumbnails, and metadata for video-heavy experiences.