Skip to content

Instantly share code, notes, and snippets.

@joshua-isaac

joshua-isaac/products.js

Last active Nov 13, 2020
Embed
What would you like to do?
Fetch products from Products Content List in Agility CMS with Agility Content Fetch SDK
import agility from "@agility/content-fetch";
// setup content fetch
const api = agility.getApi({
guid: process.env.AGILITY_GUID,
apiKey: process.env.AGILITY_API_FETCH_KEY,
});
// get products
const getProducts = async () => {
const data = api
.getContentList({
referenceName: "products",
languageCode: "en-us",
})
.then(function (contentList) {
return contentList;
})
.catch(function (error) {
console.log("Error: " + error);
});
return data;
};
export default async (req, res) => {
const response = await getProducts();
const products = response.items.map((product) => {
return {
title: product.fields.title,
id: product.fields.title.replace(/\s+/g, "-").toLowerCase(),
image: product.fields.image.url,
price: product.fields.price,
description: product.fields.description,
};
});
return res.status(200).json(products);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment