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

// With pagination
$products = $api->apiRequest('products', 'get', [
    'page_size' => 100,
    'after' => 'cursor-value',
]);

// With filters
$customers = $api->apiRequest('customers', 'get', [
    'email' => '[email protected]',
]);

$customer = $api->apiRequest('customers', 'post', [
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => '[email protected]',
]);

$api->apiRequest('customers/abc-123', 'put', [
    'phone' => '+1234567890',
]);

$api->apiRequest('customers/abc-123', 'delete');

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'];

$newTokens = $oauth->refreshToken($refreshToken);

$accessToken = $newTokens['access_token'];
$refreshToken = $newTokens['refresh_token'];

// List products with pagination
$products = $api->apiRequest('products', 'get', ['page_size' => 100]);

// Get single product
$product = $api->apiRequest('products/abc-123', 'get');

// Create product
$product = $api->apiRequest('products', 'post', [
    'name' => 'New Product',
    'sku' => 'SKU001',
    'retail_price' => 99.99,
]);

// Update product
$api->apiRequest('products/abc-123', 'put', [
    'common' => [
        'name' => 'Updated Name',
        'description' => 'New description',
    ]
]);

// Delete product
$api->apiRequest('products/abc-123', 'delete');

// Upload image
$api->apiRequest('products/abc-123/images', 'post', [
    'url' => 'https://example.com/image.jpg',
]);

// Set image position
$api->apiRequest('product_images/abc-123', 'put', ['position' => 1]);

// Delete image
$api->apiRequest('product_images/abc-123', 'delete');

// Search by email
$customers = $api->apiRequest('customers', 'get', [
    'email' => '[email protected]',
]);

// Create customer
$customer = $api->apiRequest('customers', 'post', [
    'first_name' => 'John',
    'last_name' => 'Doe',
    'email' => '[email protected]',
    'phone' => '+1234567890',
]);

// Update customer
$api->apiRequest('customers/abc-123', 'put', [
    'phone' => '+0987654321',
]);

// Add customers to group
$api->apiRequest('customer_groups/abc-123/customers', 'post', [
    'customer_ids' => ['cust-1', 'cust-2', 'cust-3'],
]);

// Bulk update categories
$api->apiRequest('product_categories/bulk', 'post', [
    'categories' => [
        ['id' => 'cat-1', 'name' => 'Updated Name 1'],
        ['id' => 'cat-2', 'name' => 'Updated Name 2'],
    ]
]);

// Create inventory adjustment
$api->apiRequest('inventory', 'post', [
    'product_id' => 'prod-123',
    'outlet_id' => 'outlet-456',
    'adjustment' => 10,
]);

// Get sales with filters
$sales = $api->apiRequest('sales', 'get', [
    'status' => 'CLOSED',
    'page_size' => 50,
]);

// Fulfill entire sale
$api->apiRequest('fulfillments/fulfill', 'post', [
    'sale_id' => 'sale-123',
]);

// Update fulfillment status
$api->apiRequest('fulfillments/abc-123', 'put', [
    'status' => 'SHIPPED',
    'tracking_number' => 'TRACK123',
]);

// Create gift card
$api->apiRequest('gift_cards', 'post', [
    'number' => '1234567890',
    'initial_balance' => 100.00,
]);

// Redeem gift card
$api->apiRequest('gift_cards/1234567890/transactions', 'post', [
    'type' => 'REDEEMING',
    'amount' => -25.00,  // Negative for redemption
    'client_id' => 'unique-transaction-id',
]);

// Reload gift card
$api->apiRequest('gift_cards/1234567890/transactions', 'post', [
    'type' => 'RELOADING',
    'amount' => 50.00,  // Positive for reload
]);

// Create webhook
$api->apiRequest('webhooks', 'post', [
    'url' => 'https://yoursite.com/webhook',
    'type' => 'sale.update',
]);

// Webhook types:
// - sale.update
// - inventory.update
// - customer.update
// - product.update

$results = $api->apiRequest('search', 'post', [
    'query' => 'blue shirt',
    'type' => 'products',
]);

$api->debug(true);

// Make requests...

// Access debug data
$rawResponse = $api->getLastResultRaw();
$httpCode = $api->getLastHttpCode();

// Allow time slip for servers with clock drift
$api->allowTimeSlip = true;

$oauth = new LightspeedOAuth(
    $clientId,
    $clientSecret,
    $domainPrefix,
    $redirectUri,
    ['products:read', 'sales:read', 'customers:read']  // Space-delimited in URL
);