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.
You can use 1Password SDKs to securely load secrets into your code with secret references. You can retrieve secrets from supported field types. You can also retrieve one-time passwords using the otp attribute parameter.
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 with a 1Password SDK. The examples on this page assume you have an initialized client instance. For example:
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
}
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);
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);
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())
See the examples folder in the 1Password Go, JavaScript, or Python SDK GitHub repository for full example code you can quickly clone and test in your project.
Load a secret from 1Password
Replace the placeholder secret reference 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 instead of the name in the secret reference.
To retrieve a secret and print its value, use the Secrets().Resolve() 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.// 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)
To retrieve a secret and print its value, use the secrets.resolve() 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.// Fetches a secret.
const secret = await client.secrets.resolve(
"op://" + item.vaultId + "/" + item.id + "/username",
);
console.log(secret);
To retrieve a secret and print its value, use the secrets.resolve() 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.# Fetch a secret using a secret reference
value = await client.secrets.resolve(
f"op://vault-id/item-id/field-id"
)
print(value)
Retrieve multiple secrets
Use the Secrets().ResolveAll() method to retrieve secrets from 1Password in bulk, improving the performance of the operation.Replace the placeholder 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.// 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)
}
Use the secrets.resolveAll() method to retrieve secrets from 1Password in bulk, improving the performance of the operation.Replace the placeholder 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.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);
}
Use the secrets.resolve_all() method to retrieve secrets from 1Password in bulk, improving the performance of the operation.Replace the placeholder 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.# 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)
Validate a secret reference
You can use the ValidateSecretReference() method to make sure that your secret reference is formatted correctly.// Validate your secret reference
err := onepassword.Secrets.ValidateSecretReference(context.Background(), fmt.Sprintf("op://%s/%s/%s", vaultID, itemID, fieldID))
if err != nil {
panic(err)
}
You can use the validateSecretReference() method to make sure that your secret reference is formatted correctly.// Validate a secret reference
try {
sdk.Secrets.validateSecretReference("op://vault/item/field");
} catch (error) {
console.error(error);
}
You can use the validate_secret_reference() method to make sure that your secret reference is formatted correctly.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)
If the secret reference is formatted incorrectly, the SDK will return an error that describes the syntax problem. Learn more about secret references.
Learn more