PHPackages                             seka19/basic-shopify-api - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. seka19/basic-shopify-api

ActiveLibrary[HTTP &amp; Networking](/categories/http)

seka19/basic-shopify-api
========================

A basic Shopify API wrapper with REST and GraphQL support.

v8.2.0(6y ago)09371MITPHP &gt;=7.1.0

Since Jul 6Pushed 5y agoCompare

[ Source](https://github.com/seka19/Basic-Shopify-API)[ Packagist](https://packagist.org/packages/seka19/basic-shopify-api)[ RSS](/packages/seka19-basic-shopify-api/feed)WikiDiscussions fix-logging Synced 1w ago

READMEChangelog (4)Dependencies (6)Versions (36)Used By (1)

Basic Shopify API
=================

[](#basic-shopify-api)

[![Build Status](https://camo.githubusercontent.com/07504be271d77f68d8614c479f73852bb85ea6059cfda66f33aac872b567bba6/68747470733a2f2f7472617669732d63692e6f72672f6f686d79627265772f42617369632d53686f706966792d4150492e7376673f6272616e63683d6d6173746572)](http://travis-ci.org/ohmybrew/Basic-Shopify-API)[![Coverage Status](https://camo.githubusercontent.com/70827e43243ddcbc2973a66b34735ad51defd7d491c7df2057493f2f3060bc22/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6f686d79627265772f42617369632d53686f706966792d4150492f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/ohmybrew/Basic-Shopify-API?branch=master)[![StyleCI](https://camo.githubusercontent.com/1c8b5949fbc520503b85d7cc7ea13a642f2f084457de19be532f41bca70a51c7/68747470733a2f2f7374796c6563692e696f2f7265706f732f36313030343737362f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/61004776)[![License](https://camo.githubusercontent.com/d6d80ec95b74f0fce47242a0ab8e268babb084958bccd2542d7df1950444e489/68747470733a2f2f706f7365722e707567782e6f72672f6f686d79627265772f62617369632d73686f706966792d6170692f6c6963656e7365)](https://packagist.org/packages/ohmybrew/basic-shopify-api)

A simple, tested, API wrapper for Shopify using Guzzle. It supports both the REST and GraphQL API provided by Shopify, and basic rate limiting abilities. It contains helpful methods for generating a installation URL, an authorize URL (offline and per-user), HMAC signature validation, call limits, and API requests. It works with both OAuth and private API apps.

Also supported: asynchronous requests through Guzzle's promises.

This library required PHP &gt;= 7.

Table of Contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Public API](#public-api)
        - [REST (sync)](#rest-sync)
        - [REST (async)](#rest-async)
        - [GraphQL](#graphql)
        - [Getting access (offline)](#getting-access-offline)
        - [Getting access (per-user)](#getting-access-per-user)
        - [Verifying HMAC signature](#verifying-hmac-signature)
    - [Private API](#private-api)
        - [REST](#rest)
        - [GraphQL](#graphql-1)
    - [Making requests](#making-requests)
        - [REST](#rest-1)
            - [If sync is true (regular rest call):](#if-sync-is-true-regular-rest-call)
            - [If sync is false (restAsync call):](#if-sync-is-false-restasync-call)
        - [GraphQL](#graphql-2)
    - [API Versioning](#api-versioning)
    - [Checking API limits](#checking-api-limits)
    - [Rate Limiting](#rate-limiting)
        - [Enable Rate Limiting](#enable-rate-limiting)
        - [Disabiling Rate Limiting](#disabiling-rate-limiting)
        - [Checking Rate Limiting Status](#checking-rate-limiting-status)
        - [page\_info / pagination Support](#page_info--pagination-support)
        - [Getting Timestamps](#getting-timestamps)
    - [Isolated API calls](#isolated-api-calls)
    - [Errors](#errors)
    - [Logging](#logging)
- [Documentation](#documentation)
- [LICENSE](#license)

Installation
------------

[](#installation)

The recommended way to install is [through composer](http://packagist.org).

```
$ composer require ohmybrew/basic-shopify-api

```

Usage
-----

[](#usage)

Add `use OhMyBrew\BasicShopifyAPI;` to your imports.

### Public API

[](#public-api)

This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.

#### REST (sync)

[](#rest-sync)

For REST calls, the shop domain and access token are required.

```
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');

// Now run your requests...
$resul = $api->rest(...);
```

#### REST (async)

[](#rest-async)

For REST calls, the shop domain and access token are required.

```
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');

// Now run your requests...
$promise = $api->restAsync(...);
$promise->then(function ($result) {
  // ...
});
```

#### GraphQL

[](#graphql)

For GraphQL calls, the shop domain and access token are required.

```
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('your shop here');
$api->setAccessToken('your token here');

// Now run your requests...
$api->graph(...);
```

#### Getting access (offline)

[](#getting-access-offline)

This is the default mode which returns a permanent token.

After obtaining the user's shop domain, to then direct them to the auth screen use `getAuthUrl`, as example (basic PHP):

```
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop($_SESSION['shop']);
$api->setApiKey(env('SHOPIFY_API_KEY'));
$api->setApiSecret(env('SHOPIFY_API_SECRET'));

$code = $_GET['code'];
if (!$code) {
  /**
   * No code, send user to authorize screen
   * Pass your scopes as an array for the first argument
   * Pass your redirect URI as the second argument
   */
  $redirect = $api->getAuthUrl(env('SHOPIFY_API_SCOPES'), env('SHOPIFY_API_REDIRECT_URI'));
  header("Location: {$redirect}");
  exit;
} else {
  // We now have a code, lets grab the access token
  $api->requestAndSetAccess($code);

  // Above is equiv. to:
  //
  // $access = $api->requestAccess($code);
  // $api->setAccessToken($access->access_token);
  //
  // You can use: $api->getAccessToken() and set it into the database or a cookie, etc

  // You can now make API callsn`
  $request = $api->rest('GET', '/admin/shop.json'); // or GraphQL
}
```

#### Getting access (per-user)

[](#getting-access-per-user)

You can also change the grant mode to be `per-user` as [outlined in Shopify documentation](https://help.shopify.com/en/api/getting-started/authentication/oauth/api-access-modes). This will receieve user info from the user of the app within the Shopify store. The token recieved will expire at a specific time.

```
$api = new BasicShopifyAPI();
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop($_SESSION['shop']);
$api->setApiKey(env('SHOPIFY_API_KEY'));
$api->setApiSecret(env('SHOPIFY_API_SECRET'));

$code = $_GET['code'];
if (!$code) {
  /**
   * No code, send user to authorize screen
   * Pass your scopes as an array for the first argument
   * Pass your redirect URI as the second argument
   * Pass your grant mode as the third argument
   */
  $redirect = $api->getAuthUrl(env('SHOPIFY_API_SCOPES'), env('SHOPIFY_API_REDIRECT_URI'), 'per-user');
  header("Location: {$redirect}");
  exit;
} else {
  // We now have a code, lets grab the access object
  $api->requestAndSetAccess($code);

  // Above is equiv. to:
  //
  // $access = $api->requestAccess($code);
  // $api->setAccessToken($access->access_token);
  // $api->setUser($access->associated_user)
  //
  // You can use: $api->getAccessToken() and set it into a cookie, etc
  // You can also get user details with: $api->getUser(), example: $api->getUser()->email

  // You can now make API calls
  $request = $api->rest('GET', '/admin/shop.json'); // or GraphQL
}
```

#### Verifying HMAC signature

[](#verifying-hmac-signature)

Simply pass in an array of GET params.

```
// Will return true or false if HMAC signature is good.
$valid = $api->verifyRequest($_GET);
```

### Private API

[](#private-api)

This assumes you properly have your app setup in the partner's dashboard with the correct keys and redirect URIs.

#### REST

[](#rest)

For REST (sync) calls, shop domain, API key, and API password are request

```
$api = new BasicShopifyAPI(true); // true sets it to private
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('example.myshopify.com');
$api->setApiKey('your key here');
$api->setApiPassword('your password here');

// Now run your requests...
$result = $api->rest(...);
```

#### GraphQL

[](#graphql-1)

For GraphQL calls, shop domain and API password are required.

```
$api = new BasicShopifyAPI(true); // true sets it to private
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setShop('example.myshopify.com');
$api->setApiPassword('your password here');

// Now run your requests...
$api->graph(...);
```

### Making requests

[](#making-requests)

#### REST

[](#rest-1)

Requests are made using Guzzle.

```
$api->rest(string $type, string $path, array $params = null, array $headers = [], bool $sync = true);
```

- `type` refers to GET, POST, PUT, DELETE, etc
- `path` refers to the API path, example: `/admin/products/1920902.json`
- `params` refers to an array of params you wish to pass to the path, examples: `['handle' => 'cool-coat']`
- `headers` refers to an array of custom headers you would like to optionally send with the request, example: `['X-Shopify-Test' => '123']`
- `sync` refers to if the request should be synchronous or asynchronous.

You can use the alias `restAsync` to skip setting `sync` to `false`.

##### If sync is true (regular rest call):

[](#if-sync-is-true-regular-rest-call)

The return value for the request will be an object containing:

- `response` the full Guzzle response object
- `body` the JSON decoded response body

*Note*: `request()` will alias to `rest()` as well.

##### If sync is false (restAsync call):

[](#if-sync-is-false-restasync-call)

The return value for the request will be a Guzzle promise which you can handle on your own.

The return value for the promise will be an object containing:

- `response` the full Guzzle response object
- `body` the JSON decoded response body

```
$promise = $api->restAsync(...);
$promise->then(function ($result) {
  // `response` and `body` available in `$result`.
});
```

#### GraphQL

[](#graphql-2)

Requests are made using Guzzle.

```
$api->graph(string $query, array $variables = []);
```

- `query` refers to the full GraphQL query
- `variables` refers to the variables used for the query (if any)

The return value for the request will be an object containing:

- `response` the full Guzzle response object
- `body` the JSON decoded response body
- `errors` if there was errors or not

Example query:

```
$result = $api->graph('{ shop { productz(first: 1) { edges { node { handle, id } } } } }');
echo $result->body->shop->products->edges[0]->node->handle; // test-product
```

Example mutation:

```
$result = $api->graph(
    'mutation collectionCreate($input: CollectionInput!) { collectionCreate(input: $input) { userErrors { field message } collection { id } } }',
    ['input' => ['title' => 'Test Collection']]
);
echo $result->body->collectionCreate->collection->id; // gid://shopify/Collection/63171592234
```

### API Versioning

[](#api-versioning)

This library supports [versioning the requests](https://www.shopify.com/partners/blog/api-versioning-at-shopify), example:

```
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"

// ... your code
```

You can override the versioning at anytime for specific API requests, example:

```
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04');

$api->rest('GET', '/admin/api/unstable/shop.json'); // Will ignore "2019-04" version and use "unstable" for this request
// ... your code
```

### Checking API limits

[](#checking-api-limits)

After each request is made, the API call limits are updated. To access them, simply use:

```
// Returns an array of left, made, and limit.
// Example: ['left' => 79, 'made' => 1, 'limit' => 80]
$limits = $api->getApiCalls('rest'); // or 'graph'
```

For GraphQL, additionally there will be the following values: `restoreRate`, `requestedCost`, `actualCost`.

To quickly get a value, you may pass an optional parameter to the `getApiCalls` method:

```
// As example, this will return 79
// You may pass 'left', 'made', or 'limit'
$left = $api->getApiCalls('graph', 'left'); // returns 79
// or
$left = $api->getApiCalls('graph')['left']; // returns 79
```

### Rate Limiting

[](#rate-limiting)

This library comes with a built-in basic rate limiter, disabled by default. It will sleep for *x* microseconds to ensure you do not go over the limit for calls with Shopify. On non-Plus plans, you get 1 call every 500ms (2 calls a second), for Plus plans you get 2 calls every 500ms (4 calls a second).

By default the cycle is set to 500ms, with a buffer for safety of 100ms added on.

#### Enable Rate Limiting

[](#enable-rate-limiting)

Setup your API instance as normal, with an added:

```
$api->enableRateLimiting();
```

This will turn on rate limiting with the default 500ms cycle and 100ms buffer. To change this, do the following:

```
$api->enableRateLimiting(0.25 * 1000, 0);
```

This will set the cycle to 250ms and 0ms buffer.

#### Disabiling Rate Limiting

[](#disabiling-rate-limiting)

If you've previously enabled it, you simply need to run:

```
$api->disableRateLimiting();
```

#### Checking Rate Limiting Status

[](#checking-rate-limiting-status)

```
$api->isRateLimitingEnabled();
```

#### page\_info / pagination Support

[](#page_info--pagination-support)

2019-07 API version introduced a new `Link` header which is used for pagination ([explained here](https://help.shopify.com/en/api/guides/paginated-rest-results)).

If an endpoint supports page\_info, you can use `$response->link` to grab the page\_info value to pass in your next request.

Example:

```
$response = $api->rest('GET', '/admin/products.json', ['limit' => 5]);
$link = $response->link->next; // eyJsYXN0X2lkIjo0MDkw
$link2 = $response->link->previous; // dkUIsk00wlskWKl
$response = $api->rest('GET', '/admin/products.json', ['limit' => 5, 'page_info' => $link]);
```

#### Getting Timestamps

[](#getting-timestamps)

The library will track timestamps from the previous and current (last) call. To see information on this:

```
$response = $api->rest('POST', '/admin/gift_cards.json', ['gift_cards' => ['initial_value' => 25.00]]);
print_r($response->timestamps);

/* Above will return an array of [previous call, current (last) call], example:
 * [1541119962.965, 1541119963.3121] */
```

### Isolated API calls

[](#isolated-api-calls)

You can initialize the API once and use it for multiple shops. Each instance will be contained to not pollute the others. This is useful for something like background job processing.

```
$api->withSession(string $shop, string $accessToken, Closure $closure);
```

- `shop` refers to the Shopify domain
- `accessToken` refers to the access token for the API calls
- `closure` refers to the closure to call for the session

`$this` will be binded to the current API. Example:

```
$api = new BasicShopifyAPI(true);
$api->setVersion('2019-04'); // "YYYY-MM" or "unstable"
$api->setApiKey('your key here');
$api->setApiPassword('your password here');

$api->withSession('some-shop.myshopify.com', 'token from database?', function() {
  $request = $this->rest('GET', '/admin/shop.json');
  echo $request->body->shop->name; // Some Shop
});

$api->withSession('some-shop-two.myshopify.com', 'token from database?', function() {
  $request = $this->rest('GET', '/admin/shop.json');
  echo $request->body->shop->name; // Some Shop Two
});
```

### Errors

[](#errors)

This library internally catches only 400-500 status range errors through Guzzle. You're able to check for an error of this type and get its response status code and body.

```
$call = $api->rest('GET', '/admin/non-existant-route-or-object.json');

if ($call->errors) {
  echo "Oops! {$call->status} error";
  var_dump($call->body);

  // Original exception can be accessed via `$call->exception`
  // Example, if response body was `{"error": "Not found"}`...
  /// then: `$call->body` would return "Not Found"
}
```

### Logging

[](#logging)

This library accepts a PSR-compatible logger.

```
$api->setLogger(... your logger instance ...);
```

Documentation
-------------

[](#documentation)

Code documentation is [available here](https://ohmybrew.com/Basic-Shopify-API) from phpDocumentor via `phpdoc -d src -t doc`.

LICENSE
-------

[](#license)

This project is released under the MIT [license](https://github.com/ohmybrew/Basic-Shopify-API/blob/master/LICENSE).

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 97.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~46 days

Recently: every ~81 days

Total

30

Last Release

1895d ago

Major Versions

v3.0.3 → v4.0.02018-11-02

v4.0.2 → v5.0.02019-02-26

v5.4.0 → v6.0.02019-10-15

v6.1.3 → v7.0.02020-03-02

v7.0.0 → v8.0.02020-03-17

PHP version history (3 changes)v1.0.0PHP &gt;=7.0.0

v8.0.0PHP &gt;=7.1.0

v6.1.5PHP &gt;=7.2.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/b086717a3f91192a67a4d5ddad6154ec01a7120cf606d7b5e385c4167e0146b7?d=identicon)[Alexey Sinkevich](/maintainers/Alexey%20Sinkevich)

---

Top Contributors

[![gnikyt](https://avatars.githubusercontent.com/u/2420282?v=4)](https://github.com/gnikyt "gnikyt (108 commits)")[![seka19](https://avatars.githubusercontent.com/u/2714877?v=4)](https://github.com/seka19 "seka19 (2 commits)")[![jaffacake](https://avatars.githubusercontent.com/u/2633927?v=4)](https://github.com/jaffacake "jaffacake (1 commits)")

---

Tags

apirestgraphqlshopify

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/seka19-basic-shopify-api/health.svg)

```
[![Health](https://phpackages.com/badges/seka19-basic-shopify-api/health.svg)](https://phpackages.com/packages/seka19-basic-shopify-api)
```

###  Alternatives

[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[gnikyt/basic-shopify-api

A basic Shopify API wrapper with REST and GraphQL support, powered by Guzzle.

245514.8k5](/packages/gnikyt-basic-shopify-api)[api-platform/metadata

API Resource-oriented metadata attributes and factories

243.5M96](/packages/api-platform-metadata)[huaweicloud/huaweicloud-sdk-php

Huawei Cloud SDK for PHP

1829.2k2](/packages/huaweicloud-huaweicloud-sdk-php)[api-platform/laravel

API Platform support for Laravel

59126.4k6](/packages/api-platform-laravel)[quickbooks/payments-sdk

The Official PHP SDK for QuickBooks Online Payments API

2758.2k2](/packages/quickbooks-payments-sdk)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
