Can I use the same Sanity data on multiple React projects?

3 replies
Last updated: Sep 15, 2023
Good day i have sanity set up for a react project of mine but i need the same sanity data on another react project do i need to setup sanity again on that project or i can make an api call to get the data
AI Update

Good news - you don't need to set up Sanity again! You can absolutely access the same Sanity data from multiple React projects. You have a couple of options:

Install the @sanity/client package in your second React project:

npm install @sanity/client

Then create a client with your existing project's credentials:

import { createClient } from '@sanity/client'

const client = createClient({
  projectId: 'your-project-id', // same as your first project
  dataset: 'production',         // same dataset
  apiVersion: '2024-01-01',
  useCdn: true,                  // use CDN for faster reads
})

// Query your data
const posts = await client.fetch('*[_type == "post"]')

You're just connecting to the same Content Lake from a different application - no need to duplicate anything!

Option 2: Direct HTTP API Calls

If you prefer not to install the Sanity client, you can make direct HTTP requests to the Query API:

const projectId = 'your-project-id'
const dataset = 'production'
const query = encodeURIComponent('*[_type == "post"]')

const url = `https://${projectId}.api.sanity.io/v2024-01-01/data/query/${dataset}?query=${query}`

const response = await fetch(url)
const data = await response.json()

Important: If your dataset is private, you'll need to include authentication. The Sanity client handles this automatically if you provide a token.

Which Option to Choose?

Use the Sanity client - it's easier, handles authentication automatically, provides better error handling, and includes helpful utilities. The direct HTTP approach works but requires more manual setup.

Both approaches access the exact same data from your Content Lake, so you can have as many frontend projects as you want all pulling from the same Sanity project!

Your project can be connected to as many presentation layers as you’d like. All you need to do is configure your Sanity client.
Thanks this was really helpful
Let us know if you need more help!

Sanity – Build the way you think, not the way your CMS thinks

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.

Was this answer helpful?