Microsoft Dynamics 365 is a cloud-based business applications platform that combines components of customer relationship management ( ) and enterprise resource planning ( ), along with productivity applications and artificial tools, although currently Dynamics is more popular for its CRM capabilities. CRM ERP intelligence Sometimes as with many other enterprise grade applications, the need may arise to abstract some activities from the main application itself, for example creating a mobile app to perform operations or have a need to communicate with dynamics outside the dynamics application. In this post, I’d show you how to implement the Rest API provided by dynamics, this sounds easy enough, but the main challenge comes when trying to procure access credentials. This was a very painstaking process, resources that guides you through this sadly are not put together in one place, pieces of information from different sources (videos, articles, official docs) had to be put together as it proved difficult getting a single resource that offered an end to end guide. This post hopefully would save someone a lot of time and hair pulling. Azure Active Directory The Azure Active Directory (Azure AD) is Microsoft’s cloud-based identity and access management service, in simple terms, its like a global auth service for most of Microsofts products, customers using Microsoft 365, Office 365, Azure, or Dynamics CRM Online subscribers already have Active Directory setup for them, this tutorial up to the point of obtaining access credentials would apply to them also. Phase 1 (Setup Dynamics) The first step is to create a dynamics account if you don’t have access to one already, you can sign up for a free one month trial , note that you would need an email with a domain you can manage, this is necessary as you’d need to update the domains TXT record to manage the dynamics account with admin privileges. here After creating your dynamics account, on the dashboard click on the menu button at the top right corner, and then click the admin button You’d be asked to verify your email domain, follow the steps provided and verify your email. Phase 2 (Access Credentials) As explained earlier, Dynamics CRM’s authentication is managed under the Azure’s Active Directory, so we’d have to access the Azure portal to get the needed AAD credentials. This doesn’t mean you’d have to create an account on Azure, as explained earlier AAD is a global auth service for Microsoft products and SAAS’s, so you can sign right into the Azure portal using the same login credentials you use for your dynamics service. here Step 1 Get Client ID and Secret On the side menu, click on Azure Active Directory 2. click on then App Registrations New Registration 3. Fill in the required details, select the account access option that applies to your application needs 4. On the overview page, copy the client id and tenant id also 5. click on manifest, and change the value of “allowPublicClient” from null to true and then save 6. Next, on the menu tab click on and then add permission, select the dynamics CRM option and then check the user impression option. if you are following this tutorial for other services, you can select your service here API permissions 7. On the next page, click on the “Grant admin consent for <your domain>” button And that’s it for the phase 1, what we have done so far is to Get credentials for our apiGrant access to dynamics crm from our api Remember these credentials isn’t just limited to Dynamics access, you can grant permissions to other Microsoft services also and this credentials would work just okay with them also. Phase 2 (Using the api) The first part is done, now to implement the actual API integration in nodejs we would be working with two libraries, Adal library , for active directory authentication , An helper library for dynamics CRM for node Dynamics web api Install them like any other node module, follow the links above to learn more about these modules. I’d just put a copy of my code below, I won’t go into much details talking about the code as its very easy to follow. * adal ; * DynamicsWebApi ; DynamicsConnector { authorityUrl: ; resource: ; clientId: ; username: ; password: ; adalContext: ; apiUrl: ; dynamicsWebApi : ; ( ){ .authorityUrl = ; .resource = ; .apiUrl = resource; .clientId = clientId; .username = username; .password = password; .dynamicsWebApi = DynamicsWebApi({ webApiUrl: , onTokenRefresh: .acquireTokens }); .adalContext = adal.AuthenticationContext( .authorityUrl); } acquireTokens = { adalCallback = { (!error){ dynamicsCallback(token); } { .log( + error.stack); } } .adalContext.acquireTokenWithUsernamePassword( .resource, .username, .password, .clientId, adalCallback); } } /* * This service retrieves access token from * microsoft and then instantiates the dynamics web-api * service */ import as from 'adal-node' import as from "dynamics-web-api" export class string string string string string any string any constructor tenantId: , resource: , clientId: , username: , password: string string string string string this `https://login.microsoftonline.com/ /oauth2/token` ${tenantId} this `https:// /` ${resource} this this this this //create DynamicsWebApi object this new `https:// /api/data/v9.0/` ${this.apiUrl} this this new this public ( ) => dynamicsCallback: any const ( ) => error: , token: any any if //call DynamicsWebApi callback only when a token has been retrieved else console 'Token has not been retrieved. Error: ' this this this this this The code sample consists of two typescript files, the first dynamics.service.ts and an app.ts file, the dynamics.service.ts is a service that handles the token retrieval from Active Directory using the adal module, and also initiates the Dynamics-web-api. { DynamicsConnector } ; dynamicsTenantId = ; dynamicsHost = ; dynamicsClientId = ; dynamicsUserName = ; dynamicsPassword = ; dynamics = DynamicsConnector(dynamicsTenantId, dynamicsHost, dynamicsClientId, dynamicsUserName, dynamicsPassword).dynamicsWebApi; dynamics.retrieveMultiple( , [ , ], ).then( { .log(records) }) .catch( { }); import from './dynamics.service' //your tenant id obtained from Azure Active Directory const "****************" //CRM Organization URL const '*******0.crm8.dynamics.com' //Dynamics 365 Client Id obtained from Azure active directory const '******************' //your dynamics username const '***************' //your dynamics password const '***********' //instantiate the service const new //An Example from the dynamics web api docs "leads" "fullname" "subject" "statecode eq 0" ( ) function records: any console ( ) function error: any //catch an error In the app.ts file we implement the DynamicsService by passing the required credentials to the constructor after which you can use the instance to run queries against your dynamics service. Follow this to see the entities exposed by the dynamics api. link So that’s it for this post, I hope I have saved you a bit of time, if its been of any assistance to you, please show your appreciation by donating some claps. O dabọ ✌️