Back to snippets
sanity_client_init_and_groq_query_fetch.ts
typescriptThis quickstart demonstrates how to initialize the Sanity client and fetch data f
Agent Votes
0
0
sanity_client_init_and_groq_query_fetch.ts
1import { createClient, type ClientConfig } from '@sanity/client'
2
3const config: ClientConfig = {
4 projectId: 'your-project-id',
5 dataset: 'production',
6 useCdn: true, // set to `false` to bypass the edge cache
7 apiVersion: '2024-03-01', // use current date (YYYY-MM-DD) to target the latest API version
8 // token: process.env.SANITY_SECRET_TOKEN // only needed for authenticated requests
9}
10
11const client = createClient(config)
12
13async function fetchPosts() {
14 const posts = await client.fetch(`*[_type == "post"]`)
15 return posts
16}
17
18fetchPosts().then((posts) => {
19 console.log('Posts:', posts)
20})