Build a web application with the Microsoft Graph Toolkit

This topic describes how to get started with the Microsoft Graph Toolkit in a web application written in vanilla JavaScript. For a step-by-step tutorial, try the Get started with Microsoft Graph Toolkit module. If you would like to learn how to use the Toolkit with a web framework, see Build a web app (React) or Build a web app (Angular).

Getting started with the Microsoft Graph Toolkit involves the following steps:

  1. Add Microsoft Graph Toolkit to your project.
  2. Initialize the MSAL2 Provider.
  3. Add components.
  4. Test your application.

Add the Microsoft Graph Toolkit to your project

You can use the Microsoft Graph Toolkit in your application by referencing the loader directly (via unpkg) or by installing the npm package.

To use the Toolkit via mgt-loader, add the reference in a script to your code:

<script src=""></script>

Initialize the MSAL2 Provider

The Microsoft Graph Toolkit providers enable authentication and access to Microsoft Graph for the components. To learn more, see Using the providers. The MSAL2 Provider uses msal-browser to sign in users and acquire tokens. You can initialize this provider in your HTML or JavaScript.

Note: If you are currently using the MSAL Provider and would like to update to MSAL2 Provider, follow the steps listed here. If you would like to use your own backend authentication, use the Proxy Provider in place of the MSAL2 Provider.

You can choose to initialize the provider in either your HTML or your JavaScript code.

Add the mgt-msal2-provider component to your HTML page and set the client-id to your application client-id.

<mgt-msal2-provider client-id="<YOUR_CLIENT_ID>"></mgt-msal2-provider>

The client ID is the only property required to initialize the provider, but you can set additional options. For the full list, see MSAL2 Provider.

Creating an app/client ID

In order to get a client ID, you need to register your application in Azure AD.

Add components

After you initialize the MSAL2 provider, you can start using any of the Toolkit components.

The following is a full working example using mgt-loader, the MSAL2 Provider initialized in HTML, and the Login component:

<script src=""></script>
<mgt-msal2-provider client-id="<YOUR_CLIENT_ID>"></mgt-msal2-provider>

This is an example using the ES6 modules, the MSAL2 Provider initialized in HTML, and the Login component:

<script type="module" src="node_modules/@microsoft/mgt/dist/es6/index.js"></script>
<mgt-msal2-provider client-id="<YOUR_CLIENT_ID>"></mgt-msal2-provider>

Test your app

In order to test your app, MSAL requires the page to be hosted in a web server for the authentication redirects.

If you're just getting started and want to play around, you can use Live Server in Visual Studio Code or any similar lightweight development server. Download the extension and open your HTML file using live server.

Note: Make sure the redirect URI in your app registration is set to the localhost port your application is hosted on. Go to your app registration in the Azure portal, click Authentication under manage, and add the correct redirect URI.

Track a user's sign in state

You can detect when a user has successfully signed in and display specific components accordingly. For example, display the agenda component if the user has signed in. Otherwise, display the sign in interface.

To properly inspect the user's sign in state, add an event handler to the providerUpdated event using the Providers.onProviderUpdated function. In the handler, check the provider state stored on the Providers.globalProvider.state property.

If you're using the mgt-loader library, you can access the Provider and ProviderState from the global mgt property.

<!DOCTYPE html>
  <script src=""></script>
  <mgt-msal2-provider client-id="<YOUR_CLIENT_ID>"></mgt-msal2-provider>
  <div id="main">
    const loadAgenda = () => {
      if (mgt.Providers.globalProvider.state === mgt.ProviderState.SignedIn) {
        document.getElementById('main').innerHTML = '<mgt-agenda></mgt-agenda>';

Next Steps