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

(5) ShareShare
ReportReport
Posted on by 16
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
    11,276 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,930 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
    16 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,930 Super User 2025 Season 2 on at
    Access Dataverse using python
  • Martin Dráb Profile Picture
    235,961 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…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

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

#1
DAnny3211 Profile Picture

DAnny3211 253

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 137 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 90 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans