Introducing the Microsoft Graph Python SDK

Isaac Vargas Chacon

We are happy to announce Microsoft Graph Python SDK in GA. Previously, this SDK was released in public preview in November 2022.

You can now access the beta and v1 endpoints of Microsoft Graph, with a fluent experience, designed to facilitate discoverability with the best features of the Python language.

In this blog, we will cover the key features of the Microsoft Graph Python SDK.

Simplified initialization and authentication

With simplified authentication, you can start making requests to Microsoft Graph in just 5 lines of code. Just add your credentials to the library of your choice, initialize the client and your app will be ready to make requests.

The authentication provider handles the fetching, caching, and refreshing of tokens automatically, ensuring your requests are always authenticated. The auth provider verifies that the token is always valid by tracking the expiration and automatically refreshing the token in the background before it expires.

Fluent experience to build requests

Contrary to other fluent experiences in Python, we don’t map every endpoint and method to a unique function. Instead, we present a framework to construct any request using method chaining, providing a clean flow that works seamlessly in Python.

A screenshot of a computer Description automatically generated

The fluent pattern contributes to reducing errors, by displaying only the methods corresponding to operations on the specified resource. Aided by the IDE’s autocomplete, the SDK allows the developer to confidently type code that runs.

Built-in retry-handler

The SDK offers a configurable built-in Retry-Handler that understands 429 (Too Many Requests), 503 (Service Unavailable) and 504 (Gateway Timeout) response status codes, that usually require the application to retry the request after some time. The handler reads the recommendation in the `Retry-After` header and retries after the recommended time automatically. You can also customize the handler and specify a custom value for the `RetriesTimeLimit` greater than 0, to introduce time-based evaluated request retries with the default count-based request retry.

More features

  • HTTP2 support: The Python SDK support HTTP2 out of the box by default.
  • Type annotations: The SDK allows you to specify a variable’s type to provide clarity and control during the development process.
  • Model classes: The SDK uses model classes corresponding to Microsoft Graph resources, allowing the developer to work with Python objects, automatically serializing and deserializing JSON format in requests and responses.

Kiota generated

You can also generate a Python SDK for any API you are using in your project and create a consistent SDK experience across APIs. All you need is an OpenAPI description. To learn more, visit the Kiota documentation.

Get started with the Microsoft Graph Python SDK

  • To get familiar with the Microsoft Graph SDKs, start with the documentation page.
  • The README and code examples will teach you how to use the Python SDK. You can also find the SDK to access the beta endpoint here.
  • Please don’t hesitate to open an issue for errors you encounter that we didn’t include in our guide or simply to share feedback with us.
  • Sign up for the Microsoft 365 Developer Program to get a free Microsoft 365 developer subscription and start building with Microsoft Graph.

Happy coding!

Follow us on X / @Microsoft365Dev for the latest updates.


Discussion is closed.

Feedback usabilla icon