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

# Programmatically access 1Password Environments

> Learn how to read and manage 1Password Environments and the secrets they contain using 1Password SDKs and 1Password CLI.

You can programmatically access [1Password Environments](https://www.1password.dev/environments.md) using [1Password SDKs](#sdks) or [1Password CLI](#cli). Both tools can read the secrets in an Environment and pass them to your applications. With 1Password SDKs or 1Password CLI, you can also create, edit, and delete Environments and their secrets.

> [!WARNING]
>
> **Deprecation**
>
> The following functionality is deprecated and will be removed on January 20, 2027:
>
> * The `op environment read` command in 1Password CLI. To list the secrets in an Environment, use `op environment secret list` instead, available in [version `2.42.0` or later](https://app-updates.agilebits.com/product_history/CLI2). To pass secrets from an Environment to an application or script, use [`op run --environment`](#pass-secrets-to-an-application-or-script).
> * The `get_variables()` function in 1Password SDKs, and the `GetVariablesResponse` and `EnvironmentVariable` types it returns. Use [`list_secrets()`](https://www.1password.dev/sdks/environments#list-secrets) instead, available in Go SDK `0.5.0`, JavaScript SDK `0.6.0`, and Python SDK `0.5.0` or later. It takes an Environment ID or name and a `reveal` option, and returns a list of `EnvironmentSecret` objects directly, without a response wrapper.

## Choose your configuration

### Tool options

* **1Password CLI**: Best for quick testing, shell scripts, CI/CD pipelines, Infrastructure as Code, build tools and task runners.
* **1Password SDKs**: Best for native integrations with Go, Python, or JavaScript applications.

### Authentication options

* **Local authentication with the 1Password desktop app**: Authenticate in the same way you unlock your [1Password desktop app](https://1password.com/downloads/), like with biometrics or your 1Password account password. Requires minimal setup with no token management. Enables human-in-the-loop approval for sensitive workflows.
* **1Password Service Accounts**: Authenticate using a [service account token](https://www.1password.dev/service-accounts.md) scoped to the Environments you want to access. Best for shared building, automated access, and headless server authentication. Enables you to follow the [principle of least privilege](https://csrc.nist.gov/glossary/term/least_privilege) in your project.

### Decision guide

Use the table below to find the best tool and authentication method for your specific use case.

| Use case | Recommended tool | Authentication method | Why this approach |
| - | - | - | - |
| **Local development** on your machine | [CLI](#cli) or [SDK](#sdks) | Desktop app | Uses existing 1Password account credentials, making it seamless for individual developers working locally. No token management. |
| **Quick testing** and exploration | [CLI](#cli) | Desktop app | Fastest way to test. Uses existing 1Password account credentials with minimal setup required. |
| **Desktop applications** | [SDK](#sdks) | Desktop app | Better integration with application code. Desktop app authentication allows end users to authenticate with their own 1Password accounts. |
| **Shell scripts** and automation tasks | [CLI](#cli) | Desktop app or service account | 1Password CLI is designed for shell scripting. Use 1Password desktop app for personal scripts, service accounts for shared/automated scripts. |
| **CI/CD pipelines** (GitHub Actions, GitLab CI, etc.) | [CLI](#cli) | Service account | Service accounts provide non-interactive authentication perfect for automated workflows. 1Password CLI is lightweight and easy to integrate into pipeline scripts. |
| **Application runtime** (production services) | [SDK](#sdks) | Service account | 1Password SDKs offer native language integration with better error handling and type safety. Service accounts enable secure, automated access without user interaction. |
| **Server-side applications** | [SDK](#sdks) | Service account | 1Password SDKs offer robust error handling and connection pooling. Service accounts enable headless server authentication. |
| **Docker containers and Kubernetes** | [SDK](#sdks) or [CLI](#cli) | Service account | Service accounts work well in containerized environments. Choose 1Password SDKs for application containers, 1Password CLI for init containers or sidecars. |
| **Infrastructure as Code** (Terraform, Pulumi, etc.) | [CLI](#cli) | Service account | 1Password CLI can be easily invoked from IaC tools. Service accounts enable automated infrastructure provisioning. |
| **Build tools and task runners** | [CLI](#cli) | Desktop app or service account | 1Password CLI integrates easily with build tools like Make, Gradle, or npm scripts. Use 1Password desktop app for developer builds. Service accounts support shared building. |

## Get an Environment's ID

1Password SDKs and 1Password CLI accept an Environment's unique identifier (ID) or its name. The exception is `op run --environment`, which requires the ID.

Use the ID if two Environments share a name. We also recommend it in scripts and automation, because it keeps working if the Environment is renamed and lookups by ID are faster.

You can get an Environment's ID by running [`op environment list`](https://www.1password.dev/cli/reference/management-commands/environment#environment-list), by [listing Environments with an SDK](https://www.1password.dev/sdks/environments.md#list-environments), or in the [1Password desktop app](https://1password.com/downloads/):

1. Open and unlock the 1Password desktop app, then navigate to **Developer** > **View Environments**.
2. Select **View environment** next to the Environment you want to fetch.
3. Select **Manage environment** > **Copy environment ID**.

## Access Environments with 1Password SDKs

With [1Password SDKs](https://www.1password.dev/sdks.md), you can read the secrets stored in your 1Password Environments by the Environment's [ID](#get-an-environments-id) or name, and create, edit, and delete Environments and their secrets. See [Manage Environments and secrets using 1Password SDKs](https://www.1password.dev/sdks/environments.md) for method details and examples in Go, JavaScript, and Python.

> [!NOTE]
>
> Service accounts can only use the Environments you grant them access to, and need write access to an Environment to manage its secrets. A service account can only edit or delete Environments it created.

### Examples

> [tab] Service account
>
> Before running the following example:
>
> * Set `OP_SERVICE_ACCOUNT_TOKEN` to the token for a service account that has access to the Environment you want to fetch.
> * Set `OP_ENVIRONMENT_ID` to the [ID](#get-an-environments-id) of your 1Password Environment.
>
> ```go Go theme={null}
> package main
>
> import (
> 	"context"
> 	"fmt"
> 	"os"
>
> 	"github.com/1password/onepassword-sdk-go"
> )
>
> func main() {
> 	ctx := context.Background()
>
> 	// Initialize the SDK client
> 	client, err := onepassword.NewClient(
> 		ctx,
> 		onepassword.WithServiceAccountToken(os.Getenv("OP_SERVICE_ACCOUNT_TOKEN")),
> 		onepassword.WithIntegrationInfo("My 1Password Integration", "v1.0.0"),
> 	)
> 	if err != nil {
> 		fmt.Fprintln(os.Stderr, err)
> 		os.Exit(1)
> 	}
>
> 	// Get all secrets from the Environment and reveal their values
> 	environment := os.Getenv("OP_ENVIRONMENT_ID")
> 	secrets, err := client.Environments().ListSecrets(ctx, environment, true)
> 	if err != nil {
> 		fmt.Fprintln(os.Stderr, err)
> 		os.Exit(1)
> 	}
>
> 	// Process the secrets
> 	for _, secret := range secrets {
> 		fmt.Printf("%s=%s (concealed: %t)\n", secret.Name, secret.Value, secret.Concealed)
> 	}
> }
> ```
>
> ```js JavaScript theme={null}
> import { createClient } from "@1password/sdk";
>
> const client = await createClient({
> 	auth: process.env.OP_SERVICE_ACCOUNT_TOKEN,
> 	integrationName: "My 1Password Integration",
> 	integrationVersion: "v1.0.0",
> });
>
> // Get all secrets from the Environment and reveal their values
> const environment = process.env.OP_ENVIRONMENT_ID;
> const secrets = await client.environments.listSecrets(environment, true);
>
> // Process the secrets
> secrets.forEach(({ name, value, concealed }) => {
> 	console.log(`${name}=${value} (concealed: ${concealed})`);
> });
> ```
>
> ```python Python theme={null}
> import asyncio
> import os
> from onepassword import Client
>
> async def main():
> 	# Initialize and authenticate the client
> 	client = await Client.authenticate(
> 		auth=os.environ.get("OP_SERVICE_ACCOUNT_TOKEN"),
> 		integration_name="My 1Password Integration",
> 		integration_version="v1.0.0",
> 	)
>
> 	# Get all secrets from the Environment and reveal their values
> 	environment = os.environ.get("OP_ENVIRONMENT_ID")
> 	secrets = await client.environments.list_secrets(environment, True)
>
> 	# Process the secrets
> 	for secret in secrets:
> 		print(f"{secret.name}={secret.value} (concealed: {secret.concealed})")
>
> asyncio.run(main())
> ```

> [tab] 1Password desktop app
>
> Before running the following example:
>
> * Replace `OP_ACCOUNT_NAME` in the code below with your 1Password account name as it appears at the top left sidebar in the 1Password desktop app.
> * Set `OP_ENVIRONMENT_ID` to the [ID](#get-an-environments-id) of your 1Password Environment.
>
> ```go Go theme={null}
> package main
>
> import (
> 	"context"
> 	"fmt"
> 	"os"
>
> 	"github.com/1password/onepassword-sdk-go"
> )
>
> func main() {
> 	ctx := context.Background()
>
> 	// Initialize the SDK client
> 	client, err := onepassword.NewClient(
> 		ctx,
> 		onepassword.WithDesktopAppIntegration(os.Getenv("OP_ACCOUNT_NAME")),
> 		onepassword.WithIntegrationInfo("My 1Password Integration", "v1.0.0"),
> 	)
> 	if err != nil {
> 		fmt.Fprintln(os.Stderr, err)
> 		os.Exit(1)
> 	}
>
> 	// Get all secrets from the Environment and reveal their values
> 	environment := os.Getenv("OP_ENVIRONMENT_ID")
> 	secrets, err := client.Environments().ListSecrets(ctx, environment, true)
> 	if err != nil {
> 		fmt.Fprintln(os.Stderr, err)
> 		os.Exit(1)
> 	}
>
> 	// Process the secrets
> 	for _, secret := range secrets {
> 		fmt.Printf("%s=%s (concealed: %t)\n", secret.Name, secret.Value, secret.Concealed)
> 	}
> }
> ```
>
> ```js JavaScript theme={null}
> import { createClient, DesktopAuth } from "@1password/sdk";
>
> const client = await createClient({
> 	auth: new DesktopAuth(process.env.OP_ACCOUNT_NAME),
> 	integrationName: "My 1Password Integration",
> 	integrationVersion: "v1.0.0",
> });
>
> // Get all secrets from the Environment and reveal their values
> const environment = process.env.OP_ENVIRONMENT_ID;
> const secrets = await client.environments.listSecrets(environment, true);
>
> // Process the secrets
> secrets.forEach(({ name, value, concealed }) => {
> 	console.log(`${name}=${value} (concealed:${concealed})`);
> });
> ```
>
> ```python Python theme={null}
> import asyncio
> import os
> from onepassword import Client, DesktopAuth
>
> async def main():
> 	# Initialize and authenticate the client
> 	client = await Client.authenticate(
> 		auth=DesktopAuth(os.environ.get("OP_ACCOUNT_NAME")),
> 		integration_name="My 1Password Integration",
> 		integration_version="v1.0.0",
> 	)
>
> 	# Get all secrets from the Environment and reveal their values
> 	environment = os.environ.get("OP_ENVIRONMENT_ID")
> 	secrets = await client.environments.list_secrets(environment, True)
>
> 	# Process the secrets
> 	for secret in secrets:
> 		print(f"{secret.name}={secret.value} (concealed:{secret.concealed})")
>
> asyncio.run(main())
> ```

## Secret visibility

Two settings control whether you get a secret's value. One is stored with the secret, and the other is chosen for each request.

* **Hide value by default** is a setting on the secret. In 1Password SDKs, it's the `concealed` property. New secrets are hidden by default.
* **Reveal** is a request option. In 1Password SDKs, it's the `reveal` argument to `get_secret()` and `list_secrets()`. In 1Password CLI, it's the `--reveal` flag. It doesn't change the secret.

| Secret setting | Reveal requested | Value returned |
| :- | :- | :- |
| Show value by default | Yes or no | Yes |
| Hide value by default | Yes | Yes |
| Hide value by default | No | No. 1Password SDKs return an empty string. |

To change a secret's default visibility, [edit the secret](https://www.1password.dev/sdks/environments.md#edit-a-secret) with an SDK, or use the 1Password desktop app:

1. Open and unlock the 1Password desktop app, then navigate to **Developer** > **View Environments**.
2. Open the Environment and select **Edit**.
3. Select the vertical ellipsis  next to the secret, then select **Show value by default** or **Hide value by default**.

## Access Environments with 1Password CLI

With [1Password CLI](https://www.1password.dev/cli.md), you can:

* Pass the secrets in an Environment to an application or script with [`op run --environment`](https://www.1password.dev/cli/reference/commands/run#load-variables-from-environments).
* List the secrets in an Environment with [`op environment secret list`](https://www.1password.dev/cli/reference/management-commands/environment#environment-secret-list).
* Create, edit, and delete Environments and their secrets with the [`op environment`](https://www.1password.dev/cli/reference/management-commands/environment) commands.

See [Manage Environments and secrets](https://www.1password.dev/cli/manage-environments.md) for the commands and examples.

> [!NOTE]
>
> Service accounts can only use the Environments you grant them access to, and need write access to an Environment to manage its secrets. A service account can only edit or delete Environments it created.

### Before you get started

Before you get started, [install 1Password CLI](https://www.1password.dev/cli/get-started.md#step-1-install-1password-cli) version `2.43.0` or later. Then choose your authentication method:

* **Local authentication with the 1Password desktop app**: Authenticate in the same way you unlock your [1Password desktop app](https://1password.com/downloads/), like with biometrics or your 1Password account password. To set up local authentication, [turn on the 1Password CLI desktop app integration](https://www.1password.dev/cli/get-started.md#step-2-turn-on-the-1password-desktop-app-integration).
* **Service account**: Authenticate using a service account token that has limited access to a singular Environment. To authenticate using a service account token, [create a new service account](https://start.1password.com/developer-tools/infrastructure-secrets/serviceaccount/) with read access to the appropriate Environments. Choose read access to list secrets, or write access to manage them. Then export your service account token:

  export OP\_SERVICE\_ACCOUNT\_TOKEN=\\\<your-service-account-token\\>

  set -x OP\_SERVICE\_ACCOUNT\_TOKEN \<your-service-account-token>

  \$Env:OP\_SERVICE\_ACCOUNT\_TOKEN = "\<your-service-account-token>"

### Pass secrets to an application or script

`op run --environment` is available in the stable release of 1Password CLI, version `2.41.0` or later.

To pass secrets from a 1Password Environment to an application or script as environment variables, use [`op run`](https://www.1password.dev/cli/reference/commands/run) with the `--environment` flag and the [Environment's ID](#get-an-environments-id), then pass the results to the application or script. 1Password CLI runs the application or script as a subprocess with your secrets loaded into the environment for the duration of the process.

```shell theme={null}
op run --environment <environmentID> -- <command>
```

> [!NOTE]
>
> Hidden secrets are automatically concealed in stdout and stderr output. To show them, change the secret's default visibility or use the `--no-masking` flag with `op run`. See [Secret visibility](#secret-visibility).

#### Use with environment variables from multiple sources

You can also use `op run` with multiple environments, or in combination with `.env` files or shell environment variables. When the same environment variable exists in multiple sources, 1Password CLI gives them the following precedence:

1. 1Password Environments (highest priority)
2. Environment files
3. Shell environment variables (lowest priorities)

If a secret with the same name exists in multiple 1Password Environments, the last Environment specified takes precedence.

#### Examples

To print a single secret from a 1Password Environment, pass its name to `printenv`. Because Environment secrets are hidden by default, the value stays concealed in the output:

```shell theme={null}
op run --environment blgexucrwfr2dtsxe2q4uu7dp4 -- printenv API_KEY
#code-result
<concealed by 1Password>
```

To run a script provisioned with the secrets from an Environment:

```shell theme={null}
op run --environment blgexucrwfr2dtsxe2q4uu7dp4 -- ./my-script.sh
```

To list the secrets in an Environment as a `.env` file you can pipe to other tools, use [`op environment secret list`](https://www.1password.dev/cli/reference/management-commands/environment#environment-secret-list) with the `--reveal` flag:

```shell theme={null}
op environment secret list blgexucrwfr2dtsxe2q4uu7dp4 --reveal --format=dotenv

#code-result
DB_HOST=localhost
DB_USER=admin
API_KEY=sk-abc123
```

## Learn more

* [Manage Environments and secrets using 1Password SDKs](https://www.1password.dev/sdks/environments.md)
* [1Password CLI `environment` command reference](https://www.1password.dev/cli/reference/management-commands/environment)
* [Access secrets from 1Password through local .env files](https://www.1password.dev/environments/local-env-file.md)
* [Use 1Password's agent hook to validate local .env files from 1Password Environments](https://www.1password.dev/environments/agent-hook-validate.md)
* [Sync secrets from 1Password to AWS Secrets Manager](https://www.1password.dev/environments/aws-secrets-manager.md)
* [Workflow: Secure local development](https://www.1password.dev/get-started/secure-developer-secrets.md)
* [Workflow: Secure your deployments](https://www.1password.dev/get-started/secure-deployment.md)


## Related topics

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