
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeGreat question! The short answer is: no, you won't lose your credentials by passing SvelteKit's fetch to the Sanity client, but there's a better approach that's recommended.
When you do sanity.fetch = fetch or use SvelteKit's fetch directly with the Sanity client, you're actually enhancing the functionality rather than breaking it. SvelteKit's special fetch implementation (available in load functions) provides benefits like:
Your Sanity credentials (API tokens, project ID, dataset) are typically passed through headers or configuration options, not embedded in the fetch function itself. So swapping the fetch implementation won't strip those credentials away.
However, based on Sanity's documentation, the better approach is to create a SvelteKit endpoint that wraps your Sanity client calls. Here's why:
+page.server.js automatically provide data to their corresponding +page.svelteInstead of using the Sanity client directly in your load function:
// +page.server.js
import { client } from '$lib/sanity'
export async function load({ fetch }) {
// This works but exposes credentials if you're using tokens
const data = await client.fetch(query)
return { data }
}Create an endpoint and use SvelteKit's fetch:
// +page.server.js
export async function load({ fetch }) {
const response = await fetch('/api/content')
const data = await response.json()
return { data }
}
// routes/api/content/+server.js
import { client } from '$lib/sanity'
import { json } from '@sveltejs/kit'
export async function GET() {
const data = await client.fetch(query)
return json(data)
}With newer SvelteKit versions, if your endpoint has the same filename as your page (minus the extension), the page automatically gets props from the endpoint without you needing to explicitly fetch.
If you're only querying public data (no read tokens, using the public API), then directly passing SvelteKit's fetch to your Sanity client is perfectly safe:
import { createClient } from '@sanity/client'
export async function load({ fetch }) {
const client = createClient({
projectId: 'your-project',
dataset: 'production',
apiVersion: '2023-05-03',
useCdn: true,
fetch // SvelteKit's fetch
})
const data = await client.fetch(query)
return { data }
}Your credentials (project ID, dataset) are still intact—you're just giving the client a different fetch implementation to use under the hood.
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