Last active
November 13, 2020 15:13
-
-
Save joshua-isaac/95285ae9452c469ada939ce1b0770801 to your computer and use it in GitHub Desktop.
Fetch products from Products Content List in Agility CMS with Agility Content Fetch SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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