web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

Access Dataverse using python

(3) ShareShare
ReportReport
Posted on by 12
How do I access Dataverse using Python? What are the prerequisites required for connecting to Dataverse using an App service principal, Client ID, and Secret?
I have the same question (0)
  • Suggested answer
    DAnny3211 Profile Picture
    10,421 Moderator on at
    Access Dataverse using python

    Hi Syazma,

    Great question! You can access Dataverse using Python by authenticating with an App Registration in Azure using a Client ID and Client Secret. Here's a general guide:

    Prerequisites

    1. Azure App Registration

      • Register an app in Azure Active Directory.
      • Note the Client ID, Tenant ID, and Client Secret.
      • Assign API permissions for Dataverse (Dynamics CRM).
    2. Dataverse Environment URL

      • Typically looks like:
        https://<your_org>.crm.dynamics.com
    3. Python Libraries

      • Use msal for authentication and requests for API calls.

    Sample Python Code

    import msal
    import requests
     
    client_id = "<your_client_id>"
    client_secret = "<your_client_secret>"
    tenant_id = "<your_tenant_id>"
    authority = f"https://login.microsoftonline.com/{tenant_id}"
    scope = ["https://your_org.crm.dynamics.com/.default"]
     
    app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
    token_response = app.acquire_token_for_client(scopes=scope)
     
    access_token = token_response['access_token']
    headers = {
        'Authorization': f'Bearer {access_token}',
        'Content-Type': 'application/json',
        'OData-MaxVersion': '4.0',
        'OData-Version': '4.0'
    }
     
    url = "https://your_org.crm.dynamics.com/api/data/v9.2/accounts"
    response = requests.get(url, headers=headers)
    print(response.json())

    Let me know if this helps, and please mark the response as helpful if it answered your question 😊

    Best regards!

  • Suggested answer
    fabipfr Profile Picture
    13 on at
    Access Dataverse using python
    I‘m assuming you want to work with the data from Dataverse tables.


    I developed a python package for that. It leverages pandas DataFrames to handle easy data transformations, imports and exports. 

    https://github.com/fabipfr/DataversePython

    this package became my trusty helper for migrations to Dynamics 365 since pandas lets you import and transform data from virtually anything :) 


    check my blog for detailed examples of the features:
    https://blog.fabianpfriem.com/


    hope this helps someone out there struggling with excel imports or dataflows..

    I‘m assuming you want to work with the data from Dataverse tables.


    I developed a python package for that. It leverages pandas DataFrames to handle easy data transformations, imports and exports. 

    https://github.com/fabipfr/DataversePython


    this package became my trusty helper for migrations to Dynamics 365 since pandas lets you import and transform data from virtually anything :) 


    check my blog for detailed examples of the features:
    https://blog.fabianpfriem.com/


    hope this helps someone out there struggling with excel imports or dataflows..
  • Vahid Ghafarpour Profile Picture
    11,910 Super User 2025 Season 2 on at
    Access Dataverse using python
     

    ** Please don't forget to close up the thread here by Like and verify it as an answer if it was solved **

  • syamna Profile Picture
    12 on at
    Access Dataverse using python
    Hi VaHiX,
        Thanks for your response,


    Here it uses interactive MSAL authentication it doesn't work in my case I need service-to-service communication or automated tasks.
    I tried,
    app = msal.ConfidentialClientApplication(
    client_id,
    authority=authority_url,
    client_credential=client_secret,
    )
    token_response = app.acquire_token_for_client(scopes=scope)

    it is able to generate token but failed at below,

    dataverse_url = 'https://<org-name>.crm.dynamics.com/api/data/v9.2/'
    headers = {
    'Authorization': f'Bearer {token}',
    'OData-MaxVersion': '4.0',
    'OData-Version': '4.0',
    'Accept': 'application/json',
    'Content-Type': 'application/json'
    }
    response = requests.get(f"{dataverse_url}accounts", headers=headers)

    I am getting error:
    requests.exceptions.ConnectionError: HTTPConnectionPool(host='<org-name>.crm.dynamics.com', port=443): Max retries exceeded with url: /api/data/v9.2/accounts (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f544af9a5d0>: Failed to establish a new connection: getaddrinfo failed',))

    What might be the reason? Do I need to get any other permission for service-to-service communication or any other set up required here?
  • Vahid Ghafarpour Profile Picture
    11,910 Super User 2025 Season 2 on at
    Access Dataverse using python
  • Martin Dráb Profile Picture
    235,412 Most Valuable Professional on at
    Access Dataverse using python
    Dataverse comes with Web API, which could be easily used from Python. But... there is also pyDataverse project which could make things even simpler. (I have no experience with it.)
     
    Here are OAuth libraries for Python: https://oauth.net/code/python/.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Mansi Soni – Community Spotlight

We are honored to recognize Mansi Soni as our August 2025 Community…

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Mansi Soni Profile Picture

Mansi Soni 243 Super User 2025 Season 2

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 158 Most Valuable Professional

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 95 Super User 2025 Season 2

Product updates

Dynamics 365 release plans