Feeling stuck with Segment? Say 👋 to RudderStack.

SVG
Log in

How to load data from the Square to Google BigQuery

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 applications for in-store or mobile payments.
  • Item & inventory management API.
  • Sales reporting & Analytics API.
  • Employee management API.

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

All the APIs that Square exposes are web APIs. As Web APIs following the RESTful architecture principles, they can be accessed through HTTP. As a RESTful API, interacting with it can be achieved using tools like CURL or Postman or by using HTTP clients for your favorite 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 favorite framework or language.

Square API Authentication

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 to handle our requests and responses properly.

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 many endpoints are related to these tasks. However, we do not need to access these endpoints for analytic purposes. In a payment system, what we need to track and use as data for further analysis is related to what is happening in our system.

For example, we would like to track when a new payment has happened, when something went wrong with a transaction, etc. We need to monitor the system for specific events and make sure that we store these events in a data warehouse solution like Google BigQuery for further analysis.

Square Connect API, offers a notification through webhooks 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. For example, 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 merchant account by sending the following curl request to the Update Webhooks endpoint (providing your access token and location ID where indicated):
JAVASCRIPT
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 to 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:

JAVASCRIPT
{
"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:

JAVASCRIPT
{
"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 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. 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 prepare my data to be sent from Square to Google BigQuery?

Before you load your data into BigQuery, you should make sure that it is presented in a format supported by it. For example, if the API you pull data from returns XML, you have to first transform it into a serialization BigQuery understands.

Currently, two data formats are supported:

  • CSV, and
  • JSON

You also need to make sure that the data types you are using are the ones supported by BigQuery, which are the following:

  • STRING
  • INTEGER
  • FLOAT
  • BOOLEAN
  • RECORD
  • TIMESTAMP

For more information, please check the Preparing Data for BigQuery page on the documentation.

Load Data from Square to Google BigQuery

If you want to load data from Square to Google BigQuery, you have to use one of the following supported data sources.

  1. Google Cloud Storage
  2. Sent data directly to BigQuery with a POST request
  3. Google Cloud Datastore Backup
  4. Streaming insert
  5. App Engine log files
  6. Cloud Storage logs

From the above list of sources, 5 and 6 are not applicable in our case.

For Google Cloud Storage, you first have to load your data into it, there are a few options on how to do this, for example, you can use the console directly as it is described here and does not forget to follow the best practices.

Another option is to post your data through the JSON API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. In its simplest case it’s just a matter of one HTTP POST request using a tool like CURL or Postman.

It should look like the following example.

JAVASCRIPT
POST /upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject HTTP/1.1
Host: www.googleapis.com
Content-Type: application/text
Content-Length: number_of_bytes_in_file
Authorization: Bearer your_auth_token
your Square data

and if everything went ok, you should get something like the following as a response from the server:

JAVASCRIPT
HTTP/1.1 200
Content-Type: application/json
{
"name": "myObject"
}

Working with Curl or Postman is good only for testing. If you would like to automate loading your data into Google Bigquery, you should write some code to send your data to Google Cloud Storage.

In case you are developing on the Google App Engine, you can use the library that is available for the languages that are supported by it:

If you are using one of the above languages and not coding for the Google App Engine, you can access Cloud Storage from your environment. However, interacting with such a feature-rich product like Google Cloud Storage can become quite complicated depending on your use case. For more details on the different options that exist, you can check Google Cloud Storage documentation.

If you are looking for a less engaged and more neutral way of using Cloud Storage, you can consider a solution like RudderStack.

After loading your data into Google Cloud Storage, you have to create a Load Job for BigQuery to load the data into it. This Job should point to the source data in Cloud Storage that has to be imported. This happens by providing source URIs that point to the appropriate objects.

The previous method used a POST request to the Google Cloud Storage API to store the data and then load it into BigQuery. Another way to go is to direct HTTP POST request to BigQuery with the data you would like to query. This approach is similar to how we loaded the data to Google Cloud Storage through the JSON API, but it uses the appropriate end-points of BigQuery to load the data directly. The way to interact with it is quite similar, for more information can be found on the Google BigQuery API Reference and on the page that describes how to load data into BigQuery using POST. You can interact with it using the HTTP client library of the language or framework of your choice. A few options are:

[@portabletext/react] Unknown block type "aboutNodeBlock", specify a component for it in the `components.types` prop
[@portabletext/react] Unknown block type "aboutNodeBlock", specify a component for it in the `components.types` prop

The best way to load data from Square to Google BigQuery and possible alternatives

So far, we just scraped the surface of what can be done with Google BigQuery 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 your use case requirements. Things can get even more complicated if you want to integrate data coming from different sources.

Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use a product like RudderStack that can automatically handle this kind of problem for you.

RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more.

Sign Up For Free And Start Sending Data

Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.