Reputation: 3040
The below is used to get products via bigcommerce api but will not work inside of the stencil environment because of cross-domain errors… Is there a way to make some form of api calls work within the theme? Maybe there are relative url endpoints to the api?
I want to do this because the catalog is not accessible through stencil's handlebars the way i need it for a theme i am working on
<script type="text/javascript">
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.bigcommerce.com/stores/***/v3/catalog/products?include=variants%2Ccustom_fields",
"method": "GET",
"headers": {
"x-auth-client": "d8zpoak96***",
"x-auth-token": "i64oipln27l***",
"cache-control": "no-cache",
}
}
$.ajax(settings).done(function (response) {
console.log(response);
});
</script>
Upvotes: 0
Views: 572
Reputation: 1866
You shouldn’t make api calls from your theme/storefront. This will openly publish your api credentials which is a security risk. You can use the product custom fields in the handlebars template context of the product details page and they should be available on the category pages as well.
If you must get data from the API to the storefront, you should make an ajax GET request to a server you can configure to make the call securely and then return the required info. I'd recommend checking this answer.
Upvotes: 2