The Microsoft Graph API is a powerful tool for integrating Microsoft cloud services into your applications. One of its most useful features is the ability to manage and interact with calendars. Whether you're building a custom calendar app or looking to automate scheduling tasks, understanding how to use the Microsoft Graph API for calendars is essential. Let's dive into an example that demonstrates how to create, update, and view events using the Microsoft Graph API.

Before we begin, ensure you have registered an application in Azure Active Directory and obtained the necessary permissions and access tokens. You'll also need to install the Microsoft Graph SDK for your preferred programming language. In this example, we'll use JavaScript with the @microsoft/microsoft-graph-client library.

Setting Up the Microsoft Graph Client
The first step is to initialize the Microsoft Graph client with your access token. You can obtain an access token by following the OAuth 2.0 authorization code flow with PKCE. Once you have the token, you can initialize the client like this:

const client = new Client({authProvider: async (scopes) => {return accessToken;}});
Creating a New Event

To create a new event, you'll need to make a POST request to the /me/events endpoint. Here's an example of creating an event with a subject, start and end times, and a location:
await client.api('/me/events').post({subject: 'Meeting with Team',
startDateTime: '2022-03-15T09:30:34',
endDateTime: '2022-03-15T10:00:34',
location: {displayName: 'Office',
address: {streetAddress: '123 Main St',
city: 'Seattle',
state: 'WA',
country: 'USA'}}}});
Updating an Existing Event

To update an event, you'll need to make a PATCH request to the event's unique identifier. Let's say you want to change the location of the previously created event:
await client.api('/me/events/eventId').patch({location: {displayName: 'Home Office',
address: {streetAddress: '456 Oak St',
city: 'Seattle',
state: 'WA',
country: 'USA'}}}}});
Viewing and Managing Events

To retrieve a list of your events, make a GET request to the /me/events endpoint. You can also filter events by specifying query parameters:
const events = await client.api('/me/events').get({queryParameters: {startDateTime: '2022-03-01',
endDateTime: '2022-03-31'}});




















Deleting an Event
To delete an event, make a DELETE request to the event's unique identifier:
await client.api('/me/events/eventId').delete();
With these examples, you now have a solid foundation for using the Microsoft Graph API to manage calendars. Whether you're creating, updating, or deleting events, the Microsoft Graph API provides a powerful and flexible way to integrate calendar functionality into your applications. Happy coding!