Business Central REST API via JavaScript

I’ve just had the task to connect a new customer to one of our Node.JS webservices, in this case a payment notification service. So, payment providers like Adyen, Ingenico and Crefopay are constantly pushing some kind of payload (xml, json) against our service endpoint, and the service works like a proxy. It looks up and redirects the calls to the specific customers Dynamics NAV and Dynamics 365 Business Central instances. As BC now offers the fully featured REST API, I had to look for a simple way to connect it to this Node.JS driven service.

Snippet to connect to the Business Central REST API

In this little snippet, we fetch a specific company using the lightweight http library unirest. Don’t forget that Basic authentication is deprecated in cloud instances (you should use OAuth instead).

Read data

//Business Central REST API
var unirest = require('unirest');

//User credentials and endpoints stored in database
var clientId = "your_user";
var clientSecret = "your_api_password";
var hostName = "https://bc-docker:7048/BC/api/v1.0/";
var method = 'companies(name=\'CRONUS DE\')';

//Base64 conversion for authentication header
var authorizationBasic = Buffer.from(clientId + ':' + clientSecret).toString('base64')

var req = unirest('GET', hostName + method)
    .headers({
        'Authorization': 'Basic ' + authorizationBasic
    })
    .end(function (res) {
        if (res.error) throw new Error(res.error);
        console.log(res.raw_body);
    });

Update Data

When updating data, please don’t forget to include the ETag in the If-Match headers so that the system can check the consistency of the record. To receive the current ETag, you have to fetch the record you are looking for first.

//Business Central REST API
var unirest = require('unirest');

//User credentials and endpoints stored in database
var clientId = "your_user";
var clientSecret = "your_api_password";
var hostName = "https://bc-docker:7048/BC/api/v1.0/";

//Get variables from your system first
var companyId = "your_company_id";
var itemToUpdate = "12345678"; //The item to update
var ETag = "12345678_xyz"; //Get current ETag first and use it to update

//Item endpoint 
var method = 'companies(companyId)/items(itemToUpdate)';

//Base64 conversion for authentication header
var authorizationBasic = Buffer.from(clientId + ':' + clientSecret).toString('base64')

var req = unirest('PUT', hostName + method)
  .headers({
    'Content-Type': 'application/json',
    'If-Match': ETag,
    'Authorization': 'Basic ' + authorizationBasic
  })
  .send(JSON.stringify({
    "displayName": "ATHENS Schubladenelement 3"
  }))
  .end(function (res) { 
    if (res.error) throw new Error(res.error); 
    console.log(res.raw_body);
  });

Insert data

//Business Central REST API
var unirest = require('unirest');

//User credentials and endpoints stored in database
var clientId = "your_user";
var clientSecret = "your_api_password";
var hostName = "https://bc-docker:7048/BC/api/v1.0/";

//Get variables from your system first
var companyId = "your_company_id";

//Customer endpoint
var method = 'companies(companyId)/customers';

//Base64 conversion for authentication header
var authorizationBasic = Buffer.from(clientId + ':' + clientSecret).toString('base64')

var req = unirest('POST', hostName + method)
  .headers({
    'Authorization': 'Basic ' + authorizationBasic,
    'Content-Type': 'application/json'
  })
  .send(JSON.stringify({
    "displayName": "Coho Winery 1",
    "type": "Company",
    "address": {
      "street": "192 Market Square",
      "city": "Atlanta",
      "state": "GA",
      "countryLetterCode": "US",
      "postalCode": "31772"
    },
    "email": "[email protected]"
  }))
  .end(function (res) { 
    if (res.error) throw new Error(res.error); 
    console.log(res.raw_body);
  });

After setting up your API user you could start calling the Business Central REST API.

Using Postman to create snippets

Please have a look into Postman if you are looking for further snippets. Here you can model your requests in a very convenient and graphical way. The tool then spits out snippets for the respective query as needed.

Generating client code | Postman Learning Center

... is a technical consultant and developer at Comsol Unternehmenslösungen AG in Kronberg/Taunus. Major tasks are the architecture and implementation of complex, usually cross-system applications in and around Microsoft Dynamics 365 Business Central.

2 comments

  1. Thank you!! After hours of searching this was the first example I could get to work!! Any chance you could also show how to do a POST request to insert data into my table from Node?

    Reply

    1. Hi Mike, i’ve updated the example with an PUT and POST for updating and inserting. I had no chance to test it but it might lead you in the right direction.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *