How to load data from Shopify to PostgreSQL
Extract data from Shopify
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 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.
This request will get us back to all the events that are related to Products and Orders for our shop. 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:
- Custom Collections
- Smart Collections
By using the “limit” and “page” parameters it is possible to page your results in case that 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 data out of the Shopify API you are ready to extract and prepare them for PostgreSQL. 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 is one of the easiest and simple to use managed e-commerce platforms. It offers an easy way for anyone to create an online store and start selling goods. It natively integrates with services like Stripe and Paypal to make things even easier for potential users of the platform. Shopify also offers a very rich app marketplace, where its users can find powerful extensions and plugins that can enhance their online shops. They have a quite flexible pricing model that can cover the needs of very small to large online shops, you can start with the lite version and as your business grows quite easily you can jump to a larger plan. Some of the benefits of Shopify are:
- Easy to start. Both the experience the platform offers and the pricing model makes it extremely easy for anyone to set up and run an online shop.
- No technical skills required. As a self-managed SaaS product, Shopify requires minimum technical skills from its users.
- It is hosted. This means that you don’t have to care about where to host your online shop, they offer all the infrastructure and services needed without requiring you to delve into technical details.
- It can be personalized. Although a hosted platform, it can be easily customized to meet your brand.
Additionally, Shopify has succeeded in creating a unique ecosystem of developers who build added value services and extensions on their platform. A large number of plug-ins are available that help Shopify users to personalize even more their e-shops while developers can generate a good income by selling their applications and extensions. Of course, such a success was built on top of a well-designed API that exposes the whole platform to the developers. This API will be also used in this article to show how we can pull out valuable data from a Shopify source you own.
Prepare data from Shopify for PostgreSQL
To populate a PostgreSQL database instance with data, first, you need to have a well-defined data model or schema that describes the data. As a relational database, PostgreSQL organizes data around tables.
Each table is a collection of columns with a predefined data type as an integer or VARCHAR. PostgreSQL, like any other SQL database, supports a wide range of different data types.
A typical strategy for loading data from Shopify to a PostgreSQL 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 a PostgreSQL compatible data type.
For example, if an endpoint from Shopify returns a value as String, you should convert it into a VARCHAR with a predefined max size or TEXT data type. tables can then be created on your own database using the CREATE SQL statement.
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 PostgreSQL, you can move forward and start loading every data into the database.
PostgreSQL or simply Postgres is one of the most well-known, popular, and well-supported databases. It can be used for different workloads, from simple single-machine applications to large-scale data warehousing scenarios.
PostgreSQL is ACID-compliant, transactional, and has one of the richest feature sets; including materialized views, triggers, foreign keys, stored procedures, and an architecture that encourages its extensibility.
Especially its last characteristic has made PostgreSQL one of the most forked databases. Amazon Redshift is based on an earlier version of Postgres as other database systems like Citus Data and Greenplum Database.
All the above characteristics of PostgreSQL, a rich set of aggregation functions, the ability to define both simple and materialized views, the support for user-defined functions, and the ability to scale to pretty large datasets, make it an ideal database for analytics-related tasks.
Let’s see what it takes to populate and maintain a PostgreSQL database with data for analytics and business intelligence purposes.
Load data from Shopify to PostgreSQL
Once you have defined your schema and you have created your tables with the proper data types, you can start loading data into your database.
The most straightforward way to insert data into a PostgreSQL database is by creating and executing INSERT statements. With INSERT statements, 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.
The preferred way for adding larger datasets into a PostgreSQL database is by using the COPY command. COPY is copying data from a file on a file system that is accessible by the PostgreSQL instance, in this way much larger datasets can be inserted into the database in less time.
You should also consult the documentation of PostgreSQL on how to populate a database with data. It includes a number of very useful best practices on how to optimize the process of loading data into your PostgreSQL database.
COPY requires physical access to a file system in order to load data. Nowadays, with cloud-based, fully managed databases, getting direct access to a file system is not always possible. If this is the case and you cannot use a COPY statement, then another option is to use PREPARE together with INSERT, to end up with optimized and more performant INSERT queries.
Updating your Shopify data on PostgreSQL
As you will be generating more data on Shopify, you will need to update your older data on PostgreSQL. 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 PostgreSQL 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 data pipelines you own, 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 PostgreSQL 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 PostgreSQL
So far we just scraped the surface of what you can do with PostgreSQL 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 PostgreSQL right away.