Shopify Buy SDK with Tags
This is simply a fork of the original buy SDK with tags enabled on the product queries by default. Based on spence's original
This was such a deal breaker for me when I first started using the package that I felt it would be good to just republish the package exactly the same but with tags enabled.
I might keep this package up to date with the original library with this added feature.
Shopify JavaScript Buy SDK
Note: For help with migrating from v0 of JS Buy SDK to v1 check out the
Migration Guide.
The JS Buy SDK is a lightweight library that allows you to build ecommerce into
any website. It's based on Shopify's Storefront API
and provides the ability to retrieve products and collections from your shop, add products to a cart, and checkout.
Full API docs are available here.
Changelog
View our Changelog for details about our releases.
SDK Version Support
Each version of the JS Buy SDK uses a specific Storefront API version and the support of an SDK version is directly linked to the support of the corresponding Storefront API version. Storefront API versioning information can be found here.
Table Of Contents
Installation
With Yarn:
$ yarn add shopify-buy
With NPM:
$ npm install shopify-buy
CDN:
There is a minified UMD build of the latest release available via CDN (see the Changelog for details about the latest release):
<script src="https://sdks.shopifycdn.com/js-buy-sdk/v2/latest/index.umd.min.js"></script>
If you don't want to use the latest version, you can use a specific older release version:
<script src="https://sdks.shopifycdn.com/js-buy-sdk/1.11.0/index.umd.min.js"></script>
You can also fetch the unoptimized release for a version (2.0.1 and above). This will be larger than the optimized version, as it will contain all fields that are available in the Storefront API:
<script src="https://sdks.shopifycdn.com/js-buy-sdk/2.0.1/index.unoptimized.umd.min.js"></script>
Builds
The JS Buy SDK has four build versions: ES, CommonJS, AMD, and UMD.
ES, CommonJS:
import Client from 'shopify-buy';
AMD:
import Client from 'shopify-buy/index.amd';
UMD:
import Client from 'shopify-buy/index.umd';
UMD Unoptimized:
This will be larger than the optimized version, as it will contain all fields that are available in the Storefront API. This should only be used when you need to add custom queries to supplement the JS Buy SDK queries.
import Client from 'shopify-buy/index.unoptimized.umd';
Examples
Initializing the Client
If your store supports multiple languages, Storefront API can return translated resource types and fields. Learn more about translating content.
import Client from 'shopify-buy';
const client = Client.buildClient({
domain: 'your-shop-name.myshopify.com',
storefrontAccessToken: 'your-storefront-access-token'
});
const clientWithTranslatedContent = Client.buildClient({
domain: 'your-shop-name.myshopify.com',
storefrontAccessToken: 'your-storefront-access-token',
language: 'ja-JP'
});
Fetching Products
client.product.fetchAll().then((products) => {
console.log(products);
});
const productId = 'gid://shopify/Product/7857989384';
client.product.fetch(productId).then((product) => {
console.log(product);
});
const handle = 'product-handle';
client.product.fetchByHandle(handle).then((product) => {
console.log(product);
});
Fetching Collections
client.collection.fetchAllWithProducts().then((collections) => {
console.log(collections);
console.log(collections[0].products);
});
const collectionId = 'gid://shopify/Collection/369312584';
client.collection.fetchWithProducts(collectionId, {productsFirst: 10}).then((collection) => {
console.log(collection);
console.log(collection.products);
});
Creating a Checkout
client.checkout.create().then((checkout) => {
console.log(checkout);
});
Updating checkout attributes
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const input = {customAttributes: [{key: "MyKey", value: "MyValue"}]};
client.checkout.updateAttributes(checkoutId, input).then((checkout) => {
});
Adding Line Items
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const lineItemsToAdd = [
{
variantId: 'gid://shopify/ProductVariant/29106064584',
quantity: 5,
customAttributes: [{key: "MyKey", value: "MyValue"}]
}
];
client.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
console.log(checkout.lineItems);
});
Updating Line Items
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const lineItemsToUpdate = [
{id: 'gid://shopify/CheckoutLineItem/194677729198640?checkout=e3bd71f7248c806f33725a53e33931ef', quantity: 2}
];
client.checkout.updateLineItems(checkoutId, lineItemsToUpdate).then((checkout) => {
console.log(checkout.lineItems);
});
Removing Line Items
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const lineItemIdsToRemove = [
'gid://shopify/CheckoutLineItem/194677729198640?checkout=e3bd71f7248c806f33725a53e33931ef'
];
client.checkout.removeLineItems(checkoutId, lineItemIdsToRemove).then((checkout) => {
console.log(checkout.lineItems);
});
Fetching a Checkout
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8'
client.checkout.fetch(checkoutId).then((checkout) => {
console.log(checkout);
});
Adding a Discount
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const discountCode = 'best-discount-ever';
client.checkout.addDiscount(checkoutId, discountCode).then(checkout => {
console.log(checkout);
});
Removing a Discount
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
client.checkout.removeDiscount(checkoutId).then(checkout => {
console.log(checkout);
});
Updating a Shipping Address
const checkoutId = 'gid://shopify/Checkout/e3bd71f7248c806f33725a53e33931ef?key=47092e448529068d1be52e5051603af8';
const shippingAddress = {
address1: 'Chestnut Street 92',
address2: 'Apartment 2',
city: 'Louisville',
company: null,
country: 'United States',
firstName: 'Bob',
lastName: 'Norman',
phone: '555-625-1199',
province: 'Kentucky',
zip: '40202'
};
client.checkout.updateShippingAddress(checkoutId, shippingAddress).then(checkout => {
});
Completing a checkout
The simplest way to complete a checkout is to use the webUrl property that is returned when creating a checkout. This URL redirects the customer to Shopify's online checkout to complete the purchase.
Expanding the SDK
Not all fields that are available on the Storefront API are exposed through the SDK. If you use the unoptimized version of the SDK, you can easily build your own queries. In order to do this, use the UMD Unoptimized build.
Initializing the Client
import Client from 'shopify-buy/index.unoptimized.umd';
const client = Client.buildClient({
domain: 'your-shop-name.myshopify.com',
storefrontAccessToken: 'your-storefront-access-token'
});
Fetching Products
const productsQuery = client.graphQLClient.query((root) => {
root.addConnection('products', {args: {first: 10}}, (product) => {
product.add('title');
product.add('tags');
});
});
client.graphQLClient.send(productsQuery).then(({model, data}) => {
console.log(model);
});
Example Apps
For more complete examples of using JS Buy SDK, check out our storefront-api-examples project.
There are JS Buy SDK specific example apps in Node, Ember, and React. You can use these examples as a guideline for creating your own custom storefront.
Documentation
Contributing
For help on setting up the repo locally, building, testing, and contributing
please see CONTRIBUTING.md.
Code of Conduct
All developers who wish to contribute through code or issues, take a look at the
CODE_OF_CONDUCT.md.
License
MIT, see LICENSE.md for details.