How to load data from Square to SQL Data Warehouse

Blendo Team

Square is a simple and powerful POS software for businesses. What if I want to get more data-driven and gather all my data from Square to my BI or custom analytics stack or to my data warehouse like SQL Data Warehouse? How can I analyse the data generated with Square? This guide is going to provide you with a clear picture about how to load data from Square to SQL Data Warehouse.  We will use Square’s API to access and extract e-mail related data and load it into SQL Data Warehouse for further analysis.

You will need to write the code to get the data and make sure that this process will run every time new data are generated. Alternatively, you can use products like Blendo that can handle this kind of problems automatically for you.

What is Square?

Square is a simple and powerful POS software for businesses. It is designed to run on multiple devices, ranging from smartphones to a merchant’s counter without requiring couple specialised equipment. As a Point of Sale software it takes care of:

  • Digital receipts from your customers
  • Your product inventory
  • Sales reports
  • valuable analytics that you can use to better understand the performance of your business

As a product, Square is suitable for both big and small businesses and scales accordingly to your needs.

The Point of Sale software of Square is free to download and use, it charges on a per transaction basis. Square charges a flat, fixed rate of 2.75% of volume for all Visa, MasterCard, Discover, American Express and other vendors transactions.

How to load data from Square to SQL Data Warehouse

How to load data from Square to SQL Data Warehouse

Additionally to the POS software, Square has a number of additional products, ranging from hardware that is needed for POS like:

  • Contactless chip readers to support EMV and NFC payments
  • MAGSTRIPE readers
  • or even stands to transform an iPad into a POS

to Payment and accounting related services like:

  • Payment processing
  • Invoice handling
  • Gift card management

Square has transformed from a POS software vendor to a complete payment platform which includes:

  • POS systems
  • Employee management
  • An app marketplace
  • Payroll services
  • and even appointment services

Finally, it is also offering a gateway to sell goods online, services to add delivery for restaurants and funding for small businesses.

As a platform, Square exposes a rich API that can be used to build applications that can be delivered via its app marketplace. This API is expressive enough to allow us to pull data that can be used for analytics. In this article, we will explore how we can use the Square API to pull this data.

What is Microsoft Azure SQL Data Warehouse?

SQL Data Warehouse is the data warehousing solution that you can use if you are a user of Microsoft Azure. It’s an elastic data warehousing as a service solution, emphasising it’s enterprise focus. It also speaks SQL like the previous two solutions and it supports querying both relational and non-relational data.

Load data to Azure SQL Data Warehouse

Load data to Azure SQL Data Warehouse

It offers a number of enterprise-class features like support for hybrid cloud installations and strong security. It’s probably the less mature solution compared to the two others though, it’s still in “Preview” mode although accessible to existing Azure subscribers.

How to Extract my data from Square?

As a platform, Square exposes a number of different APIs that cover different needs.

  • E-commerce API, with which you can take payments using
  • Register API, which can be used to build your own applications for in-store or mobile payments.
  • Item & inventory management API.
  • Sales reporting & Analytics API.
  • Employee management API.

Square is currently maintaining two version of the API, Connect v1 and Connect v2. There’s also an SDK for the Register API. It is possible that you might need to use both v1 and v2 of the Connect API, as not all endpoints have been implemented in the second and newest version. Mixing the two API versions together is encouraged by Square.

All the APIs that Square exposes are web APIs. As Web APIs following the RESTful architecture principles, they can be access 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 favourite language or framework. A few suggestions:

Square also maintains official SDKs or libraries for the following languages:

Because of the popularity of Square a number of unofficial libraries/SDKs are also implemented, so a quick search on Google or Github will help you find a library for your favourite framework or language.

Square API Authentication

In order to use the Square API you first need to register a new application with Square. By doing that you are also generating credentials that will be used to access the API endpoints.

To register your application:

1          Go to https://connect.squareup.com/apps and sign in to your Square account.

2          Click New Application and enter a name for your application.

3          Click Create App.

The application dashboard displays your new app’s credentials. One of these credentials is the personal access token. This token gives your application full access to your own Square account. Copy its value to use in the next step.

Square API rate limiting

If Connect API endpoints receive too many requests associated with the same application or access token in a short time window, they might respond with a 429 Too Many Requests error. If this occurs, try your request again at a later time. The current rate is on the order of 10 QPS. This might change in the future and is not officially documented so make sure that you properly handle our requests and responses.

Pull data from the Square platform

As mentioned earlier, Square exposes a number of APIs for different applications, thus each one offers different opportunities for pulling useful data out of it. In this post we will focus on how to get meaningful data out of the Square Connect API, that can be used for analytics.

The Square Connect API, allows us to use the Square platform to execute payment transactions, so a large number of endpoints are related to these tasks. We do not need to access these end points for analytic purposes. In a payment system what we need to track and use as data for further analysis are events related to what is happening on our system. For example we would like to be able to track when a new payment has happened, when something went wrong with a transaction etc. To do that we need to monitor the system for specific events and make sure that we store these events in a data warehouse solution like SQL Data Warehouse for further analysis.

Square Connect API, offers a notification through web hooks mechanism to receive notifications any time an important event happens in our system. We can use this mechanism to track and record events from the Square Platform for analytics. We can receive real-time webhook notifications as a merchant accepts payments. Get started with webhooks with the following steps:

1          Open your application’s settings from the application dashboard and select the Webhooks tab.

2          Click Enable Webhooks if it isn’t already enabled.

3          In the Notification URL field, specify the URL of your webhook page and click Save.

4          Subscribe to webhook notifications from your own merchant account by sending the following curl request to the Update Webhooks endpoint (providing your personal access token and location ID where indicated):

curl -X PUT -H "Authorization: Bearer PERSONAL_ACCESS_TOKEN" -H "Content-Type: application/json" -d "[\"PAYMENT_UPDATED\"]" https://connect.squareup.com/v1/LOCATION_ID/webhooks

By doing the above, we request the Square Connect API to notify us every time a new payment is happening, so we can track these events. The API is near real time and you will receive the notification in no more than 60 seconds from the moment it happens.

 The event that will be sent to our webhook will look like this:

{
  "merchant_id": "18YC4JBH91E1H",
  "location_id": "JGHJ0343",
  "event_type": "PAYMENT_UPDATED",
  "entity_id": "Jq74mCczmFXk1tC10GB"
}

As the above event does not include information that can be used directly, we will have to extract the “entity_id” value and use it to poll the “RetrieveTransaction” endpoint to get the full details of the event. If we do that we will get a full response like the following:

{
  "transaction": {
    "id": "KnL67ZIwXCPtzOrqj0HrkxMF",
    "location_id": "18YC4JDH91E1H",
    "created_at": "2016-03-10T22:57:56Z",
    "tenders": [
      {
        "id": "MtZRYYdDrYNQbOvV7nbuBvMF",
        "location_id": "18YC4JDH91E1H",
        "transaction_id": "KnL67ZIwXCPtzOrqj0HrkxMF",
        "created_at": "2016-03-10T22:57:56Z",
        "note": "some optional note",
        "amount_money": {
          "amount": 5000,
          "currency": "USD"
        },
        "processing_fee_money": {
          "amount": 138,
          "currency": "USD"
        },
        "type": "CARD",
        "card_details": {
          "status": "CAPTURED",
          "card": {
            "card_brand": "VISA",
            "last_4": "1111"
          },
          "entry_method": "KEYED"
        }
      }
    ],
    "reference_id": "some optional reference id",
    "product": "EXTERNAL_API"
  }
}

This more complete transaction object  can be used for analysis purposes.

As we can see, getting data from the Square platform to do analytics requires the combination of both a pull and push logic where the platform first notifies us about events and then we pull out the complete transaction object by performing requests to the API. This approach might make the whole process a bit complicated especially if we consider that the above example covers just one event type and we would like to do that for every possible event that the platform tracks.

How can I Load my data from Square to SQL Data Warehouse?

SQL Data Warehouse supports numerous options for loading data, such us:

  • PolyBase
  • Azure Data Factory
  • BCP command-line utility
  • SQL Server integration services

As we are interested in loading data from online services by using their exposed HTTP APIs, we are not going to consider the usage of BCP command-line utility or SQL server integration in this guide. We’ll consider the case of loading our data as Azure storage Blobs and then use PolyBase to load the data into SQL Data Wareho use.

Accessing these services happens through HTTP APIs, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. You can access these APIs by using a tool like CURL or Postman. Or use the libraries provided by Microsoft for your favourite language. Before you actually upload any data you have to create a container which is something similar as a concept to the Amazon AWS Bucket, creating a container is a straight forward operation and you can do it by following the instructions found on the Blog storage documentation from Microsoft. As an example, the following code can create a container in Node.js.

blobSvc.createContainerIfNotExists('mycontainer', function(error, result, response){
  if(!error){
    // Container exists and allows
    // anonymous read access to blob
    // content and metadata within this container
  }
});

After the creation of the container you can start uploading data to it by using again the given SDK of your choice in a similar fashion:

blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob', 'test.txt', function(error, result, response){
  if(!error){
    // file uploaded
  }
});

When you are done putting your data into Azure Blobs you are ready to load it into SQL Data Warehouse using PolyBase. To do that you should follow the directions in the Load with PolyBase documentation. In a summary the required steps to do it, are the following:

  • create a database master key
  • create a database scoped credentials
  • create an external file format
  • create an external data source

PolyBase’s ability to transparently parallelize loads from Azure Blob Storage will make it the fastest tool for loading data. After configuring PolyBase, you can load data directly into your SQL Data Warehouse by simply creating an external table that points to your data in storage and then mapping that data to a new table within SQL Data Warehouse.

Of course, you will need to establish a recurrent process that will extract any newly created data from your service, load them in the form of Azure Blobs and initiate the PolyBase process for importing the data again into SQL Data Warehouse. One way of doing this is by using the Azure Data Factory service. In case, you would like to follow this path you can read some good documentation on how to move data to and from Azure SQL Warehouse using Azure Data Factory.

What is the best way to load data from Square to SQL Data Warehouse? Which are the possible alternatives?

So far we just scraped the surface of what can be done with Microsoft Azure SQL Data Warehouse and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources. A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use a product like Blendo that can handle this kind of problems automatically for you.

 Blendo integrates with multiple sources or services like databases, CRM, email campaigns, analytics and more.  Quickly and safely move all your data from Recurly into SQL Data Warehouse and start generating insights from your data.