🧩 m365: Microsoft Graph Integration Toolkit
Build Python integrations for Microsoft 365 without coupling your business logic to tenant-specific authentication, URLs, or document-library layouts.
m365 provides reusable clients for SharePoint and OneDrive document management and Outlook mailbox workflows through Microsoft Graph. It is developed by DOYDL Technologies for applications, internal tools, and data pipelines that operate on explicitly authorized Microsoft 365 resources.
m365 requires Python 3.10 or later.
Contents
- 🔍 What m365 does
- 🏗️ Architecture and package layout
- 📦 Requirements and installation
- 🔐 Authentication and authorization
- 🚀 Quick start
- 📁 SharePoint client reference
- 📬 Outlook mailbox reference
- ☁️ OneDrive integration
- Shared storage operations
- 🌐 Shared Graph transport
- 🛡️ Reliability, security, and operational limits
- 🩺 Errors and troubleshooting
- 💼 Business integration scenarios
- 🧪 Development and validation
- 📋 Release and packaging notes
- 📚 Project links and licensing
What m365 does
The toolkit organizes ordinary Microsoft Graph operations into resource-bound clients, so an application can concentrate on the workflow rather than repeatedly assembling request paths, parsing metadata, or coordinating file transfers.
| Capability | Entry point | Purpose |
|---|---|---|
| 📁 SharePoint | m365.graph.Site |
Select a site and document library |
| 🔎 Document discovery | site.search_by |
Resolve file and folder metadata |
| 📤 Document uploads | site.operations.Uploader(...) |
Transfer a file or byte stream |
| 📥 Document downloads | site.operations.Downloader(...) |
Download selected items and folder trees |
| 🔄 Document moves | site.operations.Mover(...) |
Move a selected document |
| 📬 Outlook | m365.graph.Mail |
Work with an authorized mailbox |
| 📨 Mail search | mailbox.search_by |
Read messages, folders, and attachments |
| 📝 Composition | mailbox.operations.Drafter() |
Prepare and update drafts |
| 📎 Attachments | mailbox.operations.AttachmentDownloader() |
Save selected attachments |
| ☁️ OneDrive | m365.graph.Drive |
List, search, upload and download within an explicitly selected drive/folder |
The toolkit does not provision tenants, create permission grants, provide a job scheduler, or replace Microsoft Graph's own API contracts. OneDrive access requires existing resource permissions; creating a client never grants them.
Architecture and package layout
The m365 namespace loads graph lazily. The three resource clients share transport concepts but expose domain-specific operations.
| Layer | Module | Responsibility |
|---|---|---|
| Application | Your Python project | Business rules, schedules, approvals, persistence |
| Identity | azauthlib or injected provider |
Authentication and token renewal |
| Resource clients | graph.sharepoint, graph.onedrive, graph.mailbox |
Bind a provider to an explicit resource |
| Operations | graph.operations |
Document and mailbox operations |
The supported application entry points are Site, Drive, Mail, and GraphError, imported from m365.graph, together with their documented client operations. _dateplumb, _preprocessing, and _syslib are internal implementation helpers, not supported application APIs. Any module, function, method, or attribute whose name starts with _ is internal and must not be used by applications. Internal helpers may change without public API compatibility guarantees.
The former unprefixed helper import paths have been removed without compatibility
aliases. Private helpers remain packaged for client functionality; they are not
application APIs. Python privacy is a convention, not access control.
from m365.graph import * exports only Site, Drive, Mail, and GraphError.
If you used the former client names, update OneDrive to Drive and Mailbox
to Mail in imports and constructors. There are no old-name aliases.
Requirements and installation
📋 Dependencies
| Dependency | Requirement | Role |
|---|---|---|
| Python | >=3.10 |
Runtime |
azauthlib |
>=6.0.6,<7 |
Authentication factories |
requests |
Declared dependency | HTTP transport |
numpy |
Declared dependency | Supporting utilities |
tqdm |
Declared dependency | Progress reporting |
dately |
>=4.7.8,<4.8 (optional) |
Internal advanced-date backend; not a public API |
1️⃣ Create an isolated environment
python -m venv .venv
| Environment | Activate |
|---|---|
| Windows PowerShell | .\.venv\Scripts\Activate.ps1 |
| Windows MSYS2 with standard Windows Python | source .venv/Scripts/activate |
| Linux/macOS | source .venv/bin/activate |
On Windows, if pip reports a path-length error while installing PySide6
(a dependency of azauthlib), create the environment at a shorter path and
install using that environment's Python. For example, in PowerShell:
$shortEnv = Join-Path $env:LOCALAPPDATA 'm365-venv'
python -m venv $shortEnv
& (Join-Path $shortEnv 'Scripts\python.exe') -m pip install m365
2️⃣ Install
This guide documents m365 2.9.6. To install the latest version published on
PyPI:
python -m pip install m365
To install from a source checkout, run from the repository root:
python -m pip install .
Or install the wheel for this version after building it:
python -m pip install ./dist/m365-2.9.6-py3-none-any.whl
3️⃣ Verify the installed versions
import m365
from m365 import graph
print(m365.__version__) # 2.9.6
print(graph.__version__) # 2.9.6
The distribution and Graph module report the same version.
azauthlib is installed by default; the legacy auth extra adds no dependencies.
Authentication and authorization
Clients accept a caller-supplied authentication provider. The structural contract in m365.graph.auth_types.AuthProvider defines an access_token property and Silent(scopes=None) method. Token renewal and credential handling belong to the provider, not the resource operation.
| Mode | Appropriate use |
|---|---|
| Delegated | An approved signed-in user's operations, constrained by consent and resource permissions |
| Application | An approved service identity, constrained by administrator-granted resource access |
Built-in factories on Site, Drive, and Mail include from_default_env, from_env_file, from_os_env, and from_entry. They delegate provider construction to azauthlib. Silent delegated authentication is the default; a missing usable sign-in raises azauthlib.InteractionRequiredError. Use an explicitly approved interactive flow when necessary. For unattended workloads, use a separately configured noninteractive provider.
For provider-specific configuration consult azauthlib and your organization's approved identity setup. See the Windows certificate example for an unattended storage job. A valid token alone does not establish permission to a site, drive, mailbox, or individual item.
Quick start
Start with read-only metadata and use a resource you are authorized to access.
For example, if you saved a profile with azauthlib, use its profile name and
your actual SharePoint site and library:
from m365.graph import Site
with Site.from_profile(
"My approved Microsoft Graph profile",
site_name="https://example.sharepoint.com/sites/Finance",
drive_name="Documents",
) as site:
print(site.get_item()["name"])
This does not create a profile, grant permission, or write a file. A delegated
profile needs an existing sign-in unless you explicitly opt into interactive
authentication. See Storage API for profile and Drive examples.
The following examples take an already configured provider from your application. The resource URLs and names are illustrative.
from m365.graph import Site
def find_report(provider):
with Site(
token_engine=provider,
site_name="https://example.sharepoint.com/sites/Finance",
drive_name="Documents",
) as site:
return site.search_by.filename("Reports/Forecast.xlsx")
The context manager closes the SharePoint HTTP transport after use. Search returns metadata rather than downloading the workbook automatically.
from m365.graph import Mail
def recent_inbox_subjects(provider):
mailbox = Mail(token_engine=provider, mailbox="me")
try:
messages = mailbox.search_by.messages(
folder="inbox", top=5, select=["id", "subject"]
)
return [message.get("subject") for message in messages]
finally:
mailbox.api.close()
The second example assumes approved delegated access. me is a signed-in user resource, not an application identity.
SharePoint client reference
Site and library selection
Site resolves the target site and selected document library during construction. Use explicit resource IDs when your deployment already stores them.
| Parameter | Default | Meaning |
|---|---|---|
token_engine |
None |
Injected provider, or factory-built provider |
site_name |
None |
Site URL, hostname/path, or short site name |
site_id |
None |
Complete opaque Graph site identifier |
hostname |
None |
Optional host when identifying a site |
drive_name |
None |
Name of document library |
drive_id |
None |
Opaque ID of a drive in the selected site |
graph_url |
https://graph.microsoft.com/v1.0 |
Configured HTTPS Graph endpoint |
request_timeout |
(10, 120) |
Connect/read timeout, seconds |
max_retries |
3 |
Bounded retry configuration |
search_mode |
enumerate |
Name lookup strategy |
search_root |
None |
Optional folder item ID for name traversal |
search_max_items |
5000 |
Traversal item budget |
search_max_requests |
200 |
Logical page budget |
search_max_depth |
30 |
Traversal depth budget |
When no drive is specified, the site's default drive is selected. A named drive is resolved across its available pages. site.site_id, site.drive_id, site.web_url, and site.drive_web_url describe the resolved resource. update_site_name and update_drive_name validate rebinding; unsuccessful changes roll back. Do not retarget an active transfer client.
Paths and searching
Use unencoded, library-relative paths with forward slashes, for example Reports/2026/Forecast.xlsx. A component named Documents is treated as a real subfolder, not automatically removed. Absolute document URLs are normalized only within the selected library. Parent folders must exist for uploads.
| Method | Purpose |
|---|---|
site.search_by.filename(term, exact_match=True, sort_order=None, num_files=None) |
File search |
site.search_by.foldername(term, include_children=False, exact_match=True) |
Folder search |
site.search_by.filefoldername(file_search, folder_search) |
Resolve one file and destination folder |
site.url.normalize_sharepoint_path(url) |
Convert a selected-library URL into a relative path |
Default name lookup uses bounded enumeration, compares names case-insensitively, and does not search file contents. exact_match=False permits substring matching. Duplicate names remain ambiguous; explicit relative paths are preferred. Remote shortcuts are not followed. Indexed lookup is an explicit optional mode requiring compatible authorization.
search_root limits legacy name traversal, not explicit paths, which remain
library-relative. For the modern anchor-relative methods and search error codes,
see the storage reference.
matches = site.search_by.filename("Reports/Forecast.xlsx")
folders = site.search_by.foldername("Reports", include_children=False)
Selected-site Doc.aspx?sourcedoc=<GUID> browser links can also be resolved when their GUID uniquely matches the selected library's item metadata. A display filename is not proof of identity. Prefer known paths or IDs.
Uploads
Uploader accepts a nonempty local file or io.BytesIO. Uploads use sequential sessions and validate returned completion metadata. Uploading a directory or an empty file is not supported.
result = site.operations.Uploader(
"./exports/forecast.xlsx",
sharepoint_folder_path="Reports",
conflict_behavior="fail",
)
print(result["id"])
from io import BytesIO
report = BytesIO(b"period,revenue\n2026-01,1000\n")
result = site.operations.Uploader(
report,
sharepoint_folder_path="Reports",
new_file_name="sample.csv",
conflict_behavior="fail",
)
| Option | Default | Behavior |
|---|---|---|
new_file_name |
None |
Optional destination name; required for BytesIO |
chunk_verbose |
True |
Accepted for compatibility; does not control progress output |
max_file_size |
"4MB" |
Accepted for compatibility; does not control chunk size |
conflict_behavior |
fail |
Fail when remote filename conflicts |
| Other conflict values | replace, rename |
Explicitly permit remote replacement or renaming |
To set the upload chunk size, use the shared site.upload(source, folder_path, chunk_size=...) method. The size is in bytes and must be a positive multiple of 320 KiB below 60 MiB.
The caller's BytesIO position is restored. Completion checks validate returned identity, filename, size and file metadata; they do not establish an end-to-end cryptographic content hash. When the final outcome is unknown, inspect the selected destination before deciding whether to retry.
Downloads
Pass file or folder metadata from a successful search to a download helper. Local destinations are never overwritten.
files = site.search_by.filename("Reports/Forecast.xlsx")
downloader = site.operations.Downloader(
max_workers=2,
compress=False,
)
# Use a private, approved destination directory.
paths = downloader.file("./downloads", files)
For an explicitly selected folder, downloader.folder(save_dir, folder_metadata) always preserves the folder tree on disk. compress=True also creates a ZIP without deleting the downloaded files. With preserve_dir_structure=True, the ZIP keeps relative paths; with False (the default for Downloader), entries are flattened. Empty folders are included, and duplicate ZIP entry names are rejected rather than overwritten.
Downloads stage data before publication, check the supplied size, and clean incomplete temporary files. A filesystem supporting hard links is required for atomic no-overwrite publication. Successful earlier files may remain if a later transfer fails; a batch is not a transaction.
Moving documents
site.operations.Mover(sharepoint_filename, sharepoint_foldername, new_sharepoint_filename=None) resolves a unique source and destination before moving a document. The result is validated against the expected item and parent metadata. This changes the remote document location; review the source and destination first. A response validation failure after the move does not imply rollback.
Outlook mailbox reference
Mail(token_engine, mailbox="me", use_immutable_ids=True) binds a provider to a mailbox. The mailbox value can be me for delegated access or an authorized user identifier. Immutable IDs are requested by default where applicable. The client exposes mailbox_profile, search_by, operations, tools, and api.
Reading messages and folders
| Entry point | Purpose |
|---|---|
search_by.folders(...) |
Resolve mailbox folders |
search_by.messages(...) |
Read selected messages |
search_by.message(message_id, ...) |
Read one message by opaque ID |
search_by.attachments(message_id, ...) |
List attachments |
search_by.message_delta(...) |
Read a message delta page |
search_by.folder_delta(...) |
Read a folder delta page |
messages = mailbox.search_by.messages(
folder="inbox",
top=10,
order_by="receivedDateTime desc",
select=["id", "subject", "receivedDateTime", "isRead"],
unread=True,
)
| Message option | Default | Interpretation |
|---|---|---|
query |
None |
Optional client-side text query |
folder |
None |
Folder reference |
top |
25 |
Desired result limit, not an unlimited scan |
order_by |
receivedDateTime desc |
Requested ordering |
select |
None |
Fields to retrieve; a string denotes one field |
unread |
None |
Optional read-state filter |
body_type |
html |
Requested body format |
auto_paginate |
True |
Follow continuation pages as necessary |
max_items |
None |
Explicit maximum returned records |
page_limit |
None |
Optional intentional page subset |
Use explicit message and folder IDs when names collide. Folder discovery supports include_hidden, include_children, exact_match, recursive, and top options. High-level reads raise GraphError on malformed or failed pages rather than disguising an incomplete read as success.
Drafts and message operations
The composition API supports preparing a draft for human review separately from sending it. For example, prepare the body in your application and pass the approved recipient, subject and body to mailbox.operations.Drafter().create(...). Text and HTML are supported; recipient arguments accept a single address or a sequence. Attachment, category, reply-to, importance and custom internet-header options are available subject to Graph's own restrictions.
| Operation factory | Methods | Effect |
|---|---|---|
Drafter() |
create, update, create_reply, create_reply_all, create_forward, send |
Draft creation, revision and dispatch |
Sender() |
send |
Send a composed message |
MessageUpdater() |
update, mark_read, mark_unread |
Modify a message's properties |
Mover() |
move |
Move a selected message to a folder |
Deleter() |
delete |
Delete a selected message |
Attachments
Attachments have their own IDs, distinct from message IDs. The download helper accepts an exact attachment_id or a uniquely resolvable attachment_name. The upload helper accepts a selected message, file source, optional name and MIME content type.
| Method | Purpose |
|---|---|
search_by.attachments(message_id, auto_paginate=True, max_items=None, page_limit=None) |
Retrieve attachment metadata |
operations.AttachmentDownloader().download(message_id, save_dir='.', attachment_id=None, attachment_name=None) |
Save a selected attachment |
operations.AttachmentUploader().upload(message_id, file_source, file_name=None, content_type='application/octet-stream') |
Attach a file |
Download filenames are validated and existing files are rejected. Large attachment upload sessions validate intermediate offsets and final completion. An empty legitimate final HTTP 201 is represented with completed=True, status_code=201, and uploaded size; no ID is invented. Resumable Outlook upload recovery is not implemented.
Incremental synchronization
The delta helpers return a single page with three keys: value, next_link, and delta_link. The application owns the synchronization checkpoint, persistence, reconciliation and resync policy. Do not rewrite opaque continuation links, drop repeated events, or treat deletion tombstones as ordinary current records. A terminal delta link should be persisted only once the applicable pages have been processed successfully.
HTTP 410 delta failures use GraphError(code="delta_resync_required"). The library does not automatically delete local records or reset application state.
OneDrive integration
Use Drive when you already know the authorized drive ID and, for folder-scoped
access, the folder's item ID. It does not discover an account or call /me/drive
for you. Personal OneDrive uses delegated user authentication; an app-only
certificate is not a substitute. See Drive binding.
Shared storage API
Site and Drive share file/folder search, upload, download, folder creation,
move, and rename methods. See Storage API for signatures, examples,
scope boundaries, and transfer safety. The SharePoint-specific interfaces above
remain available.
Shared Graph transport
site.api.graph_call(...) is the lower-level request entry point for operations not represented by a convenience helper. site.api.iter_collection(resource_url) yields individual collection items across continuation pages. Prefer documented Microsoft Graph resource-relative paths rather than constructing arbitrary external URLs. The transport rejects unsupported off-endpoint requests and uses separate sessions for service-issued transfer capability URLs, without Graph bearer headers.
Important response contracts:
- SharePoint transport failures raise
GraphError. - Raw ordinary
Mail.api.graph_callfailures retain a redacted error dictionary for compatibility; high-level mailbox reads, streaming, capability operations and response-metadata calls raise errors. - For a single non-streaming request,
response_metadata=Truereturnsstatus_codeandbody, not raw headers. It is not supported for batches. - Batch subrequest failures are surfaced; successful HTTP envelope status alone does not prove all operations succeeded.
- Pagination preserves opaque continuation parameters. Cycles, malformed pages and inconsistent selected-drive metadata raise errors.
- GET-cache arguments are retained for compatibility but HTTP response caching is disabled.
Reliability, security, and operational limits
| Concern | Behavior | Application responsibility |
|---|---|---|
| Throttling | Bounded retry behavior | Schedule appropriately; respect service limits |
| Partial pagination | Fail visibly on invalid pages and cycles | Commit completeness-dependent results only after successful consumption |
| Search budgets | Limits on items, pages and depth | Narrow searches to known approved locations |
| File integrity | Metadata and size validation | Add independent verification if required |
| Local overwrite | No-clobber publication | Choose approved output paths and supported filesystem |
| Remote conflict | Explicit upload policy | Review before replacing existing content |
| Ambiguous mutation | No blind replay | Inspect current remote state |
| Credential handling | Provider-managed tokens | Use approved identity configuration and private storage |
| Concurrent access | Locks around token reads and metadata rebinding | Avoid retargeting during work; do not assume full thread/process safety |
| Delta state | Invalid checkpoints surfaced | Own checkpoint persistence and resync decisions |
Outlook's default pagination safety budget is 200 logical pages; folder traversal additionally limits depth to 30 and folder count to 5,000. SharePoint's enumeration budgets are configurable per client. A work item that exceeds a budget is not necessarily missing. Pagination does not provide snapshot isolation during concurrent remote changes.
Errors and troubleshooting
from m365.graph import GraphError
try:
report_metadata = site.search_by.filename("Reports/Forecast.xlsx")
except GraphError as exc:
# Avoid logging sensitive URLs, payloads or provider credentials.
print("Graph operation failed:", exc.status_code, exc.code)
raise
| Symptom | Interpretation / action |
|---|---|
InteractionRequiredError |
Approved delegated authentication is needed; confirm your provider setup |
| HTTP 401 | Check token audience and provider refresh |
| HTTP 403 | Verify the existing resource-specific authorization with your administrator |
| HTTP 404 | Verify the exact site/library/folder/item resource and its visibility |
| HTTP 429 | Service throttling; respect backoff and review workload frequency |
pagination_cycle |
Invalid repetition in continuation state; do not accept partial results |
mailbox_page_limit |
Mail traversal safety budget reached |
delta_resync_required |
Checkpoint invalidated; follow an explicit resync policy |
upload_completion_unknown |
Upload outcome unresolved; inspect destination before retry |
batch_subrequest_redirect |
Unsupported redirected batch response |
| Existing file or unsupported hard links | Use a new destination on a compatible filesystem |
For malformed data or invalid arguments, ValueError can also be raised. A failed mutation response is not a guarantee of rollback. Inspect status_code and code when available rather than relying on fragile message-text matching.
Business integration scenarios
| Workflow | Example application design |
|---|---|
| 📊 FP&A reporting | Generate a forecast locally, validate it, then publish to an approved document library with fail-on-conflict policy |
| 🧾 Financial close | Retrieve a known authorized reconciliation workbook, validate data, and record its processing status |
| 📚 Document lifecycle | Resolve an approved contract path and move it only after review |
| 📨 Operational notifications | Prepare a report notification draft for human approval |
| 🗂️ Records processing | Check metadata, normalize timestamps, and maintain a durable application-side checkpoint |
Keep business rules outside the client. For example, a finance application should own period-close approval, workbook validation, reconciliation, audit records, and retention policy; the toolkit provides the Graph integration layer.
Development and validation
From the source root, create an isolated Python environment and install in editable mode:
python -m pip install -e '.[dates]'
python -m pip install build
python -m build
The supplied offline test launcher supports graph and combined suites. Example commands, with directories selected by the developer:
python -B tools/test_suite.py graph --output ./.local/test-results/graph --dist ./dist
python -B tools/test_suite.py combined --output ./.local/test-results/combined --dist ./dist
Use a build directory containing only the wheel and source archive for the version under test. The regression suites block live network access and do not require Microsoft 365 credentials.
Release and packaging notes
This README is the project guide on GitHub and the project description on PyPI. The source distribution includes this README, the Storage API, the license, and asset attribution notices.
Project links and licensing
- 🏠 Project repository
- 🐛 Issue tracker
- 🔐 azauthlib
- 📅 dately
- 📖 Microsoft Graph documentation
- 🏢 DOYDL Technologies
The package declares the MIT license. Source headers copyright DOYDL Technologies and include MIT license terms. Third-party assets in assets/third-party/ have separate notices. Microsoft, Microsoft Graph, Microsoft 365, Outlook, SharePoint, and OneDrive are trademarks of Microsoft Corporation. Their use does not imply affiliation, sponsorship or endorsement.
DOYDL Technologies · Reusable integration tools for Python
Metadata
Release files for m365 2.9.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| m365-2.9.6.tar.gz | 477.3 kB | Without provenance | Details for m365-2.9.6.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| m365-2.9.6-py3-none-any.whl | Python 3 | none | any | Details for m365-2.9.6-py3-none-any.whl |
Total release size: 681.3 kB
Release files / m365-2.9.6.tar.gz
| Download URL | m365-2.9.6.tar.gz |
|---|---|
| Size | 477.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
65cb71a22aa79bd6dce360ab14060d3794069745e2a6c5ca2e9d2870facbf612
|
|
BLAKE2b-256 checksum How to use checksums |
96cbc4abf40aff55c05e6ada3aae5f17a33577df63aa4deab65f0f08b85c2549
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Release files / m365-2.9.6-py3-none-any.whl
| Download URL | m365-2.9.6-py3-none-any.whl |
|---|---|
| Size | 203.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f337af30590edfbb3403b8ce5eaca52a303d62430cba0a37ead37206abfc62f3
|
|
BLAKE2b-256 checksum How to use checksums |
16c486199e93484554d21be85750419eb95a9b5de944e2778fb222e8d8a5330d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Security for m365 2.9.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.