Skip to main content
In this tutorial, you’ll learn how to rotate a secret stored in a 1Password Environment on a schedule using 1Password SDKs. We’ll walk through the process using an example script that generates a new API key, updates it at your provider, then stores the new value in your Environment. Any application that reads the Environment picks up the new value the next time it starts. By the end, you’ll understand how to:
  • Create a service account with write access to an Environment.
  • Generate a new secret value with the 1Password SDK.
  • Edit a secret in an Environment without exposing its value.
  • Run the rotation script on a schedule.

Prerequisites

  1. 1Password subscription.
  2. 1Password desktop app.
  3. An Environment that contains the secret you want to rotate.
  4. Basic knowledge of Python.
  5. Familiarity with 1Password SDKs. To learn how to get started, see the end-to-end setup tutorial.

Part 1: Set up the Environment and service account

In the first part of this tutorial, you’ll make sure the secret you want to rotate is stored in an Environment, then create a service account that can edit it.

Step 1: Add the secret to an Environment

If you haven’t already, create an Environment and add the secret you want to rotate. For our example, we’ll use an Environment named Production that contains a secret named API_KEY.

Step 2: 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. For this tutorial, create a service account that has write access to the Production Environment, so it 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.

Part 2: Write the rotation script

In the second part of this tutorial, you’ll write a Python script that generates a new value for the secret, updates it at your provider, and stores it in the Environment.

Step 1: Export your service account token

Export the service account token you saved in part one to the OP_SERVICE_ACCOUNT_TOKEN environment variable.

Step 2: Generate a new value

Use the Secrets.generate_password() method to generate the new value. In our example, we generate a 32-character random string with letters and digits.

Step 3: Update the credential at your provider

Set the new value at the service that accepts the credential, such as your API provider or database. This step depends on your provider’s API, so the example uses a placeholder function. Replace its body with the API call your provider documents.
Update the provider before you update the Environment. If the provider call fails, the Environment still holds the value that works.

Step 4: Store the new value in the Environment

Use the environments.edit_secret() method to store the new value. Pass only the value so the secret keeps its name and default visibility. The script prints the secret’s name, not its value. Here’s the complete script:
Run the script once to confirm it works. Then check the secret in the 1Password desktop app.

Part 3: 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:
To run the script in AWS instead, see the Rotate an API key with AWS EventBridge example project.

Conclusion

In this tutorial, you learned how to rotate a secret in a 1Password Environment on a schedule, without exposing its value or giving the rotation script access to anything else in your account. You can extend the script to rotate several secrets in one run, or to rotate secrets across multiple Environments.

Learn more