With the Spring 18 release of Business Central, Microsoft introduced APIs. There 44 APIs available out-of-the-box.
There are a lot of materials available on the web about APIs for Business Central. Some of them:
Standard APIs are perfect for Connect Apps. If you want to pull/push data to Business Central, but don’t have experience in AL or Business Central architecture – you can still build your app, using any available technology and programming language.
But what if you want to create your own APIs for Business Central?
Now, with October release, that becomes possible, and actually very easy.
With October 18 release of Business Central now it’s possible to create your custom APIs in AL.
Usually, I use https://aka.ms/bcsandbox VM to develop in AL. But at DirectionsNA Microsoft announced support of AL for MacOS!
So, next scenario I will cover from my brand new MacOS Mojave desktop!
For Windows users … continue reading =)
I will create a new AL project, and this will be a hybrid App. It will have Add-on capabilities (internal data structure and UI) and Connect capabilities (exposed API to my tables).
How to create new AL project and download symbols on MacOS
My app will show the list of aeroplanes. Very simple example. I will not show here, how to create new AL table and page – there are tons of material available on the web.
Here is the AL code
After this step, a user will have an opportunity to create/change/delete records in a new table from Business Central UI.
Ok, now we have Add-on app. What’s about Connected App part?
Assume, that you want to provide to the world opportunity to communicate with your App. In my case, I have the list of all aeroplane’s models. So, why not to share that with the world?
First, add an ID field to your table with type GUID.
Assign a value, when you insert the record
From now, any new entry in my table will have a unique ID.
Before continuing, let’s stop in a minute on API endpoints. Endpoint – is a URL, which gives you access to one or many entities inside of Business Central, from outside.
You can access data, through the endpoint, using 2 types of authentication: AAD and Direct (Web service Key).
Depending on authentication type - endpoints URI will differ. In the next examples, I'll use Direct authentication.
Business Central offers out-of-the box API endpoints to the base APIs. Like:
The list of all APIs
The list of companies
The list of items
Endpoints described above, give you access to the data inside production Business Central tenant. They are well described in official docs.
What about API endpoints for the online sandbox tenant? It’s not documented feature (yet). But it’s possible!
https://api.businesscentral.dynamics.com/v1.0/<sandbox tenant id>/sandbox/api/beta
https://api.businesscentral.dynamics.com/v1.0/<sandbox tenant id>/sandbox/api/beta/companies
https://api.businesscentral.dynamics.com/v1.0/<sandbox tenant id>/sandbox/api/beta/companies(<companyId>)/items
Cool, but all endpoints, described above, link us to the base 44 APIs, published by Microsoft.
Is it possible to create our own APIs? And if yes, how to call them both for production and sandbox tenants?
YES! It’s possible!
It’s also not documented feature (or I should improve my Google experience:).
I discovered them during DirectionsNA.
<sandbox tenant id>/sandbox/api/<ApiPublisher>
Great! So how to publish and consume them?
API pages where introduced in NAV2018. All 44 base APIs have own pages with type API.
Let’s create a new API page for aeroplane models. Use snippet for that.
page 50102 "AIR AirplaneModel Entity"
PageType = API;
Caption = 'airplaneModels';
APIPublisher = 'DmitryKatson';
APIVersion = 'beta';
APIGroup = 'airplanes';
EntityName = 'airplaneModel';
EntitySetName = 'airplaneModels';
SourceTable = "AIR Airplane Model";
DelayedInsert = true;
ODataKeyFields = Id;
Caption = 'Id';
ApplicationArea = All;
field(icaoCode; "ICAO Code")
Caption = 'icaoCode';
ApplicationArea = All;
Caption = 'description';
ApplicationArea = All;
trigger OnInsertRecord(BelowxRec: Boolean): Boolean
trigger OnModifyRecord(): Boolean
AirplaneModel: Record "AIR Airplane Model";
IF "ICAO Code" <> AirplaneModel."ICAO Code" THEN BEGIN
Remember some important rules:
You’ve created custom API. But it’s not available yet. We have one final step to be able to consumpt it.
Just publish your app, as usual.
Now let’s test our custom API, using Postman.
From that moment we have our own API on the cloud Business Central!
Let’s save custom endpoint to the variable
urlCustomApiSandbox = https://api.businesscentral.dynamics.com/v1.0/b7268087-f626-4efe-ac43-37bfaafc16d8/sandbox/api/DmitryKatson/airplanes/beta
It took me about 5 minutes to create custom API (and 4 hours to turn it into a blog:)
Do you believe in that? It’s a new future! We can’t even dream about this before, but it’s possible now!
And yes! Once again, I made it without docker, VM, developer licence or whatever. Just my MacOs and VSCode with AL.
Did you know that it’s even possible to extend base APIs! Oh yeee… That’s true! But this is a topic for another blog.
Very good article that got me started. The API has given me some headaches but overall cool feature, I wrote a little article on mibuso on how to create complex types in the API if anyone is interested. forum.mibuso.com/.../nav-2018-api-complex-types
I'm trying to extend the existing API's by using a page extension on page 5471, but the fields are not showing on the default endpoints.
Could you point me in the right directions on how to achieve this?
Great post! Can we create custom api:s on local development? That is, for a local installation of Business Central via docker