SDK Reference 3.x
Version 4 is now available! Check out the Release Notes to read about its exciting new features, and v3.x to v4.x Migration Guide for details on how to upgrade.
The Chord client-side Javascript SDK can be installed via a Gatsby theme. It's available as an npm package, @chordcommerce/gatsby-theme-autonomy, via GitHub Packages.
Despite the name, this package does not provide a visual theme. To quote from the Gatsby documentation:
Gatsby themes are plugins that include a gatsby-config.js file and add pre-configured functionality, data sourcing, and/or UI code to Gatsby sites. You can think of Gatsby themes as separate Gatsby sites that can be put together and allow you to split up a larger Gatsby project!
gatsby-theme-autonomy includes the Chord SDK, a set of methods you can import and use in your React components to do things like add items to a cart and check out. The theme also preconfigures your Gatsby site with a number of useful ecommerce features like product feeds and internationalization support.
The useAnalytics hook returns a series of functions that fire a tracking event to Segment, our integrated analytics platform.
Many Segment events are fired automatically by the @chordcommerce/gatsby-theme-autonomy theme. However, some events need to be closely tied to the UI, like click tracking. These events are available via the useAnalytics hook and listed below.
The trackClickCollection function fires a Collection Clicked event to Segment. You should call this function when the user clicks on a link to a collection.
Example
The trackClickProduct function fires a Product Clicked event to Segment. You should call this function when the user clicks on a link to a product.
Example
The trackCreateStockRequest function fires a Stock Request Created event to Segment. You should call this function when a product waitlist form is submitted.
Example
The trackViewCart function fires a Cart Viewed event to Segment. You should call this function when the cart page is viewed.
Example
The trackProductViewed function fires a Product Viewed event to Segment. You should call this function when a product page is opened.
Example
The useAuth hook returns information about the current authentication session and functions for logging in and logging out. Chord supports passwordless authentication with Magic.
isLoggedIn is a boolean indicating whether the user is currently logged in.
Example
isFetching is a boolean indicating whether the user's authentication status is currently being fetched.
Example
token is a string representing the user's authentication token. This token is automatically appended to Chord API requests when the user has an active session.
The handleCallback function completes the login process, when the user is redirected back to the site after clicking a link in their email. The SDK calls this function automatically when the Magic url parameter is detected on page load, so you are unlikely to need to call this function.
The getToken function retrieves an authentication token for the current user session. While you are unlikely to need the token directly, since the SDK automatically appends it to relevant Chord API requests, you can use this function to check whether the user's session is still active. If the user's session has expired, getToken will throw an error.
You may choose to call getToken on page load or when a user visits a protected route (like an account page) to check that their session has not expired since the last time you checked.
Example
The login function should be called when a user submits a login form. It will trigger an email to the user for verification.
The showUI option controls whether Magic's default UI will display from when login is called until the user clicks the link they've received in an email.
Example
The logout function terminates the user's current session.
Example
Example
Example
Example
Example
Example
This function should be called on the order confirmation page, when the user is redirected back to the site from checking out via Stripe.
Example
forgetCart()
This function can be called at any time to clear the current order from the session. It is called automatically by finalizeCheckout and logout, so no need to call it in those cases.
Example
Updates a line item in the current order.
The giftCardsDetails object is returned in the lineItem object if the lineItem is a gift card. Additionally, newly redeemed gift cards are available in the user object for indicating to the user that they have received a gift.
The gift cards can be updated by including the gift card id along with any of the following params: recipientName, recipientEmail, giftMessage, purchaserName, sendEmailAt.The recipient email is a required field as that will be the account used to redeem the gift cards.
Note: endDate is optional. The subscription will renew indefinitely if endDate is not specified.
Example
The createStockRequest adds the customer to a product variant wait list in the OMS. When the variant is marked back in stock, customers on the wait list will automatically receive an email notification.
For kits, you will need to pass both sku and slug as options to the createStockRequest hook method.
Example
The useSubscription hook returns the current loaded subscription and functions for managing the subscription.
The subscription object represents a subscription. You will need to explicitly call loadSubscription before this object will be populated.
Example
The loadSubscription function loads information about the subscription from the Chord API.
Example
The skipSubscription function calls the Chord API to skip the next subscription delivery.
Example
The cancelSubscription function calls the Chord API to cancel a subscription.
Example
ThepauseSubscription function calls the Chord API to pause a subscription.
Paused subscriptions will not be processed until manuallyresumedor theactionable_dateis reached.Subscriptions can be either paused until a specifiedactionable_dateby sending an optionalactionable_dateparam or paused indefinitely by not passing any params. The minimum date to whichactionable_datecan be set is the next day.
TheresumeSubscriptionfunction calls the Chord API to resume a subscription.
Paused subscriptions will not be processed until manuallyresumedor theactionable_dateis reached.Subscriptions can be resumed at a specifiedactionable_dateby sending an optionalactionable_dateparam or resumed the next day (the minimum to which actionable_datecan be set) by not passing any params.
TheupdateSubscription function calls the Chord API to update a subscription.
To update the addresses, all fields need to be sent even if only a single field is updated. The two addresses that can be updated are shipAddressAttributes and billAddressAttributes.
In addition, the interval_unit('day'/'week'/'month'/'year'), interval_length, actionable_date and the end_date(null for no end date ) can be updated.
The useTranslate hook provides support for internationalization, powered by gatsby-plugin-intl.
To localize the text in your React components, use the translate function returned by the useTranslate hook. translate will load the specified string from your language resource file.
We recommend using translate even if your site currently only supports one language. It's easier to edit your website text if it's all in a single configuration file, and it makes it trivial to add multi-language support in the future.
Example
The component above assumes there is a file at /src/intl/<language>.json that looks like this:
The useUser hook returns the current user and functions for loading data about the user. At the moment, the only user information Chord supports is the user's referral identifier and personalized referral url.
The user object represents the current user. You will need to explicitly call loadUser or loadUserReferralIdentifier before this object will be populated.
Example
The loadUser function loads information about the current order's associated user from the Chord API.
Example
The loadUserReferralIdentifier function loads the referral identifier information for a user with the given email address from the Chord API.
Example
The modifyUser function is used to update the user's profile information.
Example
This module allows to checkout using Square, the typical flow is:
- Attach an email address to the order using modifyCart({ email: "[email protected]" }). This is only needed for guest checkouts, if the user is logged in, this is not needed and the user's email will be used.
- Attempt a payment via Square using the updateOrderPayment() function after retrieving the Square token client-side, using the Square Web Payment SDK.
The updateOrderAddresses() function is used to update the order's ship address. A valid ship address has the following format:
To retrieve the available states and the country id, please use getStates().
Example
The getStates() function is used to retrieve information about countries and states available in the system to be used when we need to update the order's address. It takes the ISO code of the country and returns the list of states along with other information related to the country itself.
Example
The updateOrderDelivery() function allows choosing the shipping option for the current order. At the moment we do not support multiple delivery options, so this function is only needed to move the checkout from the delivery step to the payment step. During this process, shipping costs and taxes will be added to the order.
This function should be called only when the order is in the delivery state, meaning that a valid address has been added to the order previously. This way it will be able to correctly pick the right delivery option based on the shipping address and to correctly compute taxes based on the shipment route.
Example
The updateOrderPayment() function allows completing the payment step of the checkout. Before using this function, the client should retrieve a token using the Web Payments SDK provided by Square. When the order is successfully completed, the payment source will be added to the user's wallet (for authenticated users only), allowing to re-use the payment token later, without having to input the credit card information again.
This function should be called only when the order is in the payment state, meaning that a valid delivery option has been added to the order previously. At this stage the order is updated with shipping costs and taxes, allowing to complete the payment with the right amount.
For now, this endpoint doesn't support the ability to pay with an existing token. In the future we will provide this support, allowing to re-use a previously saved credit card from the user wallets.
Example
finalizeCheckout() is the last method that needs to be called to complete the order with Square. Call this method when you want to move the user to the thank you page and let them start a new order.
Example
This function should be called only when the order is in the "confirm" state, meaning that a valid payment has been added to the order already.