@nacelle/shopify-checkout

A minimal Shopify checkout client for headless storefronts.

Usage no npm install needed!

<script type="module">
  import nacelleShopifyCheckout from 'https://cdn.skypack.dev/@nacelle/shopify-checkout';
</script>

README

@nacelle/shopify-checkout

A minimal Shopify checkout client for headless storefronts.

npm version

Overview

@nacelle/shopify-checkout adds Shopify checkout functionality to headless commerce projects. While it's tailored to support the needs of Nacelle-powered storefronts, it's possible for any headless Shopify storefront to use this package by following the API described below.

Features

  • tiny, with zero dependencies
  • full TypeScript support
  • ships in tree-shakeable ESM, as well as UMD & IIFE formats for maximum portability
  • high code coverage

Usage

Install

npm i @nacelle/shopify-checkout

Prerequisites

To get started, you'll need:

Initializing the client

The checkout client can be initialized anywhere in your application. We recommend initializing it once. You can then import the initialized client wherever it's needed.

import createShopifyCheckoutClient from '@nacelle/shopify-checkout';

const checkoutClient = createShopifyCheckoutClient({
  storefrontCheckoutToken: '<your-storefront-api-token>',
  myshopifyDomain: '<your-shop-id>',
  storefrontApiVersion: '<storefront-api-version-of-choice>'
});

Common Types

When working with the checkout client, there are some common types that it will help to be familiar with:

Type Description
Metafield A Metafield is an object that contains two properties: a key and a value, which both have string values.
CartItem A CartItem is an object with two required properties: variantId (a Shopify global ID) and a quantity (number). A CartItem may contain the optional property metafields (Metafield[]).
ShopifyCheckout A ShopifyCheckout is an object that contains three properties: an id (string) and a url, which both have string values, and completed (boolean). The url is the address of the Shopify checkout.

Examples of Common Types:

Metafield
{ key: 'forecast', value: 'sunshine' }
CartItem
{
  variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMjM0NQ==',
  quantity: 4
},
{
  variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC85ODc2NQ==',
  quantity: 2,
  metafields: [
    { key: 'care_instructions', value: 'hand wash; drip dry' }
  ]
},

Checkout client API

The checkout client exposes two methods, get and process.

get({ id })

Retrieves an existing Shopify checkout.

Accepts: params - an object containing the id (string) of interest.

Returns: a ShopifyCheckout.

Example
const id = 'Z2lkOi8vc2hvcGlmeS9DaGVja291dC85OTg4Nzc/a2V5PTEyMzEyMw==';
const checkout = await checkoutClient.get({ id });

process(params)

Creates a new Shopify checkout, or updates an existing Shopify checkout.

Accepts: params (see examples below).

Returns: a ShopifyCheckout.

Creating a new Shopify checkout
Parameter Type Required? Description
cartItems CartItem[] An array of line items.
metafields Metafield[] ⛔️ Corresponds to a Shopify Checkout's customAttributes.
note string ⛔️ The Shopify Checkout's note
Example
// create a new checkout with checkout-level `customAttributes` and `note`
const checkout = await checkoutClient.process({
  cartItems: [
    {
      variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMjM0NQ==',
      quantity: 1
    }
  ],
  metafields: [{ key: 'gift_options', value: 'wrapped in gift paper' }],
  note: 'Thanks for taking care of the gift wrapping!'
});
Updating an existing Shopify checkout
Parameter Type Required? Description
id string An existing Shopify checkout's global ID.
cartItems CartItem[] (only if updating) An array of line items.
metafields Metafield[] (only if updating) Corresponds to a Shopify Checkout's customAttributes.
note string (only if updating) The Shopify Checkout's note.
Example
// Update an existing checkout's line items, `customAttributes`, and `note`
const checkout = await checkoutClient.process({
  id: 'Z2lkOi8vc2hvcGlmeS9DaGVja291dC85OTg4Nzc/a2V5PTEyMzEyMw==',
  cartItems: [
    {
      variantId: 'Z2lkOi8vc2hvcGlmeS9Qcm9kdWN0VmFyaWFudC8xMjM0NQ==',
      quantity: 1,
      metafields: [{ key: 'engrave_text', value: 'i ❤️ headless commerce' }]
    }
  ],
  metafields: [{ key: 'gift_options', value: 'in box with bow' }],
  note: 'Please use a red ribbon for the bow, if possible :)'
});

Advanced Usage

Using a custom fetchClient

By default, @nacelle/shopify-checkout makes GraphQL requests to Shopify's Storefront API with window.fetch. If window.fetch doesn't meet your project's requirements (e.g. if you need to support IE11), you can supply a fetchClient when initializing the checkout client. For example:

import createShopifyCheckoutClient from '@nacelle/shopify-checkout';
import isoFetch from 'isomorphic-unfetch';

const checkoutClient = createShopifyCheckoutClient({
  storefrontCheckoutToken: '<your-storefront-api-token>',
  myshopifyDomain: '<your-shop-id>',
  storefrontApiVersion: '<storefront-api-version-of-choice>',
  fetchClient: isoFetch
});

The only requirement of the fetchClient is that it implements the Fetch API (axios, for instance, won't work). We recommend checking out isomorphic-unfetch, cross-fetch, or similar.

Using a custom Shopify endpoint

If you'd prefer to specify a Shopify Storefront GraphQL API endpoint directly, without supplying a myshopifyDomain or storefrontApiVersion, you may do so by specifying a customEndpoint when initializing the checkout client. For example:

const checkoutClient = createShopifyCheckoutClient({
  storefrontCheckoutToken: '<your-storefront-api-token>',
  customEndpoint: '<your-storefront-api-endpoint>'
});