Skip to main content

Load secrets into the environment

With op run, you can provide your project secrets directly from 1Password to an application or script as environment variables at runtime. You can use op run with 1Password Environments, environment variables set to secret references, or a combination of both.

1Password CLI loads the specified secrets, then runs the provided command in a subprocess with the secrets made available as environment variables only for the duration of the process. This allows you to avoid hardcoding any plaintext secrets and quickly switch between different sets of secrets for different development contexts.

Choose your configuration

1Password CLI provides multiple methods to load your project secrets:

  • 1Password Environments (beta) allow you to create Environments in 1Password that contain all your environment variables for a specific workflow. You can share Environments with your team and create separate Environments for each project, application, or development context (like staging or production).

  • Secret references are URIs that point to where a secret is stored in your 1Password account. A secret reference uses the names or unique identifiers of the vault, item, section, and field where the secret is stored in 1Password. You can set environment variables to secret references on the command line or use secret references in your .env files. Secret references require more manual setup than 1Password Environments to switch between different sets of environment variables for different contexts, or create shared team workflows.

  • Hybrid approach: You can use op run to load variables from a 1Password Environment alongside secret references from .env files or exported environment variables.

tip

Authenticate with a 1Password Service Account to follow the principle of least privilege. You can scope service account access to specific vaults and 1Password Environments so that processes in your authorized terminal session can only access secrets required for a given purpose.

caution

You should assume that processes on your computer can access the environment of other processes run by the same user. Be aware of this when supplying secrets through environment variables.

Requirements

Step 1: Store your project secrets in 1Password

To store your project secrets in a 1Password Environment, follow the steps to create an Environment, then import a .env file or manually add your environment variables.

Step 2: Pass the secrets to the application

To pass your environment variables from 1Password to an application or script:

  1. Open the 1Password app and navigate to Developer > Environments.

  2. Select the Environment where your project secrets are stored, then select Manage environment > Copy environment ID.

  3. Use op run -- with the command for starting the application or script. 1Password will run the provided command in a subprocess with the secrets made available as environment variables for the duration of the process.

    For example:

Use both methods together

You can load environment variables from an Environment in combination with secret references from a .env file or flag. For example:

Next step: Run in production

Now that the application works locally, choose how to load your secrets in production or CI/CD:

  • 1Password Service Account: Automate access with a service account token. Service accounts support both secret references and 1Password Environments.
  • 1Password Connect Server: Best for self-hosting within your own infrastructure. Connect only supports secret references and does not currently support 1Password Environments.

Learn more

Was this page helpful?