All skills
github avatar

/msgraph-sdk

@8bd33a9 official
by githubgithub/awesome-copilot40k stars
5,040

Integrate Microsoft Graph SDK into any project β€” .NET, TypeScript/JavaScript, or Python. Covers auth patterns (client credentials, OBO, managed identity), SDK setup, calling Graph APIs, batching, delta queries, change notifications, throttling, and permission scopes. Use when accessing Microsoft 365 data (users, mail, calendar, Teams, files, SharePoint) from any application type.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/msgraph-sdk

This session only. Nothing lands on disk.

SKILL.md

β‰ˆ99 tokens always: the name and description. β‰ˆ2k when used: this file. β‰ˆ5.7k more on demand in 3 files.

Microsoft Graph SDK

Use this skill when integrating Microsoft Graph into an application to access Microsoft 365 data and services.

Always ground implementation in the current Microsoft Graph SDK documentation and SDK version for the target language rather than relying on memory alone.

Determine the target language first

  1. Use the .NET workflow when the project contains .cs, .csproj, or .sln files, or when the user asks for C# guidance. Follow references/dotnet.md.
  2. Use the TypeScript / JavaScript workflow when the project contains package.json, .ts, or .js files, or when the user asks for Node.js / browser guidance. Follow references/typescript.md.
  3. Use the Python workflow when the project contains .py, pyproject.toml, or requirements.txt, or when the user asks for Python guidance. Follow references/python.md.
  4. If multiple languages are present, match the language of the files being edited or ask the user.

Always consult live documentation

Authentication β€” choose the right pattern

Selecting the wrong auth flow is the most common Graph integration mistake. Apply this decision tree before writing any auth code:

Scenario Flow to use
Background service / daemon with no user Client credentials (app-only)
Agent or API acting on behalf of a signed-in user On-Behalf-Of (OBO)
App running in Azure (Function, Container App, VM) Managed Identity (preferred over secrets)
CLI tool or local dev script Device code or interactive browser
Single-page app (browser only) Authorization code + PKCE
  • Never use client credentials when a user context is required β€” Graph enforces this at the permission level (application vs. delegated).
  • Prefer DefaultAzureCredential in Azure-hosted apps; it tries managed identity first and falls back gracefully for local dev.
  • Never hardcode secrets. Use environment variables, Azure Key Vault, or the Secret Manager.

Core SDK usage patterns

Building the client

Always construct GraphServiceClient once and reuse it (it manages token caching internally).

Pass a credential from the Azure Identity library β€” never build raw HTTP clients manually.

Making calls

  • Use the fluent builder API: client.Users[userId].Messages.GetAsync(...).
  • Always await async calls.
  • Specify $select to limit returned fields β€” Graph returns large default payloads.
  • Use $filter server-side rather than filtering returned collections in memory.
  • Use $expand to fetch related resources in a single call when relationships are small.

Pagination

Graph paginates collections. Never assume all items arrive in one response:

  • Check for an @odata.nextLink on the response.
  • Use the SDK's PageIterator helper (available in all three SDKs) to walk pages automatically.
  • Set $top to control page size (max varies by resource, typically 999).

Advanced patterns

Batch requests

Combine up to 20 independent Graph calls into a single HTTP request using the $batch endpoint. Use batching when:

  • Initializing data for a dashboard or agent that needs multiple resources upfront.
  • Reducing latency in high-call-count operations.

Batch responses arrive out of order β€” match them by the id field you assigned each request.

Delta queries

Use delta queries to sync changes incrementally instead of polling full collections:

  • First call: GET /users/delta returns all items + a @odata.deltaLink.
  • Subsequent calls: use the deltaLink to receive only what changed since the last sync.
  • Supported on: users, groups, messages, calendar events, Teams channels, and more.
  • Store the deltaLink durably (database, blob) between sync runs.

Change notifications (webhooks)

Subscribe to resource changes with POST /subscriptions:

  • Graph delivers change events to your HTTPS notification URL.
  • Subscriptions expire β€” renew them before expirationDateTime (max varies by resource; typically 1–3 days for mail/calendar, up to 4230 minutes for users/groups).
  • Validate the subscription handshake: Graph sends a validationToken query parameter on creation β€” echo it back as plain text with HTTP 200.
  • Use lifecycle notifications (notificationUrl + lifecycleNotificationUrl) to handle missed events and reauthorization.
  • For high-volume scenarios prefer change notifications with resource data (requires additional encryption setup).

Throttling

Graph throttles aggressively. Always handle HTTP 429:

  • Read the Retry-After header β€” it specifies exact seconds to wait, not a fixed backoff.
  • The SDK's built-in retry middleware handles 429 automatically when configured; enable it explicitly.
  • Avoid fan-out patterns that hit Graph with hundreds of parallel requests; use batching or queuing instead.

Permissions

Get permissions right before writing auth code β€” wrong scopes result in 403 errors that are hard to debug later.

  • Application permissions run without a user (daemon / service). Require admin consent.
  • Delegated permissions run in the context of a signed-in user. Some require admin consent.
  • Request the minimum permissions needed. Graph's permission reference lists least-privilege options for every operation.
  • Use the Graph Explorer to test which permissions a call actually requires before coding.
  • In Azure app registrations: grant API permissions β†’ Microsoft Graph β†’ select type (Application or Delegated) β†’ grant admin consent where required.

Common Graph resources β€” quick reference

Goal Resource path
Get signed-in user's profile GET /me
List user's mailbox messages GET /me/messages
Send an email POST /me/sendMail
List calendar events GET /me/events
Get user's OneDrive root GET /me/drive/root/children
List Teams the user is in GET /me/joinedTeams
Post a Teams channel message POST /teams/{id}/channels/{id}/messages
List SharePoint site lists GET /sites/{siteId}/lists
Search across M365 POST /search/query
List all users in tenant (app-only) GET /users
Get group members GET /groups/{id}/members

In similar fashion, use the SDK's fluent API to navigate to these resources in code.

Workflow

  1. Determine the target language and read the matching reference file.
  2. Identify the auth scenario and choose the correct flow from the table above.
  3. Fetch current SDK docs and Graph Explorer examples before making implementation choices.
  4. Apply least-privilege permissions β€” confirm in the Graph permissions reference.
  5. Implement pagination from the start β€” don't assume single-page responses.
  6. Enable retry middleware for throttling from day one.
  7. For syncing scenarios, prefer delta queries over polling.
  8. Use the language-specific package names, auth provider setup, and code patterns from the chosen reference file.

Completion criteria

  • Auth flow matches the scenario (not defaulting to client credentials for user-context calls).
  • GraphServiceClient is constructed once and reused.
  • All collection reads handle pagination.
  • Throttling (429) is handled via retry middleware or explicit Retry-After logic.
  • Permissions are scoped to the minimum required.
  • No secrets or credentials are hardcoded.
  • Code matches current SDK version patterns for the selected language.

Source: SKILL.md on GitHub

No alerts3mo3 checks Β· Risk SAFE
  • Gen Agent Trust Hub3mo

    This skill provides safe and authoritative guidance for integrating the Microsoft Graph SDK into .NET, Python, and TypeScript projects. It strictly follows security best practices, including the use of Managed Identities, environment variables for secret management, and enforcing the principle of least privilege for API permissions. All external resources and packages originate from official Microsoft repositories and well-known registries.

  • Socket3mo

    No alerts

  • Snyk3mo

    Risk: LOW Β· No issues

Signed by skilld at 8bd33a9. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 5 months ago
  • TypeScript
  • Python
  • microsoft-graph
  • msgraph-sdk
  • microsoft-365
  • authentication
  • dotnet
  • api-integration
  • pagination
  • throttling

README badge

README badge for github/awesome-copilot/msgraph-sdk

Integrates Microsoft Graph SDK for .NET, TypeScript/JavaScript, or Python to access Microsoft 365 data (users, mail, calendar, Teams, files, SharePoint). Covers authentication patterns (client credentials, on-behalf-of, managed identity), pagination, batching, delta queries, change notifications, throttling, and permission scopes.

Generated from the current SKILL.md.

Does this skill support all three languages β€” .NET, TypeScript, and Python?
Yes. The skill includes language-specific reference workflows for .NET, TypeScript/JavaScript, and Python. Follow the matching reference file based on your project's file types.
Which authentication flow should I use for a background service?
Use client credentials (app-only) for background services or daemons with no user context. The skill includes a decision tree to match your scenario to the correct auth pattern.
How do I handle pagination when fetching large collections from Graph?
Always check for `@odata.nextLink` in responses and use the SDK's `PageIterator` helper to walk pages automatically. Never assume all items arrive in one request.
What should I do if I receive HTTP 429 (throttling) responses?
Read the `Retry-After` header for the exact wait time, and enable the SDK's built-in retry middleware to handle 429s automatically. Avoid parallel fan-out patterns; use batching or queuing instead.
Does this skill cover change notifications and webhooks?
Yes. The skill covers subscription creation, validation handshakes, renewal before expiration, and lifecycle notifications for handling missed events.

Generated from the current SKILL.md. These answers refresh after source changes.