Skip to main content
You can use 1Password CLI to rotate a secret stored in a 1Password Environment on a schedule. A rotation script generates a new value, updates it at your provider, then stores the new value in the Environment with op environment secret edit. Any application that reads the Environment picks up the new value the next time it starts. This guide uses an Environment named Production that contains a secret named API_KEY. The script refers to the Environment by its ID, which keeps working if the Environment is renamed.

Requirements

Before you can rotate a secret with 1Password CLI, you’ll need to:

Step 1: Create a service account with write access to the Environment

Service accounts are a token-based authentication method that you can scope to specific Environments. Create one that has write access to the Production Environment, so the script can edit the secret.
If you don’t see the option to create service accounts, ask your administrator to give you access to create and manage service accounts.
  1. Use the service account creation wizard on 1Password.com.
  2. Give your service account a name. For example, Secret Rotation Service Account.
  3. Choose the Production Environment and give the service account write access. This can’t be changed later.
  4. Select Create Account, then select Save in 1Password to save the service account token in your 1Password account.

Step 2: Export your service account token

Export the service account token you saved in the previous step to the OP_SERVICE_ACCOUNT_TOKEN environment variable.

Step 3: Write the rotation script

The script does three things in order: generate a new value, update the credential at your provider, and store the new value in the Environment. Update the provider before the Environment, so that if the provider call fails, the Environment still holds the value that works.
Command arguments can be visible to other processes on your machine. The script pipes the new value to op environment secret edit as JSON instead of passing it with the --value flag, and never prints it.
The provider step depends on your provider’s API, so the example uses a placeholder function. Replace its body with the API call your provider documents.
Save the script as rotate.sh and make it executable, then run it once to confirm it works. To check the result without revealing the value:

Step 4: Run the script on a schedule

Run the script from any scheduler that can set the OP_SERVICE_ACCOUNT_TOKEN environment variable. For example, to run it every day at 02:00 with cron, add this line to your crontab:

Learn more