1Password Python SDK

0.4.0 · active · verified Tue Apr 14

The 1Password Python SDK (version 0.4.0) offers programmatic read and write access to your secrets, vaults, and user/group permissions within 1Password. It supports authentication via the 1Password desktop app or 1Password Service Accounts, and is currently in its 0.x version series, indicating a rapid release cadence with ongoing feature additions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with the 1Password SDK using a Service Account token provided via an environment variable (`OP_SERVICE_ACCOUNT_TOKEN`). It then shows how to resolve a secret using a 1Password secret reference and how to list available vaults. Remember to replace placeholder values like 'op://MyVault/MyLoginItem/password' and ensure your service account has the necessary permissions.

import asyncio
import os
from onepassword.client import Client
from onepassword.models import FieldPurpose

async def main():
    # Authenticate using a 1Password Service Account token from environment variable
    token = os.environ.get("OP_SERVICE_ACCOUNT_TOKEN")
    if not token:
        print("Error: OP_SERVICE_ACCOUNT_TOKEN environment variable not set.")
        return

    # Connect to 1Password with your integration details
    client = await Client.authenticate(
        auth=token,
        integration_name="My Test Integration",
        integration_version="v1.0.0",
    )

    # Example: Retrieve a secret using a secret reference
    # Replace 'op://vault/item/field' with your actual secret reference URI
    try:
        secret_value = await client.secrets.resolve("op://MyVault/MyLoginItem/password")
        print(f"Retrieved secret: {secret_value}")

        # Example: List vaults (requires appropriate permissions)
        vaults = await client.vaults.list()
        print(f"Found {len(vaults)} vaults:")
        for vault in vaults:
            print(f"- {vault.name} (ID: {vault.id})")

    except Exception as e:
        print(f"An error occurred: {e}")
    finally:
        # Ensure the client connection is closed (if applicable, though often handled internally)
        pass

if __name__ == '__main__':
    asyncio.run(main())

view raw JSON →