PHP code example of anytech / lightspeed-x-series-api
1. Go to this page and download the library: Download anytech/lightspeed-x-series-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
anytech / lightspeed-x-series-api example snippets
use LightspeedXSeries\LightspeedAPI;
$api = new LightspeedAPI(
'https://mystore.retail.lightspeed.app',
'Bearer',
'your-access-token'
);
// Get all products
$products = $api->apiRequest('products', 'get');
foreach ($products->data as $product) {
echo $product->name . "\n";
}
// Get a single product
$product = $api->apiRequest('products/abc-123-uuid', 'get');
// Create a customer
$customer = $api->apiRequest('customers', 'post', [
'first_name' => 'John',
'last_name' => 'Doe',
'email' => '[email protected]',
]);
// Update a product
$api->apiRequest('products/abc-123-uuid', 'put', [
'name' => 'Updated Product Name',
]);
// Delete a product
$api->apiRequest('products/abc-123-uuid', 'delete');
use LightspeedXSeries\LightspeedAPI;
// Set the default version for your entire application
// Do this once at application bootstrap/initialization
LightspeedAPI::setDefaultVersion('2026-01');
// All subsequent API instances will use this version
$api = new LightspeedAPI($url, 'Bearer', $token);
$products = $api->apiRequest('products', 'get'); // Uses 2026-01
use LightspeedXSeries\LightspeedAPI;
// Option 1: Set global default (recommended for site-wide configuration)
LightspeedAPI::setDefaultVersion('2026-01');
// Option 2: Set version per instance via constructor
$api = new LightspeedAPI($url, 'Bearer', $token, '2026-04');
// Option 3: Set version on existing instance
$api = new LightspeedAPI($url, 'Bearer', $token);
$api->setVersion('2026-04');
// Option 4: Override version for a single request
$api->apiRequest('products', 'get', null, '2026-04');
// Check current versions
echo LightspeedAPI::getDefaultVersion(); // Global default
echo $api->getVersion(); // Instance version (or default if not set)
// Before (using 2026-01)
LightspeedAPI::setDefaultVersion('2026-01');
// After (upgrading to 2026-04)
LightspeedAPI::setDefaultVersion('2026-04');
$api->apiRequest(
string $endpoint, // The endpoint path (e.g., 'products', 'customers/123')
string $method, // HTTP method: 'get', 'post', 'put', 'delete'
?array $data = null, // Request body (POST/PUT) or query params (GET)
?string $version = null // Version override (YYYY-MM format, optional)
): object
use LightspeedXSeries\LightspeedOAuth;
$oauth = new LightspeedOAuth(
'your-client-id',
'your-client-secret',
'mystore', // Your store prefix
'https://yoursite.com/oauth/callback', // Redirect URI
['products:read', 'products:write'] // Scopes (
$auth = $oauth->getAuthorizationUrl();
// Store state in session for CSRF protection
$_SESSION['oauth_state'] = $auth['state'];
// Redirect user to Lightspeed
header('Location: ' . $auth['url']);
// Verify state matches
if ($_GET['state'] !== $_SESSION['oauth_state']) {
throw new Exception('Invalid state');
}
// Exchange code for tokens
$tokens = $oauth->exchangeCodeForToken($_GET['code']);
// Store tokens securely
$accessToken = $tokens['access_token'];
$refreshToken = $tokens['refresh_token'];
$expiresIn = $tokens['expires_in'];