So, what is a Codeunit API?

Technically known as an OData Unbound Action, this approach exposes specific procedures from a Codeunit as an HTTP endpoint. Unlike Query and Page APIs, which tie directly to table structures, Codeunit APIs provide complete control over input parameters, logic execution, and response formatting.

A simple example

Here's a basic Codeunit:

codeunit 50100 "Hello"
{
    procedure Index(): Text
    begin
        exit('Hello from Odda Digital System!');
    end;
}

To make it accessible, register the Codeunit as a Web Service:

<?xml version="1.0" encoding="UTF-8"?>
<ExportedData>
    <TenantWebServiceCollection>
        <TenantWebService>
            <ObjectType>CodeUnit</ObjectType>
            <ObjectID>50100</ObjectID>
            <ServiceName>Hello</ServiceName>
            <Published>true</Published>
        </TenantWebService>
    </TenantWebServiceCollection>
</ExportedData>

Calling the API

Once deployed to Business Central, the procedure becomes an OData action. OData actions require POST requests, even for data retrieval.

Request:

POST https://api.businesscentral.dynamics.com/v2.0/<tenantId>/<env>/ODataV4/Hello_Index
Authorization: Bearer {{token}}
Content-Type: application/json

Response:

{
  "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/.../$metadata#Edm.String",
  "value": "Hello from Odda Digital System!"
}