Can't initialize JavaScript Buy SDK

Highlighted
New Member
2 0 0

Hi,

 

My main purpose is to use Shopify to embed products inside my website, Shopify will work as a backend and my website as frontend.

 

With Shopify button I can't find a way to enable the cart so that product would be added to cart instead that end up on checkout page so I did a research and endup at this page

https://shopify.github.io/js-buy-sdk/#initializing-the-client

 

That I guess is the latest version

I was trying to fetch product data in a test page 

<script src="http://sdks.shopifycdn.com/js-buy-sdk/v2/latest/index.umd.min.js"></script>

<script>
		
		//$(function() {
			
			import Client from 'shopify-buy';

			const client = Client.buildClient({
			  domain: 'myshop.myshopify.com',
			  storefrontAccessToken: '1222222'
			});
					const productId = '123456';			client.product.fetch(productId).then((product) => {
				console.log(product);
			});

		//});
		
	</script>

but I get an error 

 

SyntaxError: Unexpected identifier 'Client'. import call expects exactly one argument.

I did some research but I can only find old documentation of a previous mode about ShopifyBuy.buildClient

 

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 167

Hey @al404

 

The import syntax assumes you're using a build tool. If you're loading the SDK via the CDN, try this instead:

 

<script src="http://sdks.shopifycdn.com/js-buy-sdk/v2/latest/index.umd.min.js"></script>

<script>
const client = ShopifyBuy.buildClient({
  domain: 'myshop.myshopify.com',
  storefrontAccessToken: '1222222'
});
const productId = '123456';
client.product.fetch(productId).then((product) => {
  console.log(product);
});
</script>

 

Notice; Out of office, replies will be delayed until my return. Thanks!
1 Like
Highlighted
New Member
2 0 0

Hi,

 

I get this error: Failed to load resource: the server responded with a status of 403 () 

https://mytestshop.myshopify.com/api/2020-04/graphql

 

Unhandled Promise Rejection: [object Object]

 

I also try to add 

 

const client = ShopifyBuy.buildClient({
  domain: 'myshop.myshopify.com',
  storefrontAccessToken: '1222222',
  language: 'it-IT'
});

and enable all access in read / write but still get this error

I also edit the product that is enabled for the APP

0 Likes
Highlighted
Shopify Staff
Shopify Staff
1041 140 167

You can log more info like this:

client.product.fetch(productId).then((product) => {
  console.log(product);
}).catch(error => {
  console.log(error)
})

 

A 403 indicates a problem with the token - are you sure the token is correct / can you share store url?

Notice; Out of office, replies will be delayed until my return. Thanks!
0 Likes