
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeTo access a Sanity project in your React app when you have the project ID and API token, you'll need to install and configure the @sanity/client package. Here's how to set it up:
First, install the Sanity client:
npm install @sanity/clientCreate a Sanity client instance using createClient:
import { createClient } from "@sanity/client";
export const client = createClient({
projectId: "your-project-id",
dataset: "production", // or your dataset name
apiVersion: "2024-01-01", // use current date (YYYY-MM-DD)
useCdn: true, // set to false for fresh data
token: process.env.REACT_APP_SANITY_TOKEN, // only if you need authenticated access
});Be very careful with API tokens in browser-based React apps! According to Sanity's security guidelines, you should never expose write tokens in frontend code. Here's what you need to know:
If you need write access from your React app, implement a backend API endpoint or serverless function that:
Store your token in environment variables to keep it secure:
REACT_APP_SANITY_PROJECT_ID=your-project-id REACT_APP_SANITY_DATASET=production REACT_APP_SANITY_TOKEN=your-read-only-token
Then reference them in your client configuration:
export const client = createClient({
projectId: process.env.REACT_APP_SANITY_PROJECT_ID,
dataset: process.env.REACT_APP_SANITY_DATASET,
apiVersion: "2024-01-01",
useCdn: true,
token: process.env.REACT_APP_SANITY_TOKEN,
});Once configured, you can query content using GROQ:
import { client } from "./sanityClient";
// Fetch all posts
const posts = await client.fetch('*[_type == "post"]{_id, title, slug}');Authentication with a token is required for:
previewDrafts perspectiveFor publicly available published content, you can often omit the token entirely by setting useCdn: true and not including the token parameter.
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store