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

# Load secrets using 1Password SDKs

You can use 1Password SDKs to securely load secrets into your code with [secret references](/cli/secret-reference-syntax/). You can retrieve secrets from [supported field types](/sdks/concepts#field-types). You can also retrieve one-time passwords using the [`otp` attribute parameter](/sdks/concepts#query-parameters).

A valid secret reference should use the syntax:

```
op://<vault>/<item>/[section/]<field>
```

To get a one-time password, append the `?attribute=otp` query parameter to a secret reference that points to a one-time password field in 1Password:

```
op://<vault>/<item>/[section/]one-time password?attribute=otp
```

## Before you get started

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>

<Tip>
  See the examples folder in the 1Password [Go](https://github.com/1Password/onepassword-sdk-go/tree/main/example), [JavaScript](https://github.com/1Password/onepassword-sdk-js/tree/main/examples), or [Python](https://github.com/1Password/onepassword-sdk-python/tree/main/example) SDK GitHub repository for full example code you can quickly clone and test in your project.
</Tip>

## Load a secret from 1Password

Replace the placeholder [secret reference](/sdks/concepts#secret-references) in the example with a secret reference URI that specifies the vault, item, section (if applicable), and field where the secret is saved in your 1Password account.

If you have multiple vaults, items, or fields that share the same name, use a [unique identifier](/sdks/concepts#unique-identifiers) instead of the name in the secret reference.

<Tabs groupId="sdks">
  <Tab value="go" title="Go">
    To retrieve a secret and print its value, use the [`Secrets().Resolve()`](https://github.com/1Password/onepassword-sdk-go/blob/main/secrets.go#L34) method.

    The SDK resolves the secret reference and returns the value of the 1Password field it references. You can then use this value in your code, like to authenticate to another service.

    ```go theme={null}
    // Retrieves a secret from 1Password.
    // Takes a secret reference as input and returns the secret to which it points.
    secret, err := client.Secrets().Resolve(context.Background(), fmt.Sprintf("op://%s/%s/%s", vaultID, itemID, fieldID))
    if err != nil {
    	panic(err)
    }
    fmt.Println(secret)
    ```
  </Tab>

  <Tab value="js" title="JavaScript">
    To retrieve a secret and print its value, use the [`secrets.resolve()`](https://github.com/1Password/onepassword-sdk-js/blob/main/client/src/secrets.ts#L35) method.

    The SDK resolves the secret reference and returns the value of the 1Password field it references. You can then use this value in your code, like to authenticate to another service.

    ```js theme={null}
    // Fetches a secret.
    const secret = await client.secrets.resolve(
      "op://" + item.vaultId + "/" + item.id + "/username",
    );
    console.log(secret);
    ```
  </Tab>

  <Tab value="python" title="Python">
    To retrieve a secret and print its value, use the [`secrets.resolve()`](https://github.com/1Password/onepassword-sdk-python/blob/main/src/onepassword/secrets.py#L20) method.

    The SDK resolves the secret reference and returns the value of the 1Password field it references. You can then use this value in your code, like to authenticate to another service.

    ```python theme={null}
    # Fetch a secret using a secret reference
    value = await client.secrets.resolve(
        f"op://vault-id/item-id/field-id"
    )
    print(value)
    ```
  </Tab>
</Tabs>

## Retrieve multiple secrets

<Tabs groupId="sdks">
  <Tab value="go" title="Go">
    Use the [`Secrets().ResolveAll()`](https://github.com/1Password/onepassword-sdk-go/blob/main/secrets.go#L50) method to retrieve secrets from 1Password in bulk, improving the performance of the operation.

    Replace the placeholder [secret references](/sdks/concepts#secret-references) in the example with secret reference URIs that specify the vault, item, section (if applicable), and field where the secrets are saved in your 1Password account.

    ```go theme={null}
    // Retrieves multiple secrets from 1Password.
    // Takes multiple secret references as input and returns the secret to which it points.
    secret, _ := client.Secrets().ResolveAll(
    	context.Background(),
    	[]string{
    		fmt.Sprintf("op://%s/%s/%s", vaultID, itemID, fieldID),
    		fmt.Sprintf("op://%s/%s/%s", vaultID, itemID, fieldID2),
    	},
    )
    for _, s := range secret.IndividualResponses {
    	if s.Error != nil {
    		panic(string(s.Error.Type))
    	}
    	fmt.Println(s.Content.Secret)
    }
    ```
  </Tab>

  <Tab value="js" title="JavaScript">
    Use the [`secrets.resolveAll()`](https://github.com/1Password/onepassword-sdk-js/blob/main/client/src/secrets.ts#L22) method to retrieve secrets from 1Password in bulk, improving the performance of the operation.

    Replace the placeholder [secret references](/sdks/concepts#secret-references) in the example with secret reference URIs that specify the vault, item, section (if applicable), and field where the secrets are saved in your 1Password account.

    ```js theme={null}
    try {
      // Fetch all secrets
      const secrets = await client.secrets.resolveAll([
        "op://7turaasywpymt3jecxoxk5roli/hdvxoumwprditdustkxv7d3dqy/username",
        "op://7turaasywpymt3jecxoxk5roli/hdvxoumwprditdustkxv7d3dqy/password",
      ]);

      for (const [_, response] of Object.entries(secrets.individualResponses)) {
        if (response.error) {
          console.error("Error resolving secret:", response.error);
          continue;
        }

        console.log(response.content.secret);
      }
    } catch (error) {
      console.error("An unexpected error occurred:", error);
    }
    ```
  </Tab>

  <Tab value="python" title="Python">
    Use the [`secrets.resolve_all()`](https://github.com/1Password/onepassword-sdk-python/blob/main/src/onepassword/secrets.py#L39) method to retrieve secrets from 1Password in bulk, improving the performance of the operation.

    Replace the placeholder [secret references](/sdks/concepts#secret-references) in the example with secret reference URIs that specify the vault, item, section (if applicable), and field where the secrets are saved in your 1Password account.

    ```python theme={null}
    # Fetch multiple secrets using secret references
    secrets = await client.secrets.resolve_all(
        [
            f"op://vault-id/item-id/field-id",
            f"op://vault-id/item-id/field-id2",
        ]
    )
    for secret in secrets.individual_responses.values():
        if secret.error is not None:
            print(str(secret.error))
        else:
            print(secret.content.secret)
    ```
  </Tab>
</Tabs>

## Validate a secret reference

<Tabs groupId="sdks">
  <Tab value="go" title="Go">
    You can use the [`ValidateSecretReference()`](https://github.com/1Password/onepassword-sdk-go/blob/main/secrets.go#L66) method to make sure that your [secret reference](/cli/secret-reference-syntax/) is formatted correctly.

    ```go theme={null}
    // Validate your secret reference
    err := onepassword.Secrets.ValidateSecretReference(context.Background(), fmt.Sprintf("op://%s/%s/%s", vaultID, itemID, fieldID))
    if err != nil {
    	panic(err)
    }
    ```
  </Tab>

  <Tab value="js" title="JavaScript">
    You can use the [`validateSecretReference()`](https://github.com/1Password/onepassword-sdk-js/blob/main/client/src/secrets.ts#L79) method to make sure that your [secret reference](/cli/secret-reference-syntax/) is formatted correctly.

    ```js theme={null}
    // Validate a secret reference
    try {
      sdk.Secrets.validateSecretReference("op://vault/item/field");
    } catch (error) {
      console.error(error);
    }
    ```
  </Tab>

  <Tab value="python" title="Python">
    You can use the [`validate_secret_reference()`](https://github.com/1Password/onepassword-sdk-python/blob/main/src/onepassword/secrets.py#L59) method to make sure that your [secret reference](/cli/secret-reference-syntax/) is formatted correctly.

    ```python theme={null}
    from onepassword import Secrets

    # Validate a secret reference
    try:
        Secrets.validate_secret_reference("op://vault-id/item-id/field-id")
    except Exception as error:
        print(error)
    ```
  </Tab>
</Tabs>

If the secret reference is formatted incorrectly, the SDK will return an error that describes the syntax problem. Learn more about [secret references](/cli/secret-reference-syntax).

## Learn more

* [Secret reference syntax](/cli/secret-reference-syntax/)
* [Manage items using 1Password SDKs](/sdks/manage-items/)
* [List vaults and items using 1Password SDKs](/sdks/list-vaults-items/)
* [Share items using 1Password SDKs](/sdks/share-items/)
* [Workflow: Build integrations with 1Password](/get-started/build-integrations)
* [Workflow: Secure your developer secrets](/get-started/secure-developer-secrets)
