Microsoft Graph Webhooks Sample for Node.js
This Node.js sample shows how to start getting notifications from Microsoft Graph. The following are common tasks that a web application performs with Microsoft Graph webhooks.
- Sign-in your users with their work or school account to get an access token.
- Use the access token to create a webhook subscription.
- Send back a validation token to confirm the notification URL.
- Listen for notifications from Microsoft Graph.
- Request for more information in Microsoft Office 365 using data in the notification.
First you need sign in.
Once signing in, the app will listen to the incoming emails.
After sending the email to the address, you will see the email in the app.
To use the Webhook sample, you need the following:
Register the app
This app uses the Azure AD endpoint, so you'll register it in the Azure Portal.
Sign in to the portal using your work or school account.
Choose New application registration.
- Enter a friendly name for the application.
- Choose 'Web app/API' as the Application Type.
http://localhost:3000/callbackfor the Sign-on URL.
- Click Create.
Choose your new application from the list of registered applications.
Copy and store the Application ID. This value is shown in the Essentials pane or in Settings > Properties.
To enable multi-tenanted support for the app, choose Settings > Properties and set Multi-tenanted to Yes.
Configure permissions for your application:
- Choose Settings > Required permissions > Add.
- Choose Select an API > Microsoft Graph, and then click Select.
- Choose Select permissions, scroll down to Delegated Permissions, choose Read user mail, and then click Select.
- Click Done.
Choose Settings > Keys. Enter a description, choose a duration for the key, and then click Save.
Important: Copy the key value--this is your app's secret. You won't be able to access this value again after you leave this blade.
You'll use the application ID and secret to configure the app.
Configure a tunnel for your localhost
The sample uses localhost as the development server. For this reason, we need a tunnel that can forward requests from a URL on the Internet to your localhost. If for any reason, you don't want to use a tunnel, see Hosting without a tunnel. If you want a detailed explanation about why to use a tunnel, see Why do I have to use a tunnel?
For this sample, we use ngrok to create the tunnel. To configure ngrok:
Download and unzip the ngrok binaries for your platform.
Type the following command:
ngrok http 3000
Take note of the https public URL that ngrok provides for you. This is an example:
You'll need the
NGROK_ID value in the next section.
Configure and run the web app
Use a text editor to open
ENTER_YOUR_CLIENT_IDwith the client ID of your registered Azure application.
ENTER_YOUR_SECRETwith the client secret of your registered Azure application.
NGROK_IDwith the value in https public URL from the previous section.
Install the dependencies running the following command:
Start the application with the following command:
Note: You can also make the application wait for a debugger. To wait for a debugger, use the following command instead:
npm run debug
You can also attach the debugger included in Microsoft Visual Studio Code. For more information, see Debugging in Visual Studio Code.
Open a browser and go to http://localhost:3000.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
Questions and comments
We'd love to get your feedback about the Microsoft Graph Webhook sample. You can send your questions and suggestions to us in the Issues section of this repository.
Office 365 development questions? Post them to Stack Overflow. Make sure to tag your questions or comments with [Office365] and [API].
Copyright (c) 2016 Microsoft. All rights reserved.