Connect to Dynamics CRM in Node.js

Ahmed Sawalhy   •   December 23, 2018

There are a couple of ways that we can authenticate with a CRM deployment: Active Directory (AD), Claims, and OAuth. Finding a single library that can easily handle all three in Node.js was a bit of a challenge. Installing multiple libraries, with different methods of usage, is too much hassle for such a straightforward purpose, in my opinion.

The research

To connect to CRM using AD, I tried httpntlm library, and it worked right away without any issues. However, i faced a lot of issues with OAuth; mainly caused by outdated guides on the Internet.

Previously, I worked mainly with Google’s APIs when it came to OAuth, so it was new territory for me working with Azure. So the first step I did was to try the well-known ADAL.js library. It worked fine in a web app with a proper front end, but in my case, I wanted to connect server-to-server. Try as I might, it never worked, as expected as nearly all guides assume there is an interface for the user.

Stumbling upon this article by Lucas: link, I went back to the basics by opting to do it by hand using basic Node built-in functions.

The Solutions

I followed his steps for the most part, but I wanted to simplify things even more by automatically acquiring the tokenendpoint. The endpoint requires the tenant ID, which can be found by accessing{}/.well-known/openid-configuration, after replacing with the domain of the users.

I will add the option to specify the tenant ID in the configuration, for when the above causes an issue I didn’t foresee.

Using the knowledge acquired above, I created a Node.js library that can do the intended job, for AD and OAuth at least.


Run the following command in your app’s CLI:

Copy to Clipboard

The following imports cover the exposed types in this module:

Copy to Clipboard


Common parameters

Copy to Clipboard

AD parameters

To authenticate with AD, in addition to the above parameters, all you need is the username, password, and domain.

Copy to Clipboard

OAuth parameters

To authenticate with OAuth, you will need a few more parameters:

  • tenant: the Azure tenant
  • appId: Application ID in Azure Active Directory
  • clientId: the Client Secret created in the application above
Copy to Clipboard


The parameters defined above are passed to the right configuration constructor first:

Copy to Clipboard

Next, construct and initialize the service:

Copy to Clipboard

Make CRM requests

The service supports the following HTTP verbs: GET, POST, PUT, PATCH, and DELETE.
To make requests to CRM:

Copy to Clipboard