{"library":"redis-entraid","title":"Redis Entra ID","description":"The `redis-entraid` Python package simplifies authentication with Azure Managed Redis and Azure Cache for Redis using Microsoft Entra ID (formerly Azure Active Directory). It handles fetching and renewing authentication tokens in the background, building on `redis-py`. The current version is 1.1.2, released on March 26, 2026. This library appears to have a regular release cadence with several updates in the past year.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install redis-entraid"],"cli":null},"imports":["from redis import Redis","from redis_entraid.cred_provider import create_from_default_azure_credential","from azure.identity import DefaultAzureCredential"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nfrom redis import Redis\nfrom azure.identity import DefaultAzureCredential\nfrom redis_entraid.cred_provider import create_from_default_azure_credential\n\n# --- Environment Variables (for DefaultAzureCredential to pick up) ---\n# Set these in your environment, e.g., in a .env file or directly:\n# os.environ['AZURE_TENANT_ID'] = 'YOUR_TENANT_ID'\n# os.environ['AZURE_CLIENT_ID'] = 'YOUR_CLIENT_ID'\n# os.environ['AZURE_CLIENT_SECRET'] = 'YOUR_CLIENT_SECRET'\n# OR ensure a Managed Identity is assigned to your application/VM.\n\n# --- Redis Connection Details ---\n# Your Azure Managed Redis or Azure Cache for Redis hostname and port\n# Default port for Azure Managed Redis is 10000, for Azure Cache for Redis it's 6380.\nREDIS_HOST = os.environ.get('REDIS_HOST', 'your_redis_cache_name.redis.cache.windows.net')\nREDIS_PORT = int(os.environ.get('REDIS_PORT', 10000)) \n\ndef connect_with_entra_id():\n    try:\n        # DefaultAzureCredential will attempt to authenticate via various methods\n        # including environment variables, managed identity, etc.\n        # Scopes are crucial for Entra ID authentication with Redis.\n        credential_provider = create_from_default_azure_credential(\n            (\"https://redis.azure.com/.default\",)\n        )\n        \n        # Azure enforces TLS for Entra ID authentication.\n        # decode_responses=True automatically decodes responses to Python strings.\n        r = Redis(\n            host=REDIS_HOST,\n            port=REDIS_PORT,\n            ssl=True,\n            decode_responses=True,\n            credential_provider=credential_provider\n        )\n\n        # Test the connection\n        if r.ping():\n            print(f\"Successfully connected to Redis at {REDIS_HOST}:{REDIS_PORT} with Entra ID.\")\n            r.set(\"mykey\", \"Hello from redis-entraid!\")\n            value = r.get(\"mykey\")\n            print(f\"Retrieved value: {value}\")\n        else:\n            print(\"Redis ping failed.\")\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n\nif __name__ == \"__main__\":\n    connect_with_entra_id()","lang":"python","description":"This quickstart demonstrates how to connect to an Azure Managed Redis or Azure Cache for Redis instance using Microsoft Entra ID authentication with `redis-entraid` and `DefaultAzureCredential`. Ensure you have configured environment variables for `DefaultAzureCredential` (e.g., `AZURE_TENANT_ID`, `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`) or that your application runs with an assigned Managed Identity. Replace `your_redis_cache_name.redis.cache.windows.net` with your actual Redis endpoint and set the correct port (e.g., 10000 for Azure Managed Redis or 6380 for Azure Cache for Redis).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}