The Microsoft Graph JavaScript client library is a lightweight wrapper around the Microsoft Graph API that can be used server-side and in the browser.
Looking for IntelliSense on models (Users, Groups, etc.)? Check out the Microsoft Graph Types repository!
npm install @microsoft/microsoft-graph-clientInclude lib/graph-js-sdk-web.js in your page.
<script type="text/javascript" src="graph-js-sdk-web.js"></script>Register your application to use Microsoft Graph API using one of the following supported authentication portals:
- Microsoft Application Registration Portal: Register a new application that works with Microsoft Account and/or organizational accounts using the unified V2 Authentication Endpoint.
- Microsoft Azure Active Directory: Register a new application in your tenant's Active Directory to support work or school users for your tenant or multiple tenants.
The Microsoft Graph JavaScript Client Library has an adapter implementation (MSALAuthenticationProvider) for MSAL (Microsoft Authentication Library) which takes care of getting the accessToken. MSAL library does not ship with this library, user have to include it externally (For including MSAL, refer this).
Creating an instance of MSALAuthenticationProvider,
const clientID = 'your_client_id'; // Client Id of the registered application
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes
const options = { // An Optional options for initializing the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics#configuration-options
redirectUri: "Your redirect URI"
};
const authProvider = new MSALAuthenticationProvider(clientId, scopes, options);User can integrate own preferred authentication library by implementing IAuthenticationProvider interface.
An instance of the Client class handles requests to Microsoft Graph API and processing the responses. To create a new instance of this class, you need to provide an instance of IAuthenticationProvider which needs to be passed as a value for authProvider key in Options to a static initializer method Client.init.
const options = {
authProvider // An instance created from previous step
};
const client = MicrosoftGraph.Client.init(options);For more information, refer: default options, custom middleware chain
Once you have authentication setup and an instance of Client, you can begin to make calls to the service. All requests should be start with client.api(path) and end with an action.
Getting user details,
try {
let userDetails = await client.api("/me").get();
console.log(userDetails);
} catch(error) {
throw error;
}Sending an email to the recipients
// Construct email object
const mail = {
subject: "Microsoft Graph JavaScript Sample",
toRecipients: [{
emailAddress: {
address: "example@example.com"
}
}],
body: {
content: "<h1>MicrosoftGraph JavaScript Sample</h1>Check out https://github.com/microsoftgraph/msgraph-sdk-javascript",
contentType: "html"
}
};
try {
let response = await client.api("/me/sendMail").post({message: mail});
console.log(response);
} catch(error) {
throw error;
}For more information, refer: Calling Pattern, Actions, Query Params, API Methods and more.
We'd love to get your feedback about the Microsoft Graph JavaScript client library. You can send your questions and suggestions to us in the Issues section of this repository.
Please see the contributing guidelines.
- Microsoft Graph website
- Microsoft Graph TypeScript types
- Angular.js sample using the JavaScript client library
- Node.js sample using the JavaScript client library
- Office Dev Center
Copyright (c) Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
See Third Party Notices for information on the packages that are included in the package.json