Integrating Shopify with your PHP applications requires proper authentication through access tokens. Whether you're building a custom app, developing inventory management systems, or creating automated workflows, understanding how to obtain and manage Shopify access tokens is essential for secure API communication. This comprehensive guide walks you through obtaining a Shopify access token specifically for PHP-based integrations in 2026.
The fastest and most secure way to obtain your Shopify access token for PHP integration is using GetShopifyToken.com. This service streamlines the OAuth authentication process without requiring manual API configuration.
This method is ideal for PHP developers who want to quickly authenticate without building OAuth infrastructure from scratch. The service handles token generation securely and provides immediate access for API requests.
If you prefer building the authentication flow directly into your PHP application, follow this manual OAuth implementation process. This approach gives you complete control over token management and is suitable for production applications.
Create a PHP script to handle the OAuth authorization process:
<?php
// authorize.php - Handle OAuth authorization request
session_start();
$api_key = $_ENV['SHOPIFY_API_KEY'];
$redirect_uri = 'https://yourapp.com/shopify/callback.php';
$shop = sanitize_shop_domain($_GET['shop'] ?? '');
$scope = 'read_products,write_products,read_orders,write_orders';
$nonce = bin2hex(random_bytes(16));
$_SESSION['nonce'] = $nonce;
$auth_url = "https://{$shop}/admin/oauth/authorize?" . http_build_query([
'client_id' => $api_key,
'redirect_uri' => $redirect_uri,
'scope' => $scope,
'state' => $nonce,
]);
header("Location: {$auth_url}");
exit;
function sanitize_shop_domain($shop) {
$shop = preg_replace('/[^a-zA-Z0-9\-]/', '', $shop);
return $shop . '.myshopify.com';
}
?>
Create a callback handler to exchange the authorization code for an access token:
<?php
// callback.php - Exchange authorization code for access token
session_start();
$api_key = $_ENV['SHOPIFY_API_KEY'];
$api_secret = $_ENV['SHOPIFY_API_SECRET'];
$shop = sanitize_shop_domain($_GET['shop'] ?? '');
$code = $_GET['code'] ?? '';
$state = $_GET['state'] ?? '';
// Verify state parameter matches
if ($state !== $_SESSION['nonce']) {
die('Invalid state parameter. Possible CSRF attack.');
}
// Exchange authorization code for access token
$url = "https://{$shop}/admin/oauth/access_token";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'client_id' => $api_key,
'client_secret' => $api_secret,
'code' => $code,
]));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);
if (isset($data['access_token'])) {
$access_token = $data['access_token'];
// Store securely in database or environment
$_SESSION['shopify_access_token'] = $access_token;
$_SESSION['shopify_shop'] = $shop;
echo "Authorization successful! Token: " . substr($access_token, 0, 10) . "...";
} else {
die("Authentication failed: " . ($data['errors'] ?? 'Unknown error'));
}
function sanitize_shop_domain($shop) {
$shop = preg_replace('/[^a-zA-Z0-9\-]/', '', $shop);
return $shop . '.myshopify.com';
}
?>
Once you have your access token, integrate it into your PHP application following these best practices:
Never hardcode tokens in your source code. Use environment variables with a .env file:
SHOPIFY_ACCESS_TOKEN=shpat_1234567890abcdef1234567890abcdef
SHOPIFY_SHOP_DOMAIN=mystore.myshopify.com
SHOPIFY_API_VERSION=2024-01
Load your .env file in your PHP application using the dotenv package:
<?php
require 'vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$access_token = $_ENV['SHOPIFY_ACCESS_TOKEN'];
$shop_domain = $_ENV['SHOPIFY_SHOP_DOMAIN'];
?>
Use your token to authenticate API requests:
<?php
$access_token = $_ENV['SHOPIFY_ACCESS_TOKEN'];
$shop_domain = $_ENV['SHOPIFY_SHOP_DOMAIN'];
$api_version = $_ENV['SHOPIFY_API_VERSION'] ?? '2024-01';
$url = "https://{$shop_domain}/admin/api/{$api_version}/products.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'X-Shopify-Access-Token: ' . $access_token,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
$products = json_decode($response, true);
echo "Retrieved " . count($products['products']) . " products";
} else {
echo "API Error: HTTP {$http_code}";
}
?>
For cleaner code, install the official Shopify PHP library via Composer:
composer require shopify/shopify-api
Then use it in your PHP code:
<?php
use Shopify\Clients\Rest;
$access_token = $_ENV['SHOPIFY_ACCESS_TOKEN'];
$shop_domain = $_ENV['SHOPIFY_SHOP_DOMAIN'];
$client = new Rest(
$shop_domain,
$access_token,
api_version: '2024-01'
);
$result = $client->get('products', [], ['limit' => 10]);
$products = $result->getDecodedBody();
foreach ($products['products'] as $product) {
echo $product['title'] . "\n";
}
?>
Select appropriate scopes based on your PHP application's functionality. Requesting unnecessary scopes violates security best practices.
| Scope | Purpose |
|---|---|
| read_products | Read product data, collections, and inventory information |
| write_products | Modify product details, pricing, and inventory levels |
| read_orders | Access order history, customer information, and fulfillment status |
| write_orders | Create orders, update order status, and manage fulfillments |
| read_inventory | Monitor inventory levels across locations |
| write_inventory | Adjust inventory quantities and manage stock levels |
| read_customers | Retrieve customer profiles, emails, and purchase history |
| write_customers | Create, update, and manage customer accounts |
| read_locations | Access store location data and warehouse information |
| write_fulfillments | Manage order fulfillment and tracking information |
| read_analytics | Access store analytics and performance metrics |
| write_content | Manage blog posts, pages, and content assets |
Access tokens issued through the OAuth flow do not expire automatically. However, they can be revoked if your app is uninstalled from the store or if the store owner manually revokes permission. Store tokens securely and implement refresh logic if tokens are revoked. Monitor for 401 errors and regenerate tokens as needed.
Yes, a single access token can be used across multiple applications and servers as long as they all properly store and secure it. Each API request just needs to include the token in the X-Shopify-Access-Token header. However, it's considered a best practice to use separate tokens for separate applications to maintain security isolation and easier revocation if one application is compromised.
Private apps (deprecated as of 2024) automatically generated tokens without OAuth flow. Custom apps use the modern OAuth 2.0 flow, which is more secure and is the current Shopify standard. For new PHP projects, always use custom apps with OAuth authentication. Custom apps provide better security, audit trails, and alignment with Shopify's platform requirements.
---Skip the manual OAuth flow. GetShopifyToken automates the entire process — just paste your credentials and get your token instantly.
Generate Token Now →