API credential rotation
API credentials should be changed from time to time. Employees leave, API credentials can be accidentally committed to version control and wide-reaching security flaws can be discovered. While security risks, these occasions don’t often warrant downtime. Follow these steps to rotate your API credentials without any downtime for your app.
In the case of a serious security breach, your compromised API credentials should be revoked immediately before generating new ones. This will prevent a malicious attacker from accessing or modifying your users' data while you transition to new credentials. In a high-risk situation, downtime should not be avoided!
Step 1: Create a new secret key
A new shared secret must be generated to securely communicate with Shopify’s API. Create a new shared secret from your app’s page in the Partner Dashboard.
Step 2: Configure webhooks
Webhooks are signed with your app’s secret key to prevent forgeries. If your app uses webhooks, configure it to accept both webhooks signed with both the new secret key and webhooks signed with the old secret key until after revoking the old secret.
Shopify will sign webhooks with your app's oldest unrevoked secret key. We know it can be convenient to use the same secret key configuration for both OAuth and webhook validation, but this makes it easy to incorrectly validate webhooks using only the new secret key after following step #3.
Step 3: Configure OAuth
Access tokens requested from Shopify’s API using the new secret key will be secure. Configure your app to use only the new secret key for OAuth Authentication.
Step 4: Generate new refresh token
Many of the access tokens stored by your app will be associated with the old secret key. New access tokens must be requested from the Shopify API to work with the new secret key. You'll need a refresh token to generate these new access tokens. Create a refresh token from your app’s page in the Partner Dashboard. Refresh tokens automatically expire after one hour.
Step 5: Request new access tokens
For each access token stored by your application, refresh it by requesting an access token using your new secret key and the refresh token:
with the following parameters:
- client_id (required): The API key for your app
- client_secret (required): The new secret key for your app
- refresh_token (required): The refresh token you created from your app’s page in the Partner Dashboard
- access_token (required): The access token you would like to refresh
The refresh token is temporary, and can only be used for one hour after it has been generated.
Step 6: Revoke the old secret key
Now your app is using the new secret key to communicate with the Shopify API. The old secret key can now be revoked. Revoke it from your app’s page in the Partner Dashboard. Remember that revoking any secret will also remove the access tokens associated with it.
If your app uses Webhooks, configure it to accept Webhooks signed with the new secret key only.
The following shows a basic example implementation of Access Token rotation in the Ruby programming language.
The following shows a basic example implementation of Access Token rotation in the PHP programming language.