How to load data from Shopify to MS SQL Server
Extract your Shopify's data
Shopify exposes its complete platform to developers through its API. It is used by thousands of developers to create applications that are then sold through the Shopify marketplace.
As a Web API following the RESTful architecture principles, it can be accessed through HTTP. As a RESTful API, interacting with it can be achieved by using tools like CURL or Postman or by using HTTP clients for your favorite language or framework. A few suggestions:
- Apache HttpClient for Java
- Spray-client for Scala
- Hyper for Rust
- Ruby rest-client
- Python http-client
Shopify also offers a number of SDKs that are officially supported and maintained by them and that can be used to access their platform for different uses. For example, by using the iOS and Android Buy SDK it is possible to add Shopify checkout capabilities to your mobile application. Other SDKs that are offered are:
Shopify API Authentication
There are two different types of applications in Shopify when it comes to authentication, private and public apps.
Private apps will only function on individual stores, so you don’t need to authenticate them through Oauth. You can get Private app credentials through your Partner dashboard or by logging into any Shopify admin. You can also use this API key to manipulate your store using the API console, without the need for building a fully functional app.
The apps you create in your Partners dashboard function as public apps which can be made available for download in Shopify’s App Store if you meet the criteria.
Shopify Rate Limiting
The API call limit operates using a “leaky bucket” algorithm as a controller. This allows for infrequent bursts of calls and allows your app to continue to make an unlimited amount of calls over time. The bucket size is 40 calls (which cannot be exceeded at any given time), with a “leak rate” of 2 calls per second that continually empties the bucket. If your app averages 2 calls per second, it will never trip a 429 error (“bucket overflow”).
Endpoints and Available Resources
Shopify exposes 35+ endpoints covering all the possible touchpoints of e-commerce. Some of the most important resources that can be accessed through these endpoints are the following:
- Abandoned checkouts: used to return abandoned checkouts. A checkout is considered abandoned when a customer has entered their billing & shipping info but has yet to complete the purchase.
- ApplicationCharge: Request to charge a shop a one-time fee by issuing this call.
- Article: Operations concerning articles in your Blog.
- Asset: files that make up the theme of a shop.
- Blog: Shopify, in addition to the e-commerce features also offers an environment where the merchant can create a Blog for her shop.
- CarrierService: A Carrier Service (also known as a Carrier Calculated Service or Shipping Service) provides real-time shipping rates to Shopify.
- Collect: An object that connects a product to a custom collection.
- Customer: A customer resource instance represents a customer account with the shop.
- Event: Events are generated by specific Shopify resources when specific things happen, such as the creation of an article.
- Order: An order is a customer’s completed request to purchase one or more products from a shop.
- Product: A product is an individual item for sale in a Shopify shop.
- Transaction: Transactions are created for every order that results in an exchange of money.
For a complete list of endpoints, you can refer here.
It is clear that with such a rich platform and API the data that can be pulled out of Shopify are both valuable and come in large quantities. So, let’s assume that we want to pull all events out of Shopify to use them for further analysis. To do so we need to make a request to the Event endpoint like this.
SH
GET /admin/events.json?filter=Product,Order
This request will get us back to all the events that are related to Products and Orders for our shop. The response will be in JSON and will look like the following:
JSON
HTTP/1.1 200 OK{"events": [{"id": 677313116,"subject_id": 921728736,"created_at": "2008-01-10T08:00:00-05:00","subject_type": "Product","verb": "create","arguments": ["IPod Touch 8GB"],"body": null,"message": "created a new product: <a href=\"\/admin\/products\/921728736\">IPod Touch 8GB<\/a>.","author": "Shopify","description": "created a new product: IPod Touch 8GB.","path": "\/admin\/products\/921728736"},{"id": 365755215,"subject_id": 632910392,"created_at": "2008-01-10T07:00:00-05:00","subject_type": "Product","verb": "create","arguments": ["IPod Nano - 8GB"],"body": null,"message": "created a new product: <a href=\"\/admin\/products\/632910392\">IPod Nano - 8GB<\/a>.","author": "Shopify","description": "created a new product: IPod Nano - 8GB.","path": "\/admin\/products\/632910392"}]}
Inside the response, there will be an array of objects with each one representing one Order or Product.
Events are generated for the following resources:
- Articles
- Blogs
- Custom Collections
- Comments
- Orders
- Pages
- Products
- Smart Collections
By using the “limit” and “page” parameters it is possible to page your results, in case you need to pull out a large number of events. Additionally, there are a number of ways that you can filter the results for example based on a date, so in a continuous data extraction process, it is possible to pull only new data and avoid duplicates. This can be achieved by using the “created_at_min” and “created_at_max” parameters in your GET request.
After you have successfully pulled your data by a Shopify API you are ready to extract and prepare them for MS SQL Server. Of course, the above process is only for one of the available resources, if you would like to have a complete view of all the available data then you will have to create a much complex ETL process including the majority of the 35+ resources that Shopify has.
Shopify Data Preparation for Microsoft SQL Server
As in every relational database, SQL Server requires a well-defined database schema before we start populating with data. Data is organized in schemas, which are distinct namespaces where database objects belong to.
The most common database objects are of course tables which have a number of columns with each one having a declared data type. MS SQL Server supports a large number of different data types. This gives us great flexibility in expressing the data that we have and at the same time optimizing our data warehouse.
When working with data coming from web services, where data is usually serialized in JSON, it is important to correctly map the data to the right data types. As changing the data types in the future is a process that might cost in downtime of your database, it is important to spend enough time thinking about the proper data type assignments.
For example, dates in JSON are just strings, but when storing data objects in a database, we can enhance analytics with great capabilities by transforming the raw string data into an appropriate data type. A typical strategy for loading data using Shopify to MS SQL Server database is to create a schema where you will map each API endpoint to a table. Each key inside the Shopify API endpoint response should be mapped to a column of that table and you should ensure the right conversion to an SQL Server compatible data type.
Of course, you will need to ensure that as the data types from the Shopify API might change, you will adapt your database tables accordingly, there’s no such thing as automatic data typecasting. After you have a complete and well-defined data model or schema for Microsoft SQL Server, you can move forward and start loading your data into the database.
Load data from Shopify to MS SQL Server
As a feature-rich and mature product, MS SQL Server offers a large and diverse set of methods for loading data into a database. One way of importing data into your database is by using the SQL Server Import and Export Wizard. With it and through a visual interface you will be able to bulk load data using a number of data sources that are supported.
You can import data from another SQL Server, from an Oracle database, from Flat Files, from an Access Data Source, PostgreSQL, MySQL, and finally Azure Blob Storage. Especially if you are using a managed version of MS SQL Server on Azure, you should definitely consider utilizing the Azure Blob Storage connection.
In this way, you will be loading data as Blobs on Azure and your MS SQL Server database will sync with it through the Import and Export Wizard.
Another way for importing bulk data into an SQL Server, both on Azure and on-premises, is by using the bcp utility. This is a command-line tool that is built specifically for bulk loading and unloading of data into an MS SQL database.
Finally and for compatibility reasons, especially if you are managing databases from different vendors, you can BULK INSERT SQL statements.
In a similar way and as it happens with the rest of the databases, you can also use the standard INSERT statements, where you will be adding data row-by-row directly to a table. It is the most basic and straightforward way of adding data into a table but it doesn’t scale very well with larger datasets.
So for bulk datasets, you better consider one of the previous methods.
Updating your Shopify data on MS SQL Server
As you will be generating more data on Shopify, you will need to update your older data on an MS SQL Server database. This includes new records together with updates to older records that for any reason have been updated on Shopify.
You will need to periodically check Shopify for new data and repeat the process that has been described previously while updating your currently available data if needed. Updating an already existing row on a SQL Server table is achieved by creating UPDATE statements.
Another issue that you need to take care of is the identification and removal of any duplicate records on your database. Either because Shopify does not have a mechanism to identify new and updated records or because of errors on your data pipelines, duplicate records might be introduced to your database.
In general, ensuring the quality of the data that is inserted in your database is a big and difficult issue and MS SQL Server features like TRANSACTIONS can help tremendously, although they do not solve the problem in the general case.
The best way to load data from Shopify to MS SQL Server
So far we just scraped the surface of what you can do with MS SQL Server and how to load data into it. Things can get even more complicated if you want to integrate data coming from different sources.
Are you striving to achieve results right now?
Instead of writing, hosting, and maintaining a flexible data infrastructure use RudderStack that can handle everything automatically for you.
RudderStack with one click integrates with sources or services, creates analytics-ready data, and syncs your Shopify to MS SQL Server right away.