Getting started

Integration & migration

Image & video API

DAM user guide

API overview

Account

Sanity Integration with ImageKit.io

Seamlessly integrate ImageKit's powerful media management and optimization capabilities with your Sanity Studio workflows.

•

Sanity is a platform for structured content that powers remarkable digital experiences at scale. The ImageKit plugin for Sanity enables you to browse, manage, and deliver optimized media directly from your Sanity Studio, leveraging ImageKit's complete media storage, optimization, and transformation solution along with an image and video CDN.

This guide walks you through installing and configuring the ImageKit plugin in your Sanity Studio to enhance your workflows with powerful media management capabilities. You can view the source code on GitHub.

Plugin Features

  • Media Library Integration: Browse and manage your ImageKit media library directly in Sanity Studio without leaving your content editor.
  • Asset Source Integration: Integrates seamlessly with Sanity's image fields for a native editing experience.
  • Custom Schema Support: Provides imagekit.asset schema type for storing comprehensive asset metadata.
  • Rich Asset Data: Access complete asset metadata including dimensions, file size, custom metadata, and more.
  • Video Support: Browse and select both images and video content from your ImageKit media library.
  • Asset Preview: View thumbnails and detailed asset information before selection.
  • No Configuration Required: Works out of the box without additional configuration.

Installation

Before installing, ensure you have a Sanity Studio project (v3 or later) and a local instance of the same running. You can refer to Sanity's official documentation to understand the prerequisites for setting up your Sanity Studio instance.

To install the ImageKit plugin in your Sanity Studio instance, run one of the following commands from your project's root directory:

Using npm:

Copy
npm install sanity-plugin-imagekit

Using yarn:

Copy
yarn add sanity-plugin-imagekit

Using pnpm:

Copy
pnpm install sanity-plugin-imagekit

Usage

The package exports two plugins that serve different use cases:

  • imagekitAssetSourcePlugin - Use this when you want ImageKit to appear as an asset source for standard image fields in your Sanity Studio.
  • imagekitSchemaPlugin - Use this if you intend to define a custom schema type that stores comprehensive asset metadata from ImageKit, allowing you to access detailed information about each asset in your content.

ImageKit as an Asset Source

To use ImageKit as an asset source, import the plugin in your sanity.config.ts file:

Copy
// sanity.config.ts
import { defineConfig } from 'sanity'
import { imagekitAssetSourcePlugin } from 'sanity-plugin-imagekit'

export default defineConfig({
  projectId: 'your-project-id',
  dataset: 'production',
  // ... other configuration
  plugins: [
    imagekitAssetSourcePlugin(),
  ],
})

Once configured, ImageKit will automatically appear as an asset source option in the dropdown when you add or select images in your Sanity Studio. For most use cases, you can simply use the standard Sanity image field:

Copy
{
  type: "image",
  name: "heroImage",
  title: "Hero Image",
  // ImageKit will appear as an option in the asset source dropdown
}

ImageKit as a Custom Schema Type

For more control, you can use the custom imagekit.asset schema type.

To do this, first import the imagekitSchemaPlugin in your sanity.config.ts:

Copy
import { imagekitSchemaPlugin } from 'sanity-plugin-imagekit'

// Add to plugins array
export default defineConfig({
  // ... other configuration
  plugins: [
    imagekitSchemaPlugin(),
  ],
})

Then use it in your schema definitions:

Copy
{
  type: "imagekit.asset",
  name: "imagekitImage",
  title: "ImageKit Image",
  description: "Select an image from your ImageKit media library"
}

This custom type stores comprehensive asset metadata from ImageKit, allowing you to access detailed information about each asset in your content.

Asset Data Structure

When you select an asset from ImageKit, it's stored with comprehensive metadata. Here's what the asset object looks like:

Copy
{
  "_type": "imagekit.asset",
  "fileId": "unique_file_id",
  "name": "image_name.jpg",
  "url": "https://ik.imagekit.io/your_account/image.jpg",
  "width": 1920,
  "height": 1080,
  "fileType": "image",
  "size": 245760,
  "createdAt": "2024-01-01T00:00:00Z",
  "mime": "image/jpeg",
  // ... other fields
}

Note: Sanity Studio expects object keys to match the regex /^\$?[a-zA-Z0-9_-]+$/. Since ImageKit's Custom Metadata allows characters outside this set, the plugin automatically sanitizes keys by replacing non-supported characters with underscores (_).

Working with Asset Data

Using Asset URLs

The stored url field provides a direct link to your asset on the ImageKit CDN. You can use this URL directly in your frontend or even apply ImageKit's real-time transformations:

Copy
// Direct URL
const directUrl = asset.url;

// With transformation (example: resize to 300px width)
const transformedUrl = `${asset.url}?tr=w-300`;

Querying Assets in Your Frontend

When querying your Sanity documents, the asset data is readily available:

Copy
// Example GROQ query
const query = `*[_type == "article"][0] {
  heroImage {
    url,
    width,
    height
  }
}`;

// Result
{
  "heroImage": {
    "url": "https://ik.imagekit.io/your_account/image.jpg",
    "width": 1920,
    "height": 1080
  }
}

Custom Metadata

ImageKit's custom metadata feature allows you to store additional information with your assets. This data is accessible through the plugin:

Copy
{
  "customMetadata": {
    "alt": "Hero image for homepage",
    "photographer": "John Doe",
    "license": "CC0"
  }
}

All custom metadata fields are automatically included in the asset object and can be queried alongside standard asset properties.

Advanced Topics

Asset Transformation

ImageKit provides 50+ image and video transformations that can be applied in real-time via URL parameters on the asset url. Some common transformations include:

Copy
// Resize and crop
const resizedUrl = `${asset.url}?tr=w-400,h-300,cm-pad_resize`;

// Format conversion and quality
const optimizedUrl = `${asset.url}?tr=f-webp,q-80`;

// Multiple transformations
const transformedUrl = `${asset.url}?tr=w-400,h-300,c-maintain_ratio:e-grayscale`;

Learn more about available transformations here.

Using Both Plugins Together

You can use both plugins in the same Sanity Studio project. This allows you to use ImageKit as an asset source for standard image fields while also having access to the custom schema type for fields that require comprehensive metadata:

Copy
// sanity.config.ts
import { defineConfig } from 'sanity'
import { imagekitAssetSourcePlugin, imagekitSchemaPlugin } from 'sanity-plugin-imagekit'

export default defineConfig({
  projectId: 'your-project-id',
  dataset: 'production',
  plugins: [
    imagekitAssetSourcePlugin(),
    imagekitSchemaPlugin(),
  ],
})

This gives you the flexibility to choose the appropriate approach for each field in your content model.