Authentication overview
Shopify's authentication system facilitates secure interactions between external applications, services, and the Shopify platform. This includes custom apps, public apps, and integrations requiring access to store data or functionalities. The core principle involves granting specific permissions (scopes) to an application, which then uses appropriate credentials to make authenticated requests to the Shopify Admin API (REST or GraphQL) or the Storefront API. Shopify categorizes access based on whether an application is developed for a single store (custom app) or intended for distribution across multiple stores (public app), each having distinct authentication flows and credential management.
Authentication mechanisms are designed to protect sensitive merchant and customer data while providing flexible ways for developers to build powerful commerce solutions. Understanding the correct authentication method for your specific use case, along with proper credential management and security practices, is crucial for developing secure and functional Shopify integrations. Shopify also provides various SDKs across languages like Ruby, Node.js, and Python, which abstract some of the complexities of authentication, simplifying the process for developers.
Supported authentication methods
Shopify supports several authentication methods, each tailored for different integration scenarios:
- OAuth 2.0: This is the standard and recommended method for authenticating public and custom apps, especially when interacting with the Shopify Admin API. OAuth 2.0 allows apps to request specific permissions (scopes) from a merchant without ever handling the merchant's login credentials. The merchant grants permission, and the app receives an access token to make API calls on their behalf. Shopify's implementation follows the OAuth 2.0 Authorization Framework.
- API Access Tokens (for Custom Apps): For custom apps built for a single Shopify store, access tokens can be generated directly from the Shopify admin. These tokens provide persistent access to the store's data based on predefined permissions set during token creation. This method is simpler than OAuth for single-store integrations but requires careful handling as the token itself acts as proof of authorization.
- Storefront API Access Tokens: The Shopify Storefront API uses its own set of access tokens, specifically designed for fetching public product data, managing carts, and processing checkouts directly from a storefront application (e.g., a headless commerce frontend). These tokens are distinct from Admin API tokens and have limited scopes focused on storefront operations.
- Webhook Signature Verification: While not an authentication method for API requests, Shopify webhooks are secured by including an
X-Shopify-Hmac-SHA256header in each request. This signature allows the receiving application to verify that the webhook payload originated from Shopify and has not been tampered with. This is crucial for maintaining data integrity and preventing unauthorized requests. Verification involves hashing the webhook body with a shared secret and comparing it to the provided signature, as detailed in Shopify's webhook validation documentation.
Authentication Method Comparison
The table below summarizes Shopify's primary authentication methods:
| Method | When to Use | Security Level | Credential Type |
|---|---|---|---|
| OAuth 2.0 | Public apps, custom apps requiring merchant consent flow, multi-store integrations | High (token-based, scope-limited) | Client ID, Client Secret, Access Token |
| API Access Tokens (Custom Apps) | Custom apps for a single store, direct server-to-server integration | Medium-High (persistent token, pre-configured scopes) | API Key, Access Token |
| Storefront API Access Tokens | Headless commerce frontends, public data access, cart/checkout management | Medium (specific to Storefront API, limited scopes) | Storefront API Access Token |
| Webhook Signature Verification | Receiving and validating webhook payloads from Shopify | High (HMAC-SHA256 integrity check) | Shared Webhook Secret |
Getting your credentials
The process for obtaining credentials depends on the type of application you are building:
For Public Apps (OAuth 2.0)
- Create a Partner Account: If you don't have one, sign up for a Shopify Partner account.
- Create a New App: From your Partner Dashboard, navigate to 'Apps' and click 'Create app'. Select 'Public app'.
- Configure App Details: Provide basic information, define your app's requested API access scopes, and set the 'App URL' and 'Allowed redirection URL(s)' for the OAuth callback.
- Obtain Client ID and Client Secret: After creating the app, Shopify will provide a Client ID (API Key) and Client Secret (API Secret Key). These are your primary credentials for initiating the OAuth flow.
For Custom Apps (API Access Tokens or OAuth 2.0)
- Access Shopify Admin: Log in to the Shopify admin panel of the target store.
- Navigate to Apps: Go to 'Apps' > 'Develop apps'.
- Create a Custom App: Click 'Create an app' and give it a name.
- Configure API Scopes: Under 'Configuration', choose 'Admin API integration' or 'Storefront API integration' and select the necessary permissions (scopes) your app will require.
- Generate Access Token (Admin API): For Admin API access, after configuring scopes, click 'Install app' and then 'Reveal token once'. This will display your Admin API access token. This token is shown only once, so store it securely immediately.
- Generate Access Token (Storefront API): For Storefront API access, configure the Storefront API integration and generate a token similarly. This token is also sensitive.
- Obtain API Key and Secret (if using OAuth for Custom Apps): If you choose to use OAuth 2.0 for a custom app (less common but possible for more complex custom app flows), you will find the Client ID (API key) and Client Secret (API secret key) under the 'App credentials' section after creating the custom app.
For Webhook Secrets
When you create or update a webhook subscription in your Shopify app or directly in the Shopify admin, you will specify a secret key. This secret key is critical for validating incoming webhook payloads. You can configure this during the webhook creation process within the Shopify admin under 'Settings' > 'Notifications' > 'Webhooks' or programmatically via the API.
Authenticated request example
Here's an example of an authenticated request to the Shopify Admin REST API using an API Access Token (for a custom app) in a Node.js environment. This example fetches properties for a specific product.
const axios = require('axios');
const shopName = 'your-shop-name'; // Replace with your Shopify shop name
const accessToken = 'shpat_YOUR_ADMIN_API_ACCESS_TOKEN'; // Replace with your Admin API access token
const productId = '8406560641323'; // Example product ID
const url = `https://${shopName}.myshopify.com/admin/api/2023-10/products/${productId}.json`;
axios.get(url, {
headers: {
'X-Shopify-Access-Token': accessToken,
'Content-Type': 'application/json',
}
})
.then(response => {
console.log('Product data:', JSON.stringify(response.data, null, 2));
})
.catch(error => {
console.error('Error fetching product:', error.response ? error.response.data : error.message);
});
For applications using OAuth, the access token would be obtained dynamically through the OAuth flow and then used in the X-Shopify-Access-Token header, similar to the above example. For the Storefront API, the header would be X-Shopify-Storefront-Access-Token.
Security best practices
Adhering to security best practices is paramount when working with Shopify credentials and APIs:
- Protect Access Tokens and API Secrets: Never hardcode API keys, access tokens, or client secrets directly into your application code. Use environment variables, secure configuration files, or secret management services (e.g., AWS Secrets Manager, Google Secret Manager) to store and retrieve these credentials.
- Least Privilege Principle: Request and grant only the minimum necessary API access scopes your application needs to function. Overly broad permissions increase the risk radius in case of a compromise. Review and update scopes regularly. Shopify provides detailed documentation on API access scopes.
- Regular Token Rotation: Although Shopify Admin API access tokens for custom apps are persistent, consider rotating them periodically, especially if there are changes in personnel or security policies. OAuth access tokens often have shorter lifespans and require refresh tokens for continued access, adding another layer of security.
- Validate Webhook Signatures: Always verify the
X-Shopify-Hmac-SHA256header for every incoming webhook to ensure its authenticity and integrity. Failing to do so makes your application vulnerable to forged requests. This process involves calculating the HMAC-SHA256 hash of the raw webhook body using your shared secret and comparing it with the provided header value. - Secure Redirect URLs: For OAuth-based applications, ensure that your 'Allowed redirection URL(s)' are specific and use HTTPS. Avoid using wildcard URLs in production environments, as this can create open redirect vulnerabilities.
- HTTPS Everywhere: All communication with Shopify APIs and your application's endpoints (especially for OAuth callbacks and webhooks) must use HTTPS. This encrypts data in transit, protecting credentials and sensitive information from interception. The W3C also emphasizes the importance of HTTPS for secure web communication.
- Error Handling and Logging: Implement robust error handling and logging for authentication failures. This can help detect unauthorized access attempts or misconfigurations. However, avoid logging raw credentials or sensitive personal information.
- Secure Development Lifecycle: Integrate security considerations throughout your application's development lifecycle, from design to deployment and maintenance. Regularly audit your code and dependencies for vulnerabilities.