In this always connected world there is constant requirement for various applications to communicate with each other. One of the communication mediums in which programs talk to each other is REST (Representational State Transfer). REST represents set of rules that enables different programs to communicate with each other. In this 2-part blog series, we will see how D365 Finance and Supply Chain Management integrates with third party software. The same equally applies to D365 Finance and Operations integration.
- Consume external REST api in D365 Finance and Supply Chain, and
- How one can consume D365 Finance and Supply Chain REST api in the third-party software
Before we jump upon part 1, lets understand when to use REST based integration. Though its not a thumb rule and developer can use any technology for integration. But below points will help in making decision faster to use REST based integration.
- If there is need for real-time (synchronized) data availability between the 2 systems
- If one needs to quickly develop the integration without any complex message body
- If the data which is being sent over REST is not heavy
- Wide range of support – since REST works over HTTP/HTTPS, any browser or system which supports HTTP/HTTPS can work with REST
- Minimal learning curve for developers
Part I – Consuming external REST api in D365 Finance and Supply Chain
Calling external REST api is very well supported in D365 Finance and Supply Chain. One can use existing .NET classes in x++ to call the REST api. To demonstrate and explain how to consume the REST api in D365 Finance and Supply Chain we will use Zomato’s api for getting City Id.
Let’s have a look at below sample code which consumes Zomato’s api. In this example we are consuming the api to get the additional information related to a city for e.g. Mumbai. Brief explanation for each line of code is mentioned below:
Line#9 to Line#15:
This section contains all the required declarations of variables which will be used throughout the code
System.Net.WebHeaderCollection – Via this object we will mention the key which is provided to us by Zomato. This will serve as a kind of validation. This is must without which we will not get any response.
System.Net.WebRequest – request object in which we will pass the api url, set the format of the payload i.e. application/json and set the which method we will be calling i.e. “GET”
System.Net.HttpWebResponse – to hold response received from api
System.IO.Stream – to get the response stream
System.IO.StreamReader – to read the stream
System.String – to hold the output of the stream in string format
Newtonsoft.Json.Linq.JObject – to serialize the string in json format
Line#19 to Line#32:
This section contains the main logic in which we call the api and get the results.
Line#19 – sets the language in which we are going to send or receive the data
Line#20 – REST method. ‘GET’ in our case
Line#22-Line#23 – getting the header object and setting the “user-key” value which is api key in our case. Via this Zomato will validate the call. Without this we will not get correct response.
Line#25 – this is main line which makes the request i.e. calls the api with given parameters.
Line#27-Line#28 – gets the response stream and assign it to stream reader
Line#30 – read the stream till end of it and assign the value to variable as string
Line#31 – parse the saved response string to JSON
Line#32 – show the final response as json string via Info class. The final json response looks like
Once we execute the logic, we will receive the response. After parsing the response correctly, we are presented with below json string. As we can see in response to our query for city Mumbai, we have response i.e. id of the city, name of country in which city resides, etc.
With this demonstration we were able to consume the REST service in D365 Finance and Supply Chain. This scenario can also be known as “Pull method” i.e. D365 Finance and Supply Chain pulling the records via REST from 3rd party. This scenario can be helpful in cases where 3rd party has already hosted the services and once can pull the data for further use. Now, in our 2nd part of the blog we will try to consume the D365 Finance and Supply Chain’s exposed REST api to read data from D365.