{"library":"pymongocrypt","title":"PyMongoCrypt","description":"PyMongoCrypt provides the Python bindings for `libmongocrypt`, a C library that powers MongoDB's Client-Side Field Level Encryption (CSFLE) and Queryable Encryption features. It is currently at version 1.17.0 and releases are typically tied to `libmongocrypt` and PyMongo updates, often occurring every few months.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install pymongocrypt"],"cli":null},"imports":["from pymongo.encryption import ClientEncryption","from pymongocrypt import _lib"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom pymongo import MongoClient\nfrom pymongo.encryption import ClientEncryption\n\n# A 96-byte local master key (for demonstration purposes).\n# In production, use a secure Key Management System (KMS).\nlocal_key = os.environ.get(\"MONGO_LOCAL_MASTER_KEY\", \"A\" * 96).encode('utf-8')\nif len(local_key) != 96:\n    # Generate a temporary key if not set correctly for quick local run\n    local_key = os.urandom(96)\n    print(f\"WARNING: MONGO_LOCAL_MASTER_KEY not set or invalid length. Using a generated temporary key.\")\n\nkms_providers = {\n    \"local\": {\"key\": local_key}\n}\n\n# Key Vault setup\nconnection_string = os.environ.get(\"MONGO_URI\", \"mongodb://localhost:27017/\")\nclient = MongoClient(connection_string)\nkey_vault_db = \"encryption\"\nkey_vault_coll = \"__keyVault\"\nkey_vault_namespace = f\"{key_vault_db}.{key_vault_coll}\"\n\n# Ensure key vault collection exists and has the required index\nclient.get_database(key_vault_db).get_collection(key_vault_coll).create_index(\n    [('keyAltNames', 1)],\n    unique=True,\n    partialFilterExpression={'keyAltNames': {'$exists': True}}\n)\n\n# Initialize ClientEncryption\n# This object implicitly uses the libmongocrypt bindings provided by pymongocrypt.\n# Note: kms_tls_options is required even if empty for local KMS.\nclient_encryption = ClientEncryption(\n    kms_providers=kms_providers,\n    key_vault_namespace=key_vault_namespace,\n    kms_tls_options={},\n    key_vault_client=client\n)\n\n# Create a data key (if one doesn't exist) for encryption\ntry:\n    key_id = client_encryption.get_key_by_alt_name(\"my_data_key\")[\"_id\"]\n    print(f\"Using existing data key ID: {key_id}\")\nexcept Exception: # Key not found, create one\n    key_id = client_encryption.create_data_key(\n        \"local\",\n        key_alt_names=[\"my_data_key\"]\n    )\n    print(f\"Created new data key ID: {key_id}\")\n\n# Encrypt a value\nvalue_to_encrypt = \"My secret data that should be encrypted\"\nencrypted_value = client_encryption.encrypt(\n    value_to_encrypt,\n    key_id=key_id,\n    algorithm=\"AEAD_AES256_CBC_HMAC_SHA521_Deterministic\" # Or Random\n)\nprint(f\"Original value: {value_to_encrypt}\")\nprint(f\"Encrypted value (Binary type): {encrypted_value}\")\n\n# Decrypt the value\ndecrypted_value = client_encryption.decrypt(encrypted_value)\nprint(f\"Decrypted value: {decrypted_value}\")\n\n# Clean up\nclient.drop_database(key_vault_db)\nclient.close()\n","lang":"python","description":"This quickstart demonstrates how to set up a `ClientEncryption` object using a local master key, create a data encryption key, and then encrypt and decrypt a string. This high-level API from `pymongo.encryption` utilizes `pymongocrypt` transparently for the underlying cryptographic operations.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"1.17.0","pypi_latest":"1.17.0","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":3.1,"avg_import_s":null,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"pymongocrypt","exit_code":0,"wheel_type":"sdist","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"39.4M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"pymongocrypt","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":3.4,"import_time_s":null,"mem_mb":null,"disk_size":"48M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"pymongocrypt","exit_code":0,"wheel_type":"sdist","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"42.3M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"pymongocrypt","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":3.1,"import_time_s":null,"mem_mb":null,"disk_size":"51M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"pymongocrypt","exit_code":0,"wheel_type":"sdist","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"33.9M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"pymongocrypt","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":2.8,"import_time_s":null,"mem_mb":null,"disk_size":"42M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"pymongocrypt","exit_code":0,"wheel_type":"sdist","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"33.3M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"pymongocrypt","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":2.7,"import_time_s":null,"mem_mb":null,"disk_size":"42M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"pymongocrypt","exit_code":0,"wheel_type":"sdist","failure_reason":null,"import_side_effects":"broken","install_time_s":null,"import_time_s":null,"mem_mb":null,"disk_size":"39.4M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"pymongocrypt","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"broken","install_time_s":3.7,"import_time_s":null,"mem_mb":null,"disk_size":"48M"}]}}