🔐 How to Securely Use Azure Key Vault Secrets in Microsoft Fabric Notebooks
If you're working with sensitive values like API keys (for OpenAI, Azure, or other services) in Microsoft Fabric, you should never hardcode them in your notebook.
This post shows how to securely retrieve secrets using Azure Key Vault and Fabric’s built-in utility:notebookutils.credentials.getSecret()
✅ Step 1: Create a Secret in Azure Key Vault
Go to the Azure Portal
Create a Key Vault or use an existing one
In the Key Vault, go to Secrets
Click + Generate/Import
Name it something like
openaiapikey
Paste your API key into the value field
Click Create
✅ Step 2: Assign Access Using RBAC
Fabric uses role-based access control (RBAC) to allow secure access to Key Vault secrets.
In the Key Vault, go to Access control (IAM)
Click + Add → Add role assignment
Select:
Role:
Key Vault Secrets User
Assign access to:
Managed identity
In the identity search, locate your Fabric workspace's identity
It may appear as
Microsoft Fabric - <workspace name>
If not visible, refer to this YouTube walkthrough
✅ Step 3: Use Secrets in Fabric Notebook
In your Fabric notebook:
import openai
import pandas as pd
# Use Fabric's notebookutils to pull your Key Vault secret
openai.api_key = notebookutils.credentials.getSecret(
'https://openaisqlbits.vault.azure.net/', # your Key Vault URI
'openaiapikey' # your secret name
)
openai.api_type = "azure"
openai.api_base = "https://openaisuebayes.openai.azure.com/"
openai.api_version = "2023-07-01-preview"
DEPLOYMENT_NAME = "gpt-35-turbo"
def summarise_review(text):
response = openai.ChatCompletion.create(
engine=DEPLOYMENT_NAME,
messages=[{"role": "user", "content": f"Summarise this review: {text}"}]
)
return response['choices'][0]['message']['content']
✅ You’ve now fully secured your API key — no need to store it in your code or environment variables.
🧠 Why This Matters
No more exposing secrets in
.ipynb
files or GitHubFabric notebooks remain clean, repeatable, and safe
Supports real-world production use (like securely connecting to OpenAI, Azure services, REST APIs)
📚 Resources
🔗 Microsoft Docs: Notebook Utilities for Fabric
🎥 YouTube Walkthrough: Using Azure Key Vault secrets in Fabric
✅ Next up: I’ll show how to track OpenAI API costs + token usage so you can see exactly what each analysis is costing 💷