Monday, August 3, 2020

Creating APIs in Microsoft Dynamics 365 Business Central and using Postman to send GET, POST, PATCH and DELETE requests

Hi Readers,

I hope you are doing well. Here is another interesting blog for you all!

APIs give more feasibility over Web Services and have fewer restrictions than them. The data can also be in any format, and also provides the segregation over Page Cards which we had to create for Web services. Business Central gives a lot of facilities for APIs. You can also connect to external APIs without any hassle and access JSON parameters. But that is again for another blog.

Today, we’ll see how to create APIs pages and use GET, POST, PATCH and DELETE methods for CRUD operations, through Postman.

Let’s get started!

Prerequisites:

  • Business Central Sandbox Environment
  • Username
  • Web Access key

The steps are as follows:

  •  Create an extension or add your code to your existing extension however you like it.  
  •  Create a Table as shown below:

 


Note: Every time a record is created, the ID will be assigned with a GUID.
  •  Create an API page as shown below:






Note: We have to use Page triggers since these API pages are the ones that we'll trigger or in other words send a request to, to generate a response. 
  • Add your Page to the list of APIs in API Setup.
Refer: 
  • Get the Postman ready. It should have Authorization as Basic Auth and Username and Password will be your Business Central Username and Web Access Key respectively.

The API url format: {busiesscentralurl}/v2.0/{tenantid}/Sandbox/{APIPublisher}/{APIGroup}/{APIVersion}/

Example:https://api.businesscentral.dynamics.com/v2.0/{tenantid}/Sandbox/api/Jyotsna/itemChars/v1.0/

  • Send the request through Postman:

GET (Read): 

Example:

https://api.businesscentral.dynamics.com/v1.0/{tenantId}/sandbox/api/Jyotsna/itemChars/v1.0/companies({companyId})/itemChars

POST (Insert):

Example:

https://api.businesscentral.dynamics.com/v1.0/{tenantId}/sandbox/api/Jyotsna/itemChars/v1.0/companies({companyId})/itemChars

Note: For PATCH/PUT and DELETE you need to add an additional Parameter in your Header and that is as eTag as shown below: 


Make sure that eTag should have the format: W/"___"

You'll get the etag for every record in the GET/POST request. 

PATCH (Update):

Example:

https://api.businesscentral.dynamics.com/v1.0/{tenantId}/sandbox/api/Jyotsna/itemChars/v1.0/companies({companyId})/itemChars({Guid})

DELETE (Delete):

Example:

https://api.businesscentral.dynamics.com/v1.0/{tenantId}/sandbox/api/Jyotsna/itemChars/v1.0/companies({companyId})/itemChars({Guid})

So, overall what happens is that we take an httpclient (business central) and send a httpRequest in any format (json in our case) using Headers (eTag, if required) and Basic Auth, and different methods (GET, POST, PATCH and DELETE) which will perform  an httpResponse which we tested through Postman. 


I hope you found this blog helpful and useful. See you next time. Till then, happy learning! :) 

Please suggest what else do you want me to blog about. Keep following and supporting!

You can also email me at jyotsnaoberainav@gmail.com

Also, visit my website JONAV if you need help with any NAV/BC Services!

Thank you! 

No comments:

Post a Comment