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

# Read 1Password Environments using 1Password SDKs

export const BetaBadge = ({content}) => {
  if (content) {
    return <span className="op-beta-badge-inline not-prose">
        <span className="op-status-badge">Beta</span>
      </span>;
  }
  return <>
      <style>
        {`
        .mdx-content {
          margin-top: 4px !important;
        }
      `}
      </style>
      <span className="op-status-badge not-prose">Beta</span>
    </>;
};

<BetaBadge />

You can use [1Password SDKs](/sdks) to programmatically read environment variables stored in [1Password Environments (beta)](/environments) and use them in your applications.

## Requirements

To use this feature, you'll need to install the beta version of the Go, JS, or Python SDK:

<Tabs groupId="sdks">
  <Tab value="go" title="Go">
    ```shell theme={null}
    go get github.com/1password/onepassword-sdk-go@v0.4.1-beta.1
    ```
  </Tab>

  <Tab value="JS" title="JavaScript">
    ```shell theme={null}
    npm install @1password/sdk@0.4.1-beta.1
    ```
  </Tab>

  <Tab value="python" title="Python">
    ```python theme={null}
    pip install onepassword-sdk==0.4.1b1
    ```
  </Tab>
</Tabs>

Before you begin, [follow the steps to get started](/sdks#get-started) with a 1Password SDK. The examples on this page assume you have an initialized `client` instance. For example:

<Tabs groupId="sdks">
  <Tab value="go" title="Go">
    {/* markdownlint-disable MD046 */}

    ```go theme={null}
    package main

    import (
    	"context"

    	"github.com/1password/onepassword-sdk-go"
    )

    func main() {
    	// TODO: Initialize client using your preferred authentication method (see Overview > Get started)
    	client, err := onepassword.NewClient(context.Background(),
    	)
    	if err != nil {
    		panic(err)
    	}

    	// Your code here
    }

    ```
  </Tab>

  <Tab value="js" title="JavaScript">
    {/* markdownlint-disable MD046 */}

    <Tabs groupId="sdk-before-start-js">
      <Tab value="esm" title="ES Modules">
        ```javascript theme={null}
        import sdk from "@1password/sdk";

        async function main() {
          const client = await sdk.createClient({
            // TODO: Initialize client using your preferred authentication method (see Overview > Get started)
          });

          // Your code here
        }

        main().catch(console.error);

        ```
      </Tab>

      <Tab value="cjs" title="CommonJS">
        ```javascript theme={null}
        const sdk = require("@1password/sdk");

        async function main() {
          const client = await sdk.createClient({
            // TODO: Initialize client using your preferred authentication method (see Overview > Get started)
          });

          // Your code here
        }

        main().catch(console.error);

        ```
      </Tab>
    </Tabs>
  </Tab>

  <Tab value="python" title="Python">
    {/* markdownlint-disable MD046 */}

    ```python theme={null}
    import asyncio

    from onepassword import Client  # Also import DesktopAuth for desktop app authentication.

    async def main():
        # TODO: Initialize client using your preferred authentication method (see Overview > Get started)
        client = await Client.authenticate(...)

        # Your code here

    asyncio.run(main())

    ```
  </Tab>
</Tabs>

## Read environment variables

<Tabs groupId="sdks">
  <Tab value="go" title="Go" default>
    To read environment variables stored in an Environment, use the [`GetVariables()`](https://github.com/1Password/onepassword-sdk-go/blob/beta/environments.go#L14) method. Replace `<your-environment-id>` with the [Environment's ID](#appendix-get-an-environments-id).

    ```go theme={null}
    res, err := client.Environments().GetVariables(context.Background(), "<your-environment-id>")
    if err != nil {
    	panic(err)
    }
    for _, env := range res.Variables {
    	fmt.Printf("Variable %s: %s (hidden: %t)\n", env.Name, env.Value, env.Masked)
    }
    ```

    The method returns a [`GetVariablesResponse`](https://github.com/1Password/onepassword-sdk-go/blob/beta/types.go#L67) struct that contains a list of the environment variables stored in the Environment.

    ```go theme={null}
    // Response containing the full set of environment variables from an Environment.
    type GetVariablesResponse struct {
    	// List of environment variables.
    	Variables []EnvironmentVariable `json:"variables"`
    }
    ```

    Each [`EnvironmentVariable`](https://github.com/1Password/onepassword-sdk-go/blob/beta/types.go#L32) struct in the response contains the following:

    * **Name**: The environment variable's name (for example, `DB_HOST`).
    * **Value**: The environment variable's value.
    * **Masked**: A boolean that indicates whether the value is hidden by default in the 1Password app.

    ```go theme={null}
    // Represents an environment variable (name:value pair) and its masked state
    type EnvironmentVariable struct {
    	// An environment variable's name
    	Name string `json:"name"`
    	// An environment variable's value
    	Value string `json:"value"`
    	// An environment variable's masked state
    	Masked bool `json:"masked"`
    }
    ```
  </Tab>

  <Tab value="js" title="JavaScript">
    To read environment variables stored in an Environment, use the [`getVariables()`](https://github.com/1Password/onepassword-sdk-js/blob/beta/client/src/environments.ts#L11) method. Replace `<your-environment-id>` with the [Environment's ID](#appendix-get-an-environments-id).

    ```js theme={null}
    const res = await client.environments.getVariables("<your-environment-id>");
    for (const env of res.variables) {
      console.log(`Variable ${env.name}: ${env.value} (hidden: ${env.masked})`);
    }
    ```

    The method returns a [`GetVariablesResponse`](https://github.com/1Password/onepassword-sdk-js/blob/beta/client/src/types.ts#L68) object that contains a list of the environment variables stored in the Environment.

    ```js theme={null}
    /** Response containing the full set of environment variables from an Environment. */
    interface GetVariablesResponse {
      /** List of environment variables. */
      variables: EnvironmentVariable[];
    }
    ```

    Each [`EnvironmentVariable`](https://github.com/1Password/onepassword-sdk-js/blob/beta/client/src/types.ts#L29) object in the response contains the following:

    * **Name**: The environment variable's name (for example, `DB_HOST`).
    * **Value**: The environment variable's value.
    * **Masked**: A boolean that indicates whether the value is hidden by default in the 1Password app.

    ```js theme={null}
    /** Represents an environment variable (name:value pair) and its masked state */
    export interface EnvironmentVariable {
      /** An environment variable's name */
      name: string;
      /** An environment variable's value */
      value: string;
      /** An environment variable's masked state */
      masked: boolean;
    }
    ```
  </Tab>

  <Tab value="python" title="Python">
    To read environment variables stored in an Environment, use the [`get_variables()`](https://github.com/1Password/onepassword-sdk-python/blob/beta/src/onepassword/environments.py#L10) method. Replace `ENVIRONMENT_ID` with the [Environment's ID](#appendix-get-an-environments-id).

    ```python theme={null}
    environment_id = "your-environment-id"

    res = await client.environments.get_variables(environment_id)
    for env in res.variables:
        print(f"Environment {env.name}: {env.value} (hidden: {env.masked})")
    ```

    The method returns a [`GetVariablesResponse`](https://github.com/1Password/onepassword-sdk-python/blob/beta/src/onepassword/types.py#L162) object that contains a list of the environment variables stored in the Environment.

    ```python theme={null}
    class GetVariablesResponse(BaseModel):
        """
        Response containing the full set of environment variables from an Environment.
        """

        variables: List[EnvironmentVariable]
        """
        List of environment variables.
        """
    ```

    Each [`EnvironmentVariable`](https://github.com/1Password/onepassword-sdk-python/blob/beta/src/onepassword/types.py#L91) object in the response contains the following:

    * **Name**: The environment variable's name (for example, `DB_HOST`).
    * **Value**: The environment variable's value.
    * **Masked**: A boolean that indicates whether the value is hidden by default in the 1Password app.

    ```python theme={null}
    class EnvironmentVariable(BaseModel):
        """
        Represents an environment variable (name:value pair) and its masked state
        """

        name: str
        """
        An environment variable's name
        """
        value: str
        """
        An environment variable's value
        """
        masked: bool
        """
        An environment variable's masked state
        """
    ```
  </Tab>
</Tabs>

<Note>
  1Password Environment variables are masked by default. To change this:

  1. Open and unlock the 1Password desktop app.
  2. Select **Developer** > **View Environments**.
  3. Choose the Environment, select **Edit**, then select the vertical ellipsis <Icon icon="ellipsis-v" /> next to the variable and select **Show value by default**.
</Note>

<h2 id="appendix-get-an-environments-id">
  Appendix: Get an Environment's ID
</h2>

To read environment variables from a 1Password Environment, you'll need its unique identifier (ID). You can find this ID in the [1Password desktop app](https://1password.com/downloads/):

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