Integrate 3rd party systems with Shopify using Lumen

Francois BouyerPosted July 15, 2015

Do you remember my last post where I showed you how to create an API using Slim? In the “Possibilities” section, I tried to expand its usage to interact with Magento or WordPress. Well, I recently had to integrate a 3rd party system with Shopify, an E-Commerce platform.

In this post, I’ll show you how to interact with the Shopify API in order to add products to your store, get your latest orders and many more fun activities!

This time we won’t use Slim. On one of my recent projects, I decided to use Laravel instead of my beloved Yii as my MVC framework. I have to say that the Laravel team did a great job. That’s why when I had to build my new API, I made the switch to Laravel’s Lumen.

Lumen is a micro-framework version of Laravel. It uses the exact same core but stripped down of extra features that would not necessarily be needed to build services and APIs. It focuses on speed while still using Laravel’s features like Eloquent, caching, queues, validation, routing, middleware.

“phpish/shopify” is the package we’ll use to call the Shopify API. It’s a very simple Shopify API client that will save us a lot of time and lines of code.

3) Setup Shopify API Client credentials in Lumen

The Shopify API client will need the set of credentials to know which store to interact with and get permissions.

Make a copy of “.env.example” and name it “.env”

In “.env” change the values of:
SHOPIFY_SHOP, this is the URL of your Shopify shop
SHOPIFY_API_KEY, you will find this on the Shopify private app page
SHOPIFY_PASSWORD, you will find this on the Shopify private app page

And that’s about it!

4) Create your first product

To make it easier, I created a test route/controller with a dummy product ready to be pushed to your store.

This array contains our product and its variants data. I will assume that you know Shopify’s product architecture, if not, it is explained in the Shopify API documentation. I recommend going through this documentation to discover all the endpoints available and their parameters.

In the above array, we have a T-Shirt product that has 2 variants. To explain it in “Magento” terms, we have a configurable product that has two configurable attributes; “Size” and “Color”. The configurable products have 2 simple products attached, one “Small” and “Red”, the other “Large” and “Blue”.

And finally:

// Make a POST call to the Products endpoint of the Shopify API
// with our product data automatically JSON encoded.
// It will return a JSON response of the product created or an error
$product = $shopify('POST /admin/products.json', [], $productData);
return $product;

Now to create our product just go to:
http://path_to_lumen/create-products

I recommend creating a VirtualHost in Apache for this project that will serve the directory lumen-shopify-api/public otherwise you might get a 404 error.

If everything went well you should see a response like this in your browser:

Congrats! You created your first product!

Now go to your store, your product should look like this:

5) And now what?

Okay, you created your first product through the Shopify API, through your own API. Wait, what? Yeah, it doesn’t sound very efficient. Well, now that you’re a little bit more familiar with our environment, let’s unleash its full potential. I mostly use this project as a “middle-man” between systems.

Example: you need to sync your Shopify orders/sales with a 3rd party system or a custom project. Let’s say, every 5 minutes, the 3rd party system makes a call to your API to get new sales from Shopify.

First I would create a new controller called SaleController with a method “list”. If you go to the Shopify API documentation, you’ll find the sale endpoint, the rest of the code is easy-peasy.

// Make a GET call to the Orders endpoint which will
// return an array of sales
$sales = $shopify('GET /admin/orders.json', [], []);
// Loop through the array and make any data transformation necessary to comply with your 3rd party system requirement
$sales = /* code here */
return $sales;

In the file “app/Http/routes.php“, after the “create-products” route add the new sale route: