Close

Sign up to Gadget

Sign Up

Programmatically Sync Store Data with the New Sync API

Mohammad Hashemi
March 30, 2022

Gadget now offers the ability to programmatically sync Shopify data to your backend, using the new Sync API. The API can be used to fetch store data, check on a sync’s status, or power a user interface. The Sync API is available on all Gadget projects with an active Shopify Connection.

Overview

Gadget’s Shopify Connection provides a robust data sync between your Gadget app and a Shopify store, configured in just a few clicks. Out of the box, your app OAuths with Shopify, reliably receives webhooks, and syncs historical data without you having to write any code. 

The syncing system for the Shopify Connection works by leveraging Actions and Effects. Each time Gadget goes to sync the connected shop, it creates a new Shopify Sync record and dispatches the Run and either the Complete or Error actions based on the outcome of the sync.


In the past, Gadget offered you two ways to sync historical data:

  • Manual sync: The ability to run a sync of historical data manually, by clicking on “Sync” on the Connections screen.
  • Daily sync: An automated sync that Gadget runs each day, fetching the last 24 hours of events and rerunning Actions on any missed webhooks to keep your data in sync.

While manual and daily syncs cover many use-cases, developers often need the ability to trigger a sync programmatically, in response to end-user behavior or other events in the system. 

For example:

  • When a store installs your app, automatically sync historical records without having to log into Gadget.
  • When an end-user turns on a feature in your app, automatically sync enough historical data to power the new feature.

For maximum control, we’re introducing the Sync API, which offers you the ability to trigger and react to syncs in code.  The Shopify Sync model and its Actions are now available in your application’s GraphQL API, so you can run them to fetch data, query them to check on sync statuses, or invoke them to power your application. 

You can start a Shopify Sync programmatically by making a call to the <inline-code>runShopifySync<inline-code> mutation in GraphQL from any client or your Gadget backend:


This will dispatch the <inline-code>Run<inline-code> action in your Sync model, and subsequently the <inline-code>Complete<inline-code> or <inline-code>Error<inline-code> actions, depending on the result of the sync.

If your app should run a sync as soon as a merchant installs it, you can add a Success Effect to your Shop Model. The Shop model captures install/uninstall events from Shopify, and can run the sync code above:

The Sync API also gives you the ability to set a starting point for the sync. For example, if you only want to sync records from the last five days, you can do so by passing the optional parameter <inline-code>syncSince<inline-code>, as shown below:


In the event that a sync fails, you can troubleshoot the failure by looking for the sync ID in your logs to find the relevant details:

The Sync API is instantly available on every Gadget project, once a Shopify Connection is established. We’re excited to see what you build with it.

For more examples on what you can build using the Shopify Connection, check out these additional blog posts:

Keep reading to learn about how it's built

Under the hood