Exchange Web Services JavaScript API

0.15.3 · active · verified Sun Apr 19

ews-javascript-api is a JavaScript/TypeScript library that provides an API for interacting with Microsoft Exchange Web Services (EWS), aiming to be a counterpart to the C# EWS Managed API. It supports Office 365 OAuth, enabling interaction with modern Exchange Online environments. The current stable version is 0.15.3, with recent releases focusing on bug fixes, security dependency updates, and improved OAuth support. While development has had periods of activity and dormancy, the project is actively maintained to address issues and enhance features like async/await integration and a modular `@ewsjs` namespace. Key differentiators include comprehensive TypeScript type definitions, support for both Node.js and browser environments (via `ews-js-api-browser`), and built-in OAuth support for Exchange Online/Office 365 through `EwsOAuthHelper`, making it suitable for modern web and server-side applications needing to access Exchange data programmatically.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the ExchangeService with Office 365 OAuth credentials using the `EwsOAuthHelper` to obtain an application-only access token, preparing it for subsequent EWS operations. It sets up environment variables for sensitive credentials and includes basic error handling, illustrating the modern authentication flow.

import { ExchangeService, OAuthCredentials, ExchangeVersion } from "ews-javascript-api";
import { EwsOAuthHelper } from "ews-javascript-api/lib/EwsOAuthHelper";

const clientId = process.env.EWS_CLIENT_ID ?? '';
const clientSecret = process.env.EWS_CLIENT_SECRET ?? '';
const tenantId = process.env.EWS_TENANT_ID ?? '';
const exchangeUrl = process.env.EWS_URL ?? 'https://outlook.office365.com/EWS/Exchange.asmx';

async function main() {
  if (!clientId || !clientSecret || !tenantId) {
    console.error('Missing EWS_CLIENT_ID, EWS_CLIENT_SECRET, or EWS_TENANT_ID environment variables.');
    return;
  }

  try {
    const oAuthHelper = new EwsOAuthHelper({ clientId, clientSecret, tenantId });
    console.log('Attempting to get application access token...');
    const token = await oAuthHelper.getAppAccessToken();
    console.log('Successfully obtained access token. Expires in:', token.expiresIn);

    const ews = new ExchangeService(ExchangeVersion.Exchange2016);
    ews.Url = exchangeUrl; // Set your EWS endpoint URL
    ews.Credentials = new OAuthCredentials(token.accessToken);

    // Example: Find the Inbox folder
    // You might need to refresh the token if it expires during long-running operations.
    // A typical pattern is to wrap EWS calls in a function that checks token validity.
    // const wellKnownFolderName = new WellKnownFolderName(WellKnownFolderName.Inbox);
    // const findFoldersResults = await ews.FindFolders(wellKnownFolderName, new FolderView(10));
    // console.log('Found Inbox folder:', findFoldersResults.Folders[0].DisplayName);

    console.log('ExchangeService initialized with OAuth credentials. Ready for EWS operations.');
    // Placeholder for actual EWS operations, e.g., finding items or sending emails.
    // await ews.FindItems(WellKnownFolderName.Inbox, new ItemView(10));
  } catch (error) {
    console.error('Error during EWS operation:', error);
    if (error instanceof Error) {
      console.error('Error message:', error.message);
    }
  }
}

main().catch(console.error);

view raw JSON →