How to build a generic Shopify API integration

Reading time: 13 minutes

Shopify exposes a REST API for 3rd party applications to integrate with. This guide will cover connecting to the Shopify Admin API with Linx using API Keys in conjunction with the HTTP Basic scheme authentication and some request examples.

You can use webhook subscriptions to receive notifications in a Linx Application about particular events in a Shopify store. After you’ve subscribed to a webhook, you can let your app execute code immediately after specific events occur in shops that have your app installed instead of having to make API calls periodically to check their status. For example, you can rely on webhooks to trigger an action in your app when a customer creates a cart, or when a merchant creates a new product in their Shopify admin. By using webhooks subscriptions you can make fewer API calls overall, ensuring that your apps are more efficient and updated quickly.

More details on Shopify Webhooks can be found here.

 

Integrating Shopify

Creating a Linx application and connecting it to Shopify involves the following steps:

  1. Creating a private app and generating an API Key.
  2. Configuring Linx to authenticate.
  3. Customizing requests.

Resources

Create a Private App

To access and modify your Shopify resources using Linx, you first need to register a private app with Shopify. This will then generate the relevant API Key, which must be used when making requests from Linx.

To register a private app, log in to your Shopify dashboard, navigate to the Apps dashboard (Left menu > Apps). Click on the Manage private apps link, which will open your private app dashboard. If you haven’t already, enable private app development. Once enabled, click on the Create New App button. Give your app a name such as “LinxApp” and complete the contact details.

In the Admin API section, show the inactive permissions of the app (by default, none are selected).

Shopify Admin API

Select the relevant permissions; these will be the access scope of the linked application. In this guide, we will be reading and writing data to the Customers and Products resources; you can modify the access permissions later.

Scroll to the bottom of the page and click Save, then create app. Your app authentication details will be displayed:

  • API Key: Key used as the username in the HTTP Basic authentication of the request and the request URL.
  • Password: Used as the password in the HTTP Basic authentication of the request.
  • Base URL: Example of the base URL to use when requesting to your instance.

App Authentication Details

Connect to Shopify

To connect to the Shopify API, drag a CallRESTEndpoint onto a user-defined function and configure the relevant properties described below:

Configuring Properties

A best practice is creating $.Settings values for all the needed constants in our application which may change later. By making these $.Settings values, we can reference them throughout our Linx application and update them in a single place to have application-wide effects.

Create new  $.Settings values and add your authentication details.

New Settings Values

 

Creating the Base URL

When making requests to the Shopify API, you need to build up the base URL of each request like below:

https://{apikey}:{password}@{hostname}/admin/api/{version}/{resource}.json

In Linx, this can be done via the use of expressions or using dynamic settings.

In the provided template application, the URL is built up via a dynamic setting expression. The $.Settings.shopify_baseUrl has the structure of the URL and placeholder references that reference other $.Settings values. At runtime, the values referenced in the other settings will be concatenated into a base URL.

https://{shopify_apiKey}:{shopify_password}@{shopify_shopName}.myshopify.com/admin/api/{shopify_api_version}

At runtime, the value of $.Settings.shopify_baseUrl will be:

Runtime Value

When a request is made, the base URL can just be based on this $.Setting value. The particular resource path can then be added in each request using an expression like below:

= $.Settings.shopify_baseURI + "/products" + ".json"

Expression Editor Demo

Authentication

Private applications authenticate with Shopify through basic HTTP authentication.This can be accomplished by completing the CallRESTEndpoint Authentication properties like below:

Properties (CallRESTEndpoint)

 

Querying and Modifying Data

The below examples are demonstrations of interacting with the Shopify API and Linx.

Querying Customers

The below example deals with the Customer object. The function makes a GET request to the /customers endpoint, which returns a JSON string response containing a list of all the customers. The response is then imported by Linx into a custom-type object which is then used to structure the response body.

First, create a new Function and give it the name of GetCustomers. Drag a CallRESTEndpoint onto the GetCustomers canvas.

Complete the Basic authentication details by referencing the API Key and password from earlier. Then configure the URL to be like the below (using the expression editor):

= $.Settings.shopify_baseURI + "/customers" + ".json"

Configuring Basic Authentication Details

Right-click on the CallRESTEndpoint and Add breakpoint and Enable logging; this will expose the runtime values of the objects in scope. Next, debug the GetCustomers function and take note of the response body returned in the Debug Values panel.

The response body is a JSON string

[et_pb_dmb_code_snippet code=”ewogICJjdXN0b21lcnMiOiBbCiAgICB7CiAgICAgICJpZCI6IDIwNzExOTU1MSwKICAgICAgImVtYWlsIjogImJvYi5ub3JtYW5AaG9zdG1haWwuY29tIiwKICAgICAgImFjY2VwdHNfbWFya2V0aW5nIjogZmFsc2UsCiAgICAgICJjcmVhdGVkX2F0IjogIjIwMjEtMDEtMDFUMTQ6NDY6NDgtMDU6MDAiLAogICAgICAidXBkYXRlZF9hdCI6ICIyMDIxLTAxLTAxVDE0OjQ2OjQ4LTA1OjAwIiwKICAgICAgImZpcnN0X25hbWUiOiAiQm9iIiwKICAgICAgImxhc3RfbmFtZSI6ICJOb3JtYW4iLAogICAgICAib3JkZXJzX2NvdW50IjogMSwKICAgICAgInN0YXRlIjogImRpc2FibGVkIiwKICAgICAgInRvdGFsX3NwZW50IjogIjE5OS42NSIsCiAgICAgICJsYXN0X29yZGVyX2lkIjogNDUwNzg5NDY5LAogICAgICAibm90ZSI6IG51bGwsCiAgICAgICJ2ZXJpZmllZF9lbWFpbCI6IHRydWUsCiAgICAgICJtdWx0aXBhc3NfaWRlbnRpZmllciI6IG51bGwsCiAgICAgICJ0YXhfZXhlbXB0IjogZmFsc2UsCiAgICAgICJwaG9uZSI6ICIrMTYxMzYxMjA3MDciLAogICAgICAidGFncyI6ICIiLAogICAgICAibGFzdF9vcmRlcl9uYW1lIjogIiMxMDAxIiwKICAgICAgImN1cnJlbmN5IjogIlVTRCIsCiAgICAgICJhZGRyZXNzZXMiOiBbCiAgICAgICAgewogICAgICAgICAgImlkIjogMjA3MTE5NTUxLAogICAgICAgICAgImN1c3RvbWVyX2lkIjogMjA3MTE5NTUxLAogICAgICAgICAgImZpcnN0X25hbWUiOiBudWxsLAogICAgICAgICAgImxhc3RfbmFtZSI6IG51bGwsCiAgICAgICAgICAiY29tcGFueSI6IG51bGwsCiAgICAgICAgICAiYWRkcmVzczEiOiAiQ2hlc3RudXQgU3RyZWV0IDkyIiwKICAgICAgICAgICJhZGRyZXNzMiI6ICIiLAogICAgICAgICAgImNpdHkiOiAiTG91aXN2aWxsZSIsCiAgICAgICAgICAicHJvdmluY2UiOiAiS2VudHVja3kiLAogICAgICAgICAgImNvdW50cnkiOiAiVW5pdGVkIFN0YXRlcyIsCiAgICAgICAgICAiemlwIjogIjQwMjAyIiwKICAgICAgICAgICJwaG9uZSI6ICI1NTUtNjI1LTExOTkiLAogICAgICAgICAgIm5hbWUiOiAiIiwKICAgICAgICAgICJwcm92aW5jZV9jb2RlIjogIktZIiwKICAgICAgICAgICJjb3VudHJ5X2NvZGUiOiAiVVMiLAogICAgICAgICAgImNvdW50cnlfbmFtZSI6ICJVbml0ZWQgU3RhdGVzIiwKICAgICAgICAgICJkZWZhdWx0IjogdHJ1ZQogICAgICAgIH0KICAgICAgXSwKICAgICAgImFjY2VwdHNfbWFya2V0aW5nX3VwZGF0ZWRfYXQiOiAiMjAwNS0wNi0xMlQxMTo1NzoxMS0wNDowMCIsCiAgICAgICJtYXJrZXRpbmdfb3B0X2luX2xldmVsIjogbnVsbCwKICAgICAgInRheF9leGVtcHRpb25zIjogW10sCiAgICAgICJhZG1pbl9ncmFwaHFsX2FwaV9pZCI6ICJnaWQ6Ly9zaG9waWZ5L0N1c3RvbWVyLzIwNzExOTU1MSIsCiAgICAgICJkZWZhdWx0X2FkZHJlc3MiOiB7CiAgICAgICAgImlkIjogMjA3MTE5NTUxLAogICAgICAgICJjdXN0b21lcl9pZCI6IDIwNzExOTU1MSwKICAgICAgICAiZmlyc3RfbmFtZSI6IG51bGwsCiAgICAgICAgImxhc3RfbmFtZSI6IG51bGwsCiAgICAgICAgImNvbXBhbnkiOiBudWxsLAogICAgICAgICJhZGRyZXNzMSI6ICJDaGVzdG51dCBTdHJlZXQgOTIiLAogICAgICAgICJhZGRyZXNzMiI6ICIiLAogICAgICAgICJjaXR5IjogIkxvdWlzdmlsbGUiLAogICAgICAgICJwcm92aW5jZSI6ICJLZW50dWNreSIsCiAgICAgICAgImNvdW50cnkiOiAiVW5pdGVkIFN0YXRlcyIsCiAgICAgICAgInppcCI6ICI0MDIwMiIsCiAgICAgICAgInBob25lIjogIjU1NS02MjUtMTE5OSIsCiAgICAgICAgIm5hbWUiOiAiIiwKICAgICAgICAicHJvdmluY2VfY29kZSI6ICJLWSIsCiAgICAgICAgImNvdW50cnlfY29kZSI6ICJVUyIsCiAgICAgICAgImNvdW50cnlfbmFtZSI6ICJVbml0ZWQgU3RhdGVzIiwKICAgICAgICAiZGVmYXVsdCI6IHRydWUKICAgICAgfQogICAgfQogIF0KfQ==” _builder_version=”4.14.8″ _module_preset=”default” global_colors_info=”{}” _i=”1″ _address=”0.5.0.1″ /]

This object contains a parent object containing a ‘customers’ child object which contains a list of objects which contain the fields related to the product we are creating.

Debugging

Next, we need to create a custom data type so Linx will know what to parse the response body into; this will allow us to work with individual field values in subsequent operations. Copy the value of CallRESTEndpoint.ResponseBody from the Debug values panel or from the API documentation. Import the copied JSON string as a new Type. This will create a data object or type for you to structure the response data into. Configure the CallRESTEndpointFNC to have an Output type of the newly imported user-defined type.

Adding Products

In the below example, a Product will be added to Shopify. This will involve making a POST request to the /products endpoint. A JSON structure containing details of a Product will be submitted as the request body.

Example request:

[et_pb_dmb_code_snippet code=”UE9TVCAvYWRtaW4vYXBpLzIwMjEtMDEvcHJvZHVjdHMuanNvbgp7CiAgInByb2R1Y3QiOiB7CiAgICAidGl0bGUiOiAiQnVydG9uIEN1c3RvbSBGcmVlc3R5bGUgMTUxIiwKICAgICJib2R5X2h0bWwiOiAiPHN0cm9uZz5Hb29kIHNub3dib2FyZCE8L3N0cm9uZz4iLAogICAgInZlbmRvciI6ICJCdXJ0b24iLAogICAgInByb2R1Y3RfdHlwZSI6ICJTbm93Ym9hcmQiLAogICAgInRhZ3MiOiBbCiAgICAgICJCYXJuZXMgJiBOb2JsZSIsCiAgICAgICJKb2huJ3MgRmF2IiwKICAgICAgIkJpZyBBaXIiCiAgICBdCiAgfQp9″ _builder_version=”4.14.8″ _module_preset=”default” global_colors_info=”{}” _i=”1″ _address=”0.6.0.1″ /]

In the below example, we are going to create a user-defined function that will take in a product’s details as the input parameters. These details will then be assigned to the user-defined type and submitted in a request.

First, in order to submit a valid JSON data structured like the above, you need to create a user-defined Type that matches the fields in the product object, either manually or by importing the provided JSON example as a new Type with the name of ‘newProduct.’ This will create two types, a parent newProduct which holds a child newProduct_product.

This user-defined type is then available to reference across your Linx application to structure data. Next, create a user-defined function that will take in data in this format and then submit this data to the Shopify API.

To do this, create a new user-defined function with the name of ‘CreateProduct,’ with an input parameter with the name of product and the Type of product. When this CreateProduct user-defined function is called from somewhere else in the application, the structure of the child object will be able to be used to pass data through. The parent product is not needed as an input parameter as it just creates another level; we can assign it later inside the function.

Creating Product

For the request body, submit a newProduct type from earlier in a JSON format. Currently, the CreateProduct function takes in an input parameter of the type newProduct_product. However, for the request to succeed, we need to submit the entire structure of newProduct. To do this, we need to assign the input parameter productDetails details as the child newProduct_product of newProduct. To do this, drag an instance of the newProduct type from the Solution Explorer onto the CreateProduct canvas. Expand its field values properties and in the product field, reference the input parameter productDetails.

Values Editor Demo

Now to make the request using the newProduct type. Inside the CreateProduct function, drag a CallRESTEndpoint onto the canvas. Configure the authentication credentials and the request URL.

Next, submit the local instance of newProduct as the request body. Change the Method of the request to POST and set the Body format as JSON. For the Body property, reference the local newProduct.

Now we can make a request to test it out.

Debug the CreateProduct function and add debug input parameter values for the new product. Add some breakpoints and logging to see how the input data is assigned to a local instance of the newProduct type and then submitted as a JSON body with a successful 201 - Created response being returned. A new product should be visible on the Shopify products dashboard:

You can then import the response as a new user-defined type and set it as the Output type of the CallRESTEndpoint function. When a new product is now created, the newly added details and some metadata, including the new id are then available in subsequent functions such as logging them to a database with an ExecuteSQL function or writing to a file using a TextFileWrite function.

More prebuilt Integration samples and templates

Sign up to our Newsletter