Vincenzo Raco
My Developing Blog

My Developing Blog

How to consume Shopify Custom App API from Laravel

Vincenzo Raco's photo
Vincenzo Raco
·Jun 2, 2022·

2 min read

Hi, today I'm going to show you how to setup and consume Shopify API from a Custom App created in the Shopify admin page.

Shopify already provides a PHP library ( https://github.com/Shopify/shopify-php-api) that helps to authorize your application using OAuth.

Since we're bypassing the OAuth process and we're able to authenticate through our Access Token that we can get from our API credentials tab inside our custom app and it should start with shpat_.

We will also need the API key that is also accessible from the same tab.

We need to store those keys in our .env file like this:

SHOPIFY_STORE_URL=yourstore.myshopify.com
SHOPIFY_API_VERSION=2022-04
SHOPIFY_ADMIN_API_KEY=***********************
SHOPIFY_ADMIN_API_TOKEN=shpat_*******************

We then need to create a new config file called shopify.php in our config folder and the file should contain the following:

<?php

return [
    'store_url' => env('SHOPIFY_STORE_URL'),
    'api'       => [
        'version' => env('SHOPIFY_API_VERSION', '2022-04'),
        'key'     => env('SHOPIFY_ADMIN_API_KEY'),
        'token'   => env('SHOPIFY_ADMIN_API_TOKEN')
    ],
];

We then need to create a Class that conforms with the Shopify SessionStorage, this class has no use to us in this configuration but it's required by the library. You can create this Class anywhere you want.

ShopifySessionStorage.php

<?php

namespace App\Actions\Shared;

use Shopify\Auth\Session;
use Shopify\Auth\SessionStorage;

class ShopifySessionStorage implements SessionStorage
{
    public function storeSession( Session $session ): bool
    {
        return true;
    }

    public function loadSession( string $sessionId ): ?Session
    {
        return null;
    }

    public function deleteSession( string $sessionId ): bool
    {
        return true;
    }
}

We then need to create a ServiceProvider using the following command:

php artisan make:provider ShopifyServiceProvider

And in the boot() method will need to type the following code:

Context::initialize(
    config('shopify.api.key'),
    config('shopify.api.token'),
    [ '*' ],
    config('shopify.store_url'),
    new ShopifySessionStorage(),
    config('shopify.api.version'),
    false,
    true,
);

Add the ServiceProvider class at the end of the providers array in config/app.php:

App\Providers\ShopifyServiceProvider::class,

We now need to create a Class that we need to use anytime we want to perform our API calls. I called it Shopify but you can name it how you prefer. It goes without saying that you can place this class anywhere you want.

Shopify.php

use Shopify\Clients\Graphql;
use Shopify\Clients\Rest;

class Shopify
{
    public static function graphql(): Graphql
    {
        return new Graphql(self::getStoreUrl());
    }

    public static function rest(): Rest
    {
        return new Rest(self::getStoreUrl());
    }

    private static function getStoreUrl(): string
    {
        return config('shopify.store_url');
    }
}

That's all there is to it. You can now call Shopify::graphql()->query($queryString); or Shopify::rest()->get('products'); in your application.

If you have any questions, please leave a comment below.

 
Share this