Standard APIs
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.
Custom APIs
With October 18 release of Business Central now it’s possible to create your custom APIs in AL.
Step 1. Open VS Code with AL extension installed
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 =)
Step 2. Open/clone/create AL project and download symbols
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
Step 3. Create a new table, list page and publish an app
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.
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?
Step 4. Create custom API for your App
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?
Add an Id field to the table
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.
API endpoints
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.
Production Tenant
Business Central offers out-of-the box API endpoints to the base APIs. Like:
The list of all APIs |
https://api.businesscentral.dynamics.com/v1.0/<your domain>/api/beta |
The list of companies |
https://api.businesscentral.dynamics.com/v1.0/<your domain>/api/beta/companies |
The list of items |
https://api.businesscentral.dynamics.com/v1.0/<your domain>/api/beta/ companies(<companyId>)/items |
Endpoints described above, give you access to the data inside production Business Central tenant. They are well described in official docs.
Sandbox Tenant
What about API endpoints for the online sandbox tenant? It’s not documented feature (yet). But it’s possible!
The list of all APIs |
https://api.businesscentral.dynamics.com/v1.0/<sandbox tenant id>/sandbox/api/beta |
The list of companies |
https://api.businesscentral.dynamics.com/v1.0/<sandbox tenant id>/sandbox/api/beta/companies |
The list of items |
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!
Custom APIs endpoints
It’s also not documented feature (or I should improve my Google experience:).
I discovered them during DirectionsNA.
Production | Sandbox | |
The list of all custom APIs |
https://api.businesscentral.dynamics.com/v1.0/ <your domain>/api/<ApiPublisher> /<ApiGroup>/<ApiVersion> |
https://api.businesscentral.dynamics.com/v1.0/ <sandbox tenant id>/sandbox/api/<ApiPublisher> /<ApiGroup>/<ApiVersion> |
The list of companies |
https://api.businesscentral.dynamics.com/v1.0/ <your domain>/api/<ApiPublisher> /<ApiGroup>/<ApiVersion>/companies |
https://api.businesscentral.dynamics.com/v1.0/ <sandbox tenant id>/sandbox/api/<ApiPublisher> /<ApiGroup>/<ApiVersion>/companies |
The list of your entities |
https://api.businesscentral.dynamics.com/v1.0/ <your domain>/api/<ApiPublisher> /<ApiGroup>/<ApiVersion>/companies (<companyId>)/<entitySetNames> |
https://api.businesscentral.dynamics.com/v1.0/ <sandbox tenant id>/sandbox/api/<ApiPublisher> /<ApiGroup>/<ApiVersion>/companies (<companyId>)/<entitySetNames> |
Great! So how to publish and consume them?
Create a new page with type API
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; layout { area(Content) { repeater(GroupName) { field(id; Id) { Caption = 'Id'; ApplicationArea = All; } field(icaoCode; "ICAO Code") { Caption = 'icaoCode'; ApplicationArea = All; } field(description; Description) { Caption = 'description'; ApplicationArea = All; } } } } trigger OnInsertRecord(BelowxRec: Boolean): Boolean begin Insert(true); Modify(true); exit(false); end; trigger OnModifyRecord(): Boolean var AirplaneModel: Record "AIR Airplane Model"; begin AirplaneModel.SETRANGE(Id, Id); AirplaneModel.FINDFIRST; IF "ICAO Code" <> AirplaneModel."ICAO Code" THEN BEGIN AirplaneModel.TRANSFERFIELDS(Rec, FALSE); AirplaneModel.RENAME("ICAO Code"); TRANSFERFIELDS(AirplaneModel); END; end; }
Remember some important rules:
- Fields should be named in the APIs supported format
- Add Insert(true) for OnInsert Trigger. Cos when you insert an entity through API endpoint, Business Central don’t run OnInsert trigger on the table. And you remember – we assign the ID for the new record there.
- Add business logic to Modify trigger. Remember that an external user can change values through API, even the value of the primary key field.
- Add Delete(true) for On Delete trigger. The same story as described above.
That’s it!
You’ve created custom API. But it’s not available yet. We have one final step to be able to consumpt it.
Publish your app
Just publish your app, as usual.
Now let’s test our custom API, using Postman.
Before publishing
After publishing
Amazing!!!
From that moment we have our own API on the cloud Business Central!
Explore custom APIs
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
Get the list of companies
Get the list of custom entities
Insert a new custom entity
Update custom entity
Delete custom entity
Conclusion
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.
One more thing
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.
Stay tuned!
*This post is locked for comments