> ## Documentation Index
> Fetch the complete documentation index at: https://www.1password.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Rotate a secret in an Environment

You can use 1Password CLI to rotate a secret stored in a [1Password Environment](https://www.1password.dev/environments.md) 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`](https://www.1password.dev/cli/reference/management-commands/environment#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](https://www.1password.dev/environments/programmatic-access.md#get-an-environments-id), which keeps working if the Environment is renamed.

## Requirements

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

* [Sign up for 1Password](https://1password.com/pricing/password-manager)
* [Install 1Password CLI](https://www.1password.dev/cli/get-started.md#step-1-install-1password-cli) version `2.43.0` or later
* An [Environment](https://www.1password.dev/environments.md#create-an-environment) that contains the [secret](https://www.1password.dev/environments.md#add-secrets-to-an-environment) you want to rotate

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

[Service accounts](https://www.1password.dev/service-accounts.md) 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.

> [!TIP]
>
> If you don't see the option to create service accounts, ask your administrator to [give you access to create and manage service accounts](https://www.1password.dev/service-accounts/manage-service-accounts.md#manage-who-can-create-service-accounts).

1. Use the [service account creation wizard](https://start.1password.com/developer-tools/infrastructure-secrets/serviceaccount/?source=dev-portal) 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.

> [tab] Bash, Zsh, sh
>
> ```shell theme={null}
> export OP_SERVICE_ACCOUNT_TOKEN=<your-token>
> ```

> [tab] fish
>
> ```shell theme={null}
> set -x OP_SERVICE_ACCOUNT_TOKEN <your-token>
> ```

> [tab] PowerShell
>
> ```powershell theme={null}
> $Env:OP_SERVICE_ACCOUNT_TOKEN = <your-token>
> ```

## 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.

> [!CAUTION]
>
> 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.

```shell theme={null}
#!/usr/bin/env bash
set -euo pipefail

ENVIRONMENT_ID="blgexucrwfr2dtsxe2q4uu7dp4"  # Replace with your Environment's ID
SECRET_NAME="API_KEY"

update_provider() {
  # TODO: Replace with the API call that sets the new credential at your provider.
  :
}

# Generate a new value
NEW_VALUE="$(openssl rand -base64 32)"

# Update the credential at your provider
update_provider "$NEW_VALUE"

# Store the new value in the Environment
printf '{"value": "%s"}' "$NEW_VALUE" | op environment secret edit "$SECRET_NAME" - --environment "$ENVIRONMENT_ID" > /dev/null

echo "Rotated $SECRET_NAME in Environment $ENVIRONMENT_ID"
```

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:

```shell theme={null}
op environment secret get API_KEY --environment blgexucrwfr2dtsxe2q4uu7dp4
```

## 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:

```shell theme={null}
0 2 * * * OP_SERVICE_ACCOUNT_TOKEN=<your-token> /path/to/rotate.sh
```

## Learn more

* [Manage Environments and secrets](https://www.1password.dev/cli/manage-environments.md)
* [`op environment` reference documentation](https://www.1password.dev/cli/reference/management-commands/environment)
* [Tutorial: Rotate a secret in a 1Password Environment on a schedule](https://www.1password.dev/sdks/tutorials/rotate-environment-secret.md) with 1Password SDKs
* [Programmatically access 1Password Environments](https://www.1password.dev/environments/programmatic-access.md)
* [Workflow: Secure your deployments](https://www.1password.dev/get-started/secure-deployment.md)


## Related topics

- [Manage Environments and secrets](https://www.1password.dev/cli/manage-environments.md)
- [Tutorial: Rotate a secret in a 1Password Environment on a schedule](https://www.1password.dev/sdks/tutorials/rotate-environment-secret.md)
- [environment](https://www.1password.dev/cli/reference/management-commands/environment.md)
