Skip to main content

Python wrapper for 1Password CLI (op) for managing secrets

Project description

op-python

A Python wrapper for the 1Password CLI (op) tool, providing a clean interface for managing secrets programmatically.

Features

  • Token-based authentication - Supports both Service Account tokens and 1Password Connect
  • Flexible .env support - Optional dotenv loading with customizable paths and override behavior
  • Comprehensive API - Get items, list vaults, manage secrets, and more
  • Type hints - Full typing support for better IDE experience
  • Error handling - Clear error messages and custom exceptions

Installation

pip install op-python

Quick Start

Authentication

Choose one of the following authentication methods:

Option 1: Service Account Token

export OP_SERVICE_ACCOUNT_TOKEN="ops_your_service_account_token"

Option 2: 1Password Connect

export OP_CONNECT_HOST="https://your-connect-server.com"
export OP_CONNECT_TOKEN="your_connect_token"

Basic Usage

from op_python import OpClient, OnePasswordError

try:
    # Initialize client
    op = OpClient()
    
    # Get a secret
    password = op.get_secret("op://Personal/MyApp/password")
    
    # Get an item
    item = op.get_item("MyApp", vault="Personal")
    
    # List items in a vault
    items = op.list_items(vault="Personal")
    
    # List all vaults
    vaults = op.list_vaults()
    
except OnePasswordError as e:
    print(f"Error: {e}")

Configuration Options

Using .env Files

# Enable .env loading (disabled by default)
op = OpClient(use_dotenv=True)

# Custom .env file path
op = OpClient(use_dotenv=True, dotenv_path="config/production.env")

# Let .env override environment variables
op = OpClient(use_dotenv=True, dotenv_override=True)

.env File Format

Create a .env file in your project root:

# Service Account Authentication
OP_SERVICE_ACCOUNT_TOKEN=ops_your_service_account_token

# OR Connect Authentication
# OP_CONNECT_HOST=https://your-connect-server.com
# OP_CONNECT_TOKEN=your_connect_token

API Reference

OpClient

Constructor

OpClient(
    op_path: str = "op",
    use_dotenv: bool = False,
    dotenv_path: Union[str, Path] = ".env",
    dotenv_override: bool = False
)
  • op_path: Path to the op CLI executable
  • use_dotenv: Enable loading environment variables from .env file
  • dotenv_path: Path to .env file (only used if use_dotenv=True)
  • dotenv_override: Whether .env values override existing environment variables

Methods

get_secret(secret_reference: str) -> str Get a secret using 1Password's secret reference syntax.

password = op.get_secret("op://vault/item/field")

get_item(item_identifier: str, vault: Optional[str] = None) -> Dict[str, Any] Get a complete item with all its fields.

item = op.get_item("MyApp", vault="Personal")

list_items(vault: Optional[str] = None, categories: Optional[List[str]] = None) -> List[Dict[str, Any]] List items, optionally filtered by vault and categories.

items = op.list_items(vault="Personal", categories=["Login", "Password"])

list_vaults() -> List[Dict[str, Any]] List all available vaults.

vaults = op.list_vaults()

create_item(title: str, category: str = "Login", vault: Optional[str] = None, **fields) -> Dict[str, Any] Create a new item.

item = op.create_item(
    title="New App",
    category="Login",
    vault="Personal",
    username="user@example.com",
    password="secret123"
)

delete_item(item_identifier: str, vault: Optional[str] = None) -> str Delete an item.

op.delete_item("MyApp", vault="Personal")

Requirements

  • Python 3.9+
  • 1Password CLI (op) installed and accessible in PATH
  • Valid 1Password authentication (Service Account token or Connect credentials)

Development

# Clone the repository
git clone https://github.com/yourusername/op-python.git
cd op-python

# Install dependencies (including dev tools)
poetry install

# Run tests
poetry run pytest

# Format code
poetry run black .

# Type checking
poetry run mypy src/

# Build package
poetry build

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

op_python-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

op_python-0.1.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file op_python-0.1.0.tar.gz.

File metadata

  • Download URL: op_python-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-28-cloud-amd64

File hashes

Hashes for op_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a2774f030985a06ea7e1158b00dae08a028b99ddd65c2011bb111667d9f8f314
MD5 56092dadeef14b6fd90d05018cab17e7
BLAKE2b-256 4cfc831c25fa6b1f32840cbd5383122400ac289247d62050d2078a6402bdfb9c

See more details on using hashes here.

File details

Details for the file op_python-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: op_python-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.2 Linux/6.1.0-28-cloud-amd64

File hashes

Hashes for op_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ad0edf375716fa7baf11691b5fc78f8b241838d550a33c9006c8da6805aa5222
MD5 219469b5dcb8d27321eb7f782b177912
BLAKE2b-256 616e95c11ab92adf3545ce67559c44ab3862d489cd3c9210e755c06569fa3a7c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page