Skip to content

Demonstration App

Jarrod Swift edited this page Jan 17, 2022 · 7 revisions

A basic setup for using the BigCommerce V3 API client.

If you want documentation about specific endpoints, view the BigCommerce API Reference. If you're looking for the documentation for this library, see the client documentation.

Before You Get Started

Prerequisites for running this tutorial:

  1. You'll need PHP available (at least PHP 7.4)
  2. composer

Creating Your Project

Create the directory you want to store your project in, mine is ~/test-client and then run composer init to setup your project.

jarrod.swift:~/ $ mkdir test-client
jarrod.swift:~/ $ cd test-client/
jarrod.swift:~/test-client $ composer init

You will be prompted to set some values for your project. Choose some that make sense, but they can always be changed later by editing composer.json.

Package name (<vendor>/<name>) [root/app]: jarrod/bc-test-client
Description []: A demonstration client for BigCommerce
Author [, n to skip]: n
Minimum Stability []: 
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: 

After setting these composer will ask you if you wish to define your dependencies interactively, enter yes. Then search for aligent/bigcommerce and select the package.

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? yes
Search for a package: aligent/bigcommerce

Found 1 packages matching aligent/bigcommerce

   [0] aligent/bigcommerce-api-client 

Enter package # to add, or the complete package name if it is not listed: 0
Enter the version constraint to require (or leave blank to use the latest version): 
Using version ^1.7 for aligent/bigcommerce-api-client
Search for a package: 
Would you like to define your dev dependencies (require-dev) interactively [yes]? no
Add PSR-4 autoload mapping? Maps namespace "Jarrod\BcTestClient" to the entered relative path. [src/, n to skip]: src/

{
    "name": "jarrod/bc-test-client",
    "description": "A demonstration client for BigCommerce",
    "type": "project",
    "require": {
        "aligent/bigcommerce-api-client": "^1.7"
    },
    "autoload": {
        "psr-4": {
            "Jarrod\\BcTestClient\\": "src/"
        }
    }
}

Do you confirm generation [yes]? yes

Once you've confirmed this, and installed the dependencies (either now or later with composer install) you're ready to write some code.

Using the API Client

All code files should be in the folder specified by the PSR autoload statement in composer.json, in our case - src/. Lets make a file called products.php in that folder.

Lets start by checking that everything is working ok.

src/products.php

<?php
include __DIR__.'/../vendor/autoload.php';

echo "Hello World", PHP_EOL;

Then run this file from the terminal, and it should print "Hello World"

jarrod.swift:~/test-client $ php src/products.php 
Hello World

So if this has worked, now we can go ahead and use the API Client. Update the products.php file to match the following:

<?php
include __DIR__.'/../vendor/autoload.php';

$client = new BigCommerce\ApiV3\Client(
    $_ENV['SOURCE_HASH'], $_ENV['SOURCE_CLIENT_ID'], $_ENV['SOURCE_ACCESS_TOKEN']
);

$products = $client->catalog()->products()->getAll()->getProducts();

echo "There were ", count($products), " fetched!", PHP_EOL;

Run this again, but this time we'll see an error!

Warning: Undefined array key "SOURCE_HASH" in /app/src/products.php on line 5

Warning: Undefined array key "SOURCE_CLIENT_ID" in /app/src/products.php on line 5

Warning: Undefined array key "SOURCE_ACCESS_TOKEN" in /app/src/products.php on line 5

Fatal error: Uncaught TypeError: BigCommerce\ApiV3\BaseApiClient::__construct(): Argument #1 ($storeHash) must be of type string, null given

Looks like we need to define our environment variables to access our store!

Define Store Credentials

We don't want to store our credentials in a code repo, or hard-code them so it's hard to update in the future, so lets add a library to help us manage this.

Clone this wiki locally