SDKs overview
Sanity.io provides a set of Software Development Kits (SDKs) and libraries designed to facilitate interaction with its Content Lake, a real-time, API-first content backend. The primary focus for official SDKs is on JavaScript and TypeScript, reflecting their prevalence in modern web development frameworks. These SDKs abstract the underlying HTTP API calls, offering programmatic interfaces for content retrieval (fetching), modification (mutating), and real-time listening for changes. The content is queried using GROQ (Graph-Relational Object Queries), a declarative query language optimized for structured content [Sanity.io GROQ documentation].
Beyond the official offerings, the Sanity.io ecosystem includes community-contributed libraries that extend support to other programming languages, allowing a broader range of applications to integrate with the Sanity Content Lake. These libraries typically wrap the core API functionalities, providing language-idiomatic ways to perform operations such as fetching documents, assets, and schema definitions.
Developers typically use these SDKs to:
- Fetch content for display in frontends (e.g., websites, mobile apps).
- Mutate (create, update, delete) content programmatically.
- Listen for real-time changes to content, enabling live previews or collaborative features.
- Manage assets like images and files.
- Interact with the Sanity Studio configuration and data.
Official SDKs by language
Sanity.io primarily maintains official SDKs for JavaScript and TypeScript environments, which are foundational for building web applications and integrating with frameworks like React, Vue, and Next.js. These SDKs are designed to provide type safety, efficient caching, and robust error handling.
JavaScript / TypeScript
The core official SDK for JavaScript and TypeScript is @sanity/client, which provides methods for interacting with the Content Lake. It supports fetching data using GROQ queries, performing mutations, and subscribing to real-time updates. The client is configurable to connect to specific Sanity projects and datasets [Sanity.io JavaScript Client documentation].
Another key package is @sanity/image-url, which helps generate image URLs from Sanity image assets, handling transformations like resizing, cropping, and formatting. This is crucial for optimizing image delivery in web applications [Sanity.io Image URL documentation].
For authentication, Sanity.io utilizes OAuth 2.0 for third-party integrations and offers token-based authentication for API access. The SDKs facilitate the secure handling of these authentication mechanisms [OAuth 2.0 specification overview].
| Language | Package Name | Install Command | Maturity |
|---|---|---|---|
| JavaScript / TypeScript | @sanity/client |
npm install @sanity/client or yarn add @sanity/client |
Official, Stable |
| JavaScript / TypeScript | @sanity/image-url |
npm install @sanity/image-url or yarn add @sanity/image-url |
Official, Stable |
Installation
Installing the official Sanity.io SDKs typically involves using a package manager like npm or Yarn in a Node.js environment. For developers working with JavaScript or TypeScript projects, the process is straightforward.
Prerequisites
- Node.js (LTS version recommended)
- npm or Yarn package manager
- A Sanity.io project and dataset
Steps for @sanity/client
- Open your project terminal: Navigate to the root directory of your JavaScript/TypeScript project.
- Install the package: Execute one of the following commands:
npm install @sanity/client
or
yarn add @sanity/client
This command downloads and adds the @sanity/client package to your project's node_modules directory and updates your package.json file.
Steps for @sanity/image-url
If your project requires dynamic image URL generation, install the image URL builder:
npm install @sanity/image-url
or
yarn add @sanity/image-url
Quickstart example
This quickstart demonstrates how to initialize the Sanity client and fetch documents using a GROQ query in a JavaScript environment. This example assumes you have a Sanity project configured with public read access or have provided appropriate authentication tokens.
Step 1: Configure your Sanity client
Create a client instance by providing your Sanity project ID, dataset name, and API version. You can find these details in your Sanity project dashboard [Sanity.io client configuration details].
import { createClient } from '@sanity/client'
const client = createClient({
projectId: 'your-project-id', // Replace with your Sanity project ID
dataset: 'production', // Replace with your dataset name (e.g., 'production')
apiVersion: '2023-05-03', // Use a date for latest API version
useCdn: true, // Set to 'false' if you want to ensure fresh data
token: process.env.SANITY_API_TOKEN // Only if you need authenticated access, e.g., for mutations
})
Step 2: Fetch content with GROQ
Use the client instance to execute a GROQ query. This example fetches all documents of type post, selecting their _id, title, and slug.
async function getPosts() {
const query = `*[_type == "post"]{
_id,
title,
"slug": slug.current
}`;
try {
const posts = await client.fetch(query);
console.log('Fetched posts:', posts);
return posts;
} catch (error) {
console.error('Error fetching posts:', error);
}
}
getPosts();
Step 3: Generate image URLs (optional)
If you have image assets in your Sanity project, you can use @sanity/image-url to generate optimized URLs.
import imageUrlBuilder from '@sanity/image-url'
// Assuming you have an image asset object from a Sanity query
const sampleImage = {
_type: 'image',
asset: {
_ref: 'image-a1b2c3d4e5f6-1200x800-jpg', // Example asset reference
_type: 'reference'
}
};
const builder = imageUrlBuilder(client);
function urlFor(source) {
return builder.image(source)
}
const imageUrl = urlFor(sampleImage).width(800).height(600).url();
console.log('Generated image URL:', imageUrl);
Community libraries
While Sanity.io officially supports JavaScript and TypeScript SDKs, the developer community has created libraries and integrations for other popular programming languages. These community-driven efforts enable developers to interact with the Sanity Content Lake using their preferred language, often by wrapping the RESTful API endpoints or providing utility functions for GROQ queries and data handling.
These libraries vary in maturity, features, and maintenance. Developers should consult the respective project repositories for detailed documentation and support. Sanity.io's API is accessible via standard HTTP requests, allowing any language with an HTTP client to integrate directly, even without a dedicated SDK [Sanity.io HTTP API documentation].
Examples of languages with community-contributed libraries include:
- Python: Libraries exist for fetching and manipulating content, often leveraging Python's HTTP request libraries.
- PHP: Community packages provide interfaces for interacting with the Sanity API within PHP applications, including frameworks like Laravel.
- Ruby: Ruby gems are available to simplify content retrieval and management.
- Go: Developers have built Go clients for Sanity's API, enabling high-performance content delivery.
- C#: .NET developers can find community libraries to integrate Sanity content into their applications.
- Java: Community efforts provide Java clients for Android or other Java-based applications.
For the most up-to-date information and to discover community-maintained libraries, developers are encouraged to explore the Sanity.io community forums, GitHub repositories, and package registries specific to each language. The official Sanity.io documentation also often links to prominent community resources [Sanity.io community resources].
When using community libraries, it is important to review their documentation, recent activity, and community support to ensure they meet project requirements for stability and features. Direct API interaction using standard HTTP clients (e.g., Fetch API in JavaScript or requests in Python) remains an option for any language if a suitable community SDK is not available or does not meet specific needs.