Hello, I am using Snipcart in a Nextjs 13 webshop, while using the Snipcart products api to check product availability. It has been working a while (six month) ago, but now it fails (500 error), while working in developer mode.
In the terminal I am getting the right data:
–
curl -H “Accept: application/json”
https://app.snipcart.com/api/products
-u {API_SECRET_KEY}:
This is the code in ‘getSnipcartproducts.js’ located in the ‘api’ folder of the ‘pages’ folder:
import axios from ‘axios’;
const nodeBtoa = (b) => Buffer.from(b).toString(‘base64’);
export const base64encode = typeof btoa !== ‘undefined’ ? btoa : nodeBtoa;
module.exports = async (req, res) => {
const secret = ${process.env.SNIPCART_API_SECRET_KEY}
;
const products = (
await axios.get(‘https://app.snipcart.com/api/products’, {
headers: {
Authorization: Basic ${nodeBtoa(secret)}
,
Accept: ‘application/json’
}
})
).data;
var snipitems = [];
products &&
products.items.forEach((element) => {
var feature = {
id: element.userDefinedId,
stock: element.stock || 0
};
snipitems.push(feature);
});
return res.json(snipitems);
};
This is how I get the ‘snipProducts’, but the result is “[ ]”
useEffect(() => {
fetch(/api/getSnipcartProducts
)
.then((res) => {
return res.json();
})
.then((data) => {
setSnipProducts(data);
});
}, []);
What is going wrong here?