Skip to main content

Python SDK for accessing Key Vault API keys and values

Project description

Key Vault Python SDK

A Python SDK for securely accessing your Key Vault API keys and secrets.

Installation

pip install amay-key-vault-sdk

Quick Start

from key_vault_sdk import KeyVault

# Initialize the SDK
kv = KeyVault(
    api_url="https://yourdomain.com/api",
    token="your-api-token"
)

# Get a key by name
api_key = kv.get_key_by_name("folder-id", "stripe-secret-key")
print(f"API Key: {api_key}")

# List all keys in a folder
result = kv.list_keys(folder_id="folder-id", limit=50)
print(f"Found {len(result['keys'])} keys")

# Get multiple keys at once
keys = kv.get_multiple_keys(
    folder_id="folder-id",
    key_names=["stripe-key", "database-password", "api-secret"]
)
print(f"Retrieved {len(keys)} keys")

Features

  • 🔐 Secure Access: All keys are encrypted and securely transmitted
  • 📁 Folder Support: Organize keys in hierarchical folders
  • 🔍 Search & Filter: Find keys by name, type, or tags
  • 📊 Statistics: Get usage statistics and folder information
  • 🔄 Auto Refresh: Automatic token refresh handling
  • 🛡️ Error Handling: Comprehensive error handling with specific exceptions

API Reference

Authentication

from key_vault_sdk import KeyVault

kv = KeyVault(
    api_url="https://yourdomain.com/api",
    token="your-api-token",
    timeout=30  # Optional: request timeout in seconds
)

Key Operations

Get Key by Name

# Get a key's value by name (convenience method)
api_key = kv.get_key_by_name("folder-id", "stripe-secret-key")

Get Key by ID

# Get key metadata only
key = kv.get_key("key-id")

# Get key with decrypted value
key_with_value = kv.get_key("key-id", include_value=True)
print(f"Key: {key_with_value['name']}, Value: {key_with_value['value']}")

List Keys

# List keys in a folder with pagination
result = kv.list_keys(
    folder_id="folder-id",
    limit=20,  # Number of keys to return
    offset=0   # Number of keys to skip
)

print(f"Found {result['total']} keys")
for key in result['keys']:
    print(f"- {key['name']} ({key['type']})")

Get Multiple Keys

# Get multiple keys by name
keys = kv.get_multiple_keys(
    folder_id="folder-id",
    key_names=["stripe-key", "database-password", "api-secret"]
)

for name, value in keys.items():
    if value:
        print(f"{name}: {value}")
    else:
        print(f"{name}: Not found")

Folder Operations

List Folders

# List all folders with hierarchical structure
folders = kv.list_folders()
print(f"Found {len(folders['folders'])} root folders")

# List folders within a specific project
project_folders = kv.list_folders(project_id="project-id")

List Projects

# List only root folders (projects)
projects = kv.list_projects()
for project in projects:
    print(f"Project: {project['name']} (ID: {project['id']})")

Get Folder Details

# Get a specific folder with its contents
folder_data = kv.get_folder("folder-id")
print(f"Folder: {folder_data['folder']['name']}")
print(f"Contains {len(folder_data['keys'])} keys")

Search Operations

Search Keys

# Search for keys across all folders
results = kv.search_keys(
    search="database",
    key_type="PASSWORD",
    favorite=True,
    limit=20
)

print(f"Found {len(results['keys'])} database passwords")

Utility Methods

Test Connection

# Test the connection to the Key Vault API
if kv.test_connection():
    print("Connection successful!")
else:
    print("Connection failed!")

Get Statistics

# Get folder and key statistics
stats = kv.get_stats()
print(f"Total keys: {stats['totalKeys']}")
print(f"Total folders: {stats['folders']}")

Error Handling

The SDK provides specific exception types for different error scenarios:

from key_vault_sdk import KeyVaultError, KeyVaultAuthError, KeyVaultNotFoundError

try:
    key = kv.get_key("key-id")
except KeyVaultAuthError:
    print("Authentication failed - check your token")
except KeyVaultNotFoundError:
    print("Key not found")
except KeyVaultError as e:
    print(f"API error: {e}")

Examples

Complete Example

from key_vault_sdk import KeyVault

def main():
    # Initialize SDK
    kv = KeyVault(
        api_url="https://yourdomain.com/api",
        token="your-api-token"
    )
    
    try:
        # Test connection
        if not kv.test_connection():
            print("Failed to connect to Key Vault")
            return
        
        # List projects
        projects = kv.list_projects()
        print(f"Available projects: {len(projects)}")
        
        # Get keys from first project
        if projects:
            project_id = projects[0]['id']
            result = kv.list_keys(folder_id=project_id, limit=10)
            
            print(f"Keys in {projects[0]['name']}:")
            for key in result['keys']:
                print(f"- {key['name']} ({key['type']})")
                
                # Get key value if needed
                if key['type'] == 'API_KEY':
                    key_with_value = kv.get_key(key['id'], include_value=True)
                    print(f"  Value: {key_with_value['value']}")
    
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    main()

Development

Local Testing

For local development, use the local server URL:

kv = KeyVault(
    api_url="http://localhost:3000/api",
    token="your-local-token"
)

Running Tests

cd python-sdk
python -m pytest tests/

License

MIT License - see LICENSE file for details.

Support

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

amay_key_vault_sdk-1.0.4.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

amay_key_vault_sdk-1.0.4-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file amay_key_vault_sdk-1.0.4.tar.gz.

File metadata

  • Download URL: amay_key_vault_sdk-1.0.4.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for amay_key_vault_sdk-1.0.4.tar.gz
Algorithm Hash digest
SHA256 5674e187527f2b55f1211fdcac36efe049c08c021d5238382da6ccb3e351b3c4
MD5 7296ee73755ae7565dd529eea98adcef
BLAKE2b-256 1f697b16d7788c5fcc6d06e409013e63f937f05910c5bd9e8456e3781137cf0c

See more details on using hashes here.

File details

Details for the file amay_key_vault_sdk-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for amay_key_vault_sdk-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d3052b245cbf1636f583f9faca7e70dd0f73537967c76cd7ac9bcaea634346a1
MD5 87d8c34abb360981e538510b380fad0e
BLAKE2b-256 845989ffd12d6f32ac2772f4c694c9b9c18265c73ee9cddf52415dcc46e67fae

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