Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
SravyaNeela
Microsoft Employee
Microsoft Employee

How to authenticate to cosmos db using spn in fabric notebook or workspace identity in copy activity

How to authenticate to cosmos db using spn in fabric notebook or workspace identity in copy activity

1 ACCEPTED SOLUTION

this one didn't work for me, below code has worked

pip install azure-cosmos azure-identity
from azure.cosmos import CosmosClient
from azure.identity import ClientSecretCredential
from datetime import datetime
import pandas as pd
from pyspark.sql import *
# SPN credentials
    cosmos_account_uri = "https://{accountName}.documents.azure.com:443/".format(accountName=accountName)

    # Authenticate
    credential = ClientSecretCredential(
        tenant_id=tenant_id,
        client_id=client_id,
        client_secret=client_secret
    )

    # Create Cosmos client
    client = CosmosClient(cosmos_account_uri, credential=credential)
    container = client.get_database_client("database_name").get_container_client("container_name")
    # Construct query
    query = " SELECT * FROM c "

    # Execute query
    items = list(container.query_items(query=query, enable_cross_partition_query=True))

    # Convert to DataFrame
    df = pd.DataFrame(items)
    display(df)
 

View solution in original post

3 REPLIES 3
burakkaragoz
Community Champion
Community Champion

Hi @SravyaNeela ,

 

To authenticate to Cosmos DB from Fabric using either a Service Principal (SPN) or Workspace Managed Identity, here’s what you can do:

Option 1: Using Service Principal in Notebook

In your Fabric Notebook (PySpark or Python), use the following pattern:

from azure.cosmos import CosmosClient

url = "<your-cosmos-db-uri>"
key = "<your-client-secret>"  # or use a credential object if using MSAL
client_id = "<your-client-id>"
tenant_id = "<your-tenant-id>"

# Use MSAL to get token
from msal import ConfidentialClientApplication

app = ConfidentialClientApplication(
    client_id,
    authority=f"https://login.microsoftonline.com/{tenant_id}",
    client_credential=key
)

token = app.acquire_token_for_client(scopes=["https://cosmos.azure.com/.default"])
headers = {"Authorization": f"Bearer {token['access_token']}"}

Then use this token to connect to Cosmos DB REST API or SDK.

Option 2: Using Workspace Identity in Copy Activity

  • In your Data Pipeline, add a Copy Data activity.
  • Choose Cosmos DB as the source or sink.
  • In the Authentication method, select Managed Identity.
  • Make sure your workspace identity has Cosmos DB Built-in Data Reader or Contributor role assigned in Azure IAM for the Cosmos DB account.

 

If my response resolved your query, kindly mark it as the Accepted Solution to assist others. Additionally, I would be grateful for a 'Kudos' if you found my response helpful.

this one didn't work for me, below code has worked

pip install azure-cosmos azure-identity
from azure.cosmos import CosmosClient
from azure.identity import ClientSecretCredential
from datetime import datetime
import pandas as pd
from pyspark.sql import *
# SPN credentials
    cosmos_account_uri = "https://{accountName}.documents.azure.com:443/".format(accountName=accountName)

    # Authenticate
    credential = ClientSecretCredential(
        tenant_id=tenant_id,
        client_id=client_id,
        client_secret=client_secret
    )

    # Create Cosmos client
    client = CosmosClient(cosmos_account_uri, credential=credential)
    container = client.get_database_client("database_name").get_container_client("container_name")
    # Construct query
    query = " SELECT * FROM c "

    # Execute query
    items = list(container.query_items(query=query, enable_cross_partition_query=True))

    # Convert to DataFrame
    df = pd.DataFrame(items)
    display(df)
 

Hi @SravyaNeela,

 

Thank you for reaching out to Microsoft Fabric Community.

 

Thank you for the response and confirming that the above code worked. Since the issue is resolved, I request you to please mark the post as "Accept as Solution" so that other community members who has a similar issue will find it more quickly.

 

Thanks and regards,

Anjan Kumar Chippa

Helpful resources

Announcements
Fabric July 2025 Monthly Update Carousel

Fabric Monthly Update - July 2025

Check out the July 2025 Fabric update to learn about new features.

July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.