Microsoft Graph JavaScript Client Library

repository·dev·Indexed 21 days ago

https://github.com/microsoftgraph/msgraph-sdk-javascript

A lightweight wrapper around the Microsoft Graph API for Node.js (12 LTS+) and browser environments. It provides a Core SDK for authentication and serialization, and a Service Library featuring a Fluent API for type-checked requests. Supports MSAL and Azure Identity for authentication, including AuthCodeMSALBrowserAuthenticationProvider and TokenCredentialAuthenticationProvider. Includes support for large file uploads via LargeFileUploadTask and provides dedicated TypeScript definitions for v1.0 and beta models.

Tokens
38.8K
Snippets
118
Records
157
Agent score
73%

What's inside microsoftgraph-msgraph-sdk-javascript

  1. What is middleware in the Microsoft Graph Client?

    dev

    Middleware components sit in the middle of the request and response cycle. Each middleware has access to a Context object, which contains the request, response, and any middleware-specific options (middlewareOptions).

    To allow the request to continue through the pipeline, a middleware must explicitly call the execute method of the next middleware in the chain using the same Context object. The final middleware in the chain (typically an HTTP message handler) is responsible for performing the actual network request and setting the response object back into the Context.

  2. How to use ImplicitMSALAuthenticationProvider in the browser

    dev

    The library provides an ImplicitMSALAuthenticationProvider to work with the Microsoft Authentication Library (MSAL). This is specifically for frontend applications using loginPopup or acquireTokenPopup flows. You must provide an MSAL UserAgentApplication instance and an array of graphScopes.

    // Configuration for MSAL
    const msalConfig = {
    	auth: {
    		clientId: "your_client_id",
    		redirectUri: "your_redirect_uri",
    	},
    };
    const graphScopes = ["user.read", "mail.send"];
    
    // Initialize MSAL and the Graph Auth Provider
    const msalApplication = new Msal.UserAgentApplication(msalConfig);
    const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
    const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
  3. How PageIterator works for consuming paged collections

    dev

    Microsoft Graph API collections are often split into multiple pages, where each response includes a URL to the next page. PageIterator is an abstraction that simplifies consuming these collections by automatically handling the pagination logic.

    To use it, you provide:

    1. A Graph Client instance.
    2. The initial PageCollection response from an API request.
    3. A PageIteratorCallback function that is executed for every item in the collection.

    The callback function must return a boolean:

    • true: Continue iterating to the next item/page.
    • false: Stop the iteration process.

    Calling await pageIterator.iterate() will process the entire collection until no more pages are available.

    // Example of basic iteration pattern
    let response: PageCollection = await client.api("/me/messages").get();
    let callback: PageIteratorCallback = (data) => {
        console.log(data);
        return true; // Continue iteration
    };
    let pageIterator = new PageIterator(client, response, callback);
    await pageIterator.iterate();
  4. Use Authentication Provider options for MSAL and Azure Identity

    dev
    The authProviderOptions/ directory provides exported options for configuring different authentication providers within the Microsoft Graph JavaScript Client Library. This includes support for MSAL (Microsoft Authentication Library) and Azure Identity Token Credentials. These options allow you to plug in various authentication mechanisms to authorize your Graph API requests.
  5. How ImplicitMSALAuthenticationProvider works with MSAL

    dev

    The library provides an ImplicitMSALAuthenticationProvider adapter for the Microsoft Authentication Library (MSAL). This adapter handles the loginPopup and acquireTokenPopup flows.

    Important: MSAL is intended for frontend applications. For server-side applications, you should implement your own IAuthenticationProvider instead of using this implicit provider.

  6. How to authenticate using MSAL (ImplicitMSALAuthenticationProvider)

    dev

    The library provides an ImplicitMSALAuthenticationProvider adapter for the Microsoft Authentication Library (MSAL).

    Important Notes:

    • MSAL is only compatible with front-end applications. For server-side authentication, you must implement a custom IAuthenticationProvider.
    • The ImplicitMSALAuthenticationProvider implements loginPopup and acquireTokenPopup flows.
    • MSAL is not included in this library; you must install it separately.

    Browser Environment Setup

    1. Include MSAL via script tag (ensure the version matches the library's requirements).
    2. Configure Msal.UserAgentApplication with your clientId and redirectUri.
    3. Initialize MSALAuthenticationProviderOptions with your required Graph scopes.
    4. Create the ImplicitMSALAuthenticationProvider using the MSAL application instance.

    Node.js Environment Setup

    1. Install msal via npm.
    2. Import UserAgentApplication from msal.
    3. Follow the same configuration pattern as the browser environment to create the ImplicitMSALAuthenticationProvider.
    // Browser Example
    const msalConfig = {
    	auth: {
    		clientId: "your_client_id",
    		redirectUri: "your_redirect_uri",
    	},
    };
    const graphScopes = ["user.read", "mail.send"];
    
    const msalApplication = new Msal.UserAgentApplication(msalConfig);
    const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
    const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
  7. How to authenticate using ImplicitMSALAuthenticationProvider

    dev

    The library provides an ImplicitMSALAuthenticationProvider to work with the Microsoft Authentication Library (MSAL).

    Note: MSAL is only supported for frontend applications. For server-side authentication, you must implement your own IAuthenticationProvider.

    Browser Environment

    1. Include MSAL via script tag.
    2. Initialize Msal.UserAgentApplication with your configuration.
    3. Create the ImplicitMSALAuthenticationProvider using the MSAL instance and desired scopes.

    Node.js Environment

    1. Install msal via npm.
    2. Import UserAgentApplication from msal.
    3. Initialize the provider similarly to the browser environment.
    // Browser Example
    const msalConfig = {
    	auth: {
    		clientId: "your_client_id",
    		redirectUri: "your_redirect_uri",
    	},
    };
    const graphScopes = ["user.read", "mail.send"];
    
    const msalApplication = new Msal.UserAgentApplication(msalConfig);
    const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
    const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
  8. How to use ImplicitMSALAuthenticationProvider in Node.js

    dev

    For Node.js environments, you can use MSAL by installing it via npm and importing the UserAgentApplication. Note that MSAL is primarily designed for frontend use; for server-side authentication, you should implement a custom IAuthenticationProvider.

    npm install msal@<version>
    import { UserAgentApplication } from "msal";
    import { ImplicitMSALAuthenticationProvider } from "@microsoft/microsoft-graph-client/lib/src/ImplicitMSALAuthenticationProvider";
    
    const msalConfig = {
    	auth: {
    		clientId: "your_client_id",
    		redirectUri: "your_redirect_uri",
    	},
    };
    const graphScopes = ["user.read", "mail.send"];
    
    const msalApplication = new UserAgentApplication(msalConfig);
    const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
    const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, options);