xrm-webapi-node

Create an object representing the Xrm.WebApi convention in a node.js application

Usage no npm install needed!

<script type="module">
  import xrmWebapiNode from 'https://cdn.skypack.dev/xrm-webapi-node';
</script>

README

Xrm.WebApi for Node.js

(Mostly*) unopinionated library for consuming the Microsoft Dataverse Web API using the Xrm.WebApi interface.

*: Xrm.WebApi does not provide any documented way for querying metadata definitions. For any functionality not covered by Xrm.WebApi, you can use a fetch-ish function which handles authentication and (TODO) 429 Too Many Requests errors.

Installation

npm install xrm-webapi-node

Setup

getFetch

With provided credentials

import { getFetch } from "xrm-webapi-node";

const fetch = getFetch("https://myorg.crm.dynamics.com/", {
  clientId: "12345678-9012-3456-7890-123456789012",
  clientSecret: "abcdefghijklmnopqrstuvwxyz12345678",
  authority:
    "https://login.microsoftonline.com/09876543-2109-8765-4321-098765432109",
});

// OR

const fetch = getFetch("https://myorg.crm.dynamics.com/", {
  clientId: "12345678-9012-3456-7890-123456789012",
  clientSecret: "abcdefghijklmnopqrstuvwxyz12345678",
  tenantId: "09876543-2109-8765-4321-098765432109",
});

With environment variables

This code snippet assumes the following environment variables are assigned.

import { getFetch } from "xrm-webapi-node";

const fetch = getFetch();

getApi

With fetch

import { getApi, getFetch } from "xrm-webapi-node";

const fetch = getFetch();
const api = getApi(fetch);

With provided credentials

import { getApi } from "xrm-webapi-node";

const api = getApi("https://myorg.crm.dynamics.com/", {
  clientId: "12345678-9012-3456-7890-123456789012",
  clientSecret: "abcdefghijklmnopqrstuvwxyz12345678",
  authority:
    "https://login.microsoftonline.com/09876543-2109-8765-4321-098765432109",
});

// OR

const api = getApi("https://myorg.crm.dynamics.com/", {
  clientId: "12345678-9012-3456-7890-123456789012",
  clientSecret: "abcdefghijklmnopqrstuvwxyz12345678",
  tenantId: "09876543-2109-8765-4321-098765432109",
});

With environment variables

This code snippet assumes the same environment variables used to setup fetch.

import { getApi } from "xrm-webapi-node";

const api = getApi();

Usage

Get the display name of a table

(async () => {
  const response = await fetch(
    "EntityDefinitions(LogicalName='contact')?$select=DisplayName"
  );
  const definition = await response.body();
  console.log(`Display Name: ${definition.UserLocalizedLabel.Label}`);
})();

Create a record

(async () => {
  const result = await api.createRecord("contact", {
    firstname: "Melody",
    lastname: "Universe",
  });
  console.log(`Contact ID: ${result.id}`);
})();