Using Sanity data in Next.js API routes for Snipcart integration

7 replies
Last updated: Nov 4, 2021
Hello, I am using Nextjs wiht Sanity and I want to have Sanity data available to be inported in pages/api/posts:api.js
export async function fetchSanity() {
const res = sanityClient.fetch(

*[_type == "post"] {
    	title,
   	 }
);
// .then((res) => (res.ok ? res : Promise.reject(res)))
const data = res;
return data;
}
When I call this function fetchSanity() in pages/api/posts, I get:
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Array(1)
0: title: "My first post"
I can see the data as [PromiseResult], but I don't know what to do to get the data?
Oct 28, 2021, 11:20 AM
Hi Pieter, sanityClient.fetch returns a promise, you can unwrap it by calling
await
:

- const res = sanityClient.fetch(...)
+ const res = await sanityClient.fetch(...)
Oct 28, 2021, 4:15 PM
Won't work. With await I get the same behavoir. Maybe I can put the question some other way: I am using Snipcart as Webshop integration. Now Snipcart needs an API in pages/api/products from static defined products.json. But I would like to use the product data from Sanity instead. How can I do that?
Oct 28, 2021, 7:42 PM
Could you share how you are calling the function
fetchSanity
? You’d need to await that function as well
Oct 31, 2021, 2:01 PM
Hello Derek, nice to reply! I was trying to export fetchSanity() and import the result as data in the serverless function at /pages/api/products. This function is consumed by the Snipcart Shop in order to safeguard the payment process. But obviously it didn't work! Is there a different way to import the products in /pages/api/products?
/utils/api.js
import sanityClient from '../client';
export async function fetchSanity() {
const res = await sanityClient.fetch(

*[_type == "post"] {
    title,
   }
);
const data = res;
return data;
}

/pages/api/products/index.ts
import {NextApiRequest, NextApiResponse} from "next";
import {products} from "../../../components/Products"; // hardcoded here!
export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json(products);
}
Nov 1, 2021, 5:48 PM
Hey Pieter, your
sanityClient
functions look fine to me — you’d use it in your handler like this:

export default async function handler(req, res) {
  const products = await sanityFetch()
  // handle error somewhere
  res.status(200).json(products)
}
Note that your handler function has to be async & sanityFetch has to be awaited
Nov 2, 2021, 6:15 AM
Hey Pieter, your
sanityClient
functions look fine to me — you’d use it in your handler like this:

export default async function handler(req, res) {
  const products = await sanityFetch()
  // handle error somewhere
  res.status(200).json(products)
}
Note that your handler function has to be async & sanityFetch has to be awaited
Nov 2, 2021, 6:15 AM
Thank you so much! I forgot to make the handler async, may bad. Now it works fine! Cheers, Pieter
Nov 4, 2021, 10:08 AM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in