SDKs overview
Sanity, a developer-first content platform, offers Software Development Kits (SDKs) and client libraries designed to streamline interactions with its Content Lake and Studio. These tools abstract the underlying API calls, providing idiomatic interfaces for various programming languages. The SDKs enable developers to perform common operations such as querying content using GROQ (Graph-Oriented Query Language) or GraphQL, mutating documents, and subscribing to real-time updates from the Content Lake. This approach minimizes boilerplate code and allows developers to focus on application logic rather than direct HTTP requests and response parsing.
Sanity's SDK ecosystem supports a range of popular programming environments, with a strong emphasis on JavaScript and TypeScript due to the Sanity Studio's React foundation. Beyond the official offerings, a community has contributed libraries for additional languages, extending Sanity's reach. These libraries typically wrap the core HTTP API endpoints for content delivery and management, providing a consistent developer experience across different technology stacks.
Official SDKs by language
Sanity maintains official SDKs primarily for JavaScript/TypeScript, which are foundational for interacting with the Sanity Content Lake and building applications that consume Sanity-managed content. These official libraries are actively developed and supported, ensuring compatibility with the latest platform features and best practices for performance and security. The JavaScript client, @sanity/client, is the core library for most web-based and Node.js applications, offering comprehensive functionality for CRUD operations and real-time listening.
The following table outlines the key official SDKs and their primary uses:
| Language | Package/Library | Description |
|---|---|---|
| JavaScript/TypeScript | @sanity/client |
Core client for fetching and mutating documents, real-time subscriptions. |
| JavaScript/TypeScript | next-sanity |
Utilities for integrating Sanity with Next.js applications, including server-side rendering and static site generation. |
| JavaScript/TypeScript | @sanity/image-url |
Helper library for generating optimized image URLs from Sanity image assets. |
| JavaScript/TypeScript | @sanity/ui |
React component library used to build custom Sanity Studio interfaces and plugins. |
For detailed documentation on each official library, refer to the Sanity API clients documentation, which provides comprehensive guides and API references for effective integration.
Installation
Installation of Sanity's official SDKs typically involves using a package manager like npm or Yarn for JavaScript/TypeScript projects. For other languages, community libraries often use their respective package managers. The primary client, @sanity/client, is installed as a dependency in your project.
JavaScript/TypeScript
To install the core Sanity client in a JavaScript or TypeScript project, use npm or Yarn:
npm install @sanity/client
# or
yarn add @sanity/client
Once installed, you can configure the client with your project details, such as the project ID and dataset name, which are found in your Sanity project settings. These credentials are essential for the client to authenticate and communicate with your specific Sanity Content Lake instance. For example, a basic client setup might include specifying your API version for consistent interaction.
Other languages (Community)
For community-maintained libraries in other languages, installation methods vary. Developers typically use language-specific package managers:
- Python: Often involves
pip install sanity-ioor similar, depending on the specific library's name. - PHP: Composer is the standard for PHP packages, e.g.,
composer require sanity/sanity-client-php. - Ruby: Gemfile entries for Bundler, e.g.,
gem 'sanity-ruby'. - Go: Go modules for Go projects, e.g.,
go get github.com/sanity-io/sanity-go(example, actual path may vary).
Always consult the specific library's documentation for precise installation instructions and configuration details, as these can differ based on the maintainer and the library's scope.
Quickstart example
This quickstart demonstrates how to initialize the Sanity client and fetch documents using GROQ in a JavaScript environment. This example assumes you have a Sanity project set up with some content and your project ID and dataset are available.
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' or 'development')
apiVersion: '2023-05-03', // Use a consistent API version
useCdn: true, // Set to 'false' if you want to ensure fresh data (disables CDN)
})
async function getPosts() {
// GROQ query to fetch all documents of type 'post' with title and slug
const query = `*[_type == "post"]{
_id,
title,
"slug": slug.current
}`;
try {
const posts = await client.fetch(query);
console.log('Fetched posts:', posts);
} catch (error) {
console.error('Error fetching posts:', error);
}
}
getPosts();
This script first initializes the Sanity client with necessary credentials. It then defines an asynchronous function getPosts that uses a GROQ query to retrieve all documents of type post, selecting their ID, title, and current slug. The results are logged to the console. For more complex queries, including filtering, ordering, and projections, refer to the official GROQ documentation.
Community libraries
While Sanity provides robust official SDKs for JavaScript and TypeScript, the community has developed and maintains client libraries for other programming languages. These libraries enable developers working in different ecosystems to easily integrate with Sanity's Content Lake. Community contributions extend Sanity's reach and provide greater flexibility for diverse development stacks.
Some notable community-supported libraries include:
- Python: Libraries such as
sanity-io-py(or similar packages available on PyPI) allow Python applications to query and manage Sanity content. These often mirror the functionality of the JavaScript client, providing methods for fetching documents, handling assets, and performing mutations. The Python ecosystem is widely used for data processing and backend services, making a Python client valuable for integrations. - PHP: For PHP-based applications, client libraries facilitate interaction with Sanity, enabling content retrieval for frameworks like Laravel or Symfony. These usually abstract the HTTP communication and provide an object-oriented interface for content operations. The PHP community leverages Composer for package management, simplifying the inclusion of such libraries.
- Ruby: Ruby clients for Sanity can be found as gems, allowing Ruby on Rails or other Ruby applications to consume content from Sanity. These libraries typically integrate with Ruby's conventions, making them feel natural for Ruby developers.
- Go: Go language clients for Sanity provide a performant way to interact with the Content Lake, suitable for microservices and high-concurrency applications. Go modules are used for dependency management, ensuring straightforward integration.
When using community libraries, it is important to review their documentation, community support, and maintenance status. While they offer flexibility, they may not always have the same level of support or feature parity as official SDKs. Resources like Sanity's GitHub organization and community forums are good starting points for discovering and evaluating these libraries. Developers can also find examples of integrating Sanity with various technologies, including JavaScript modules and server-side frameworks, by exploring community projects and discussions.