Feature
AL HttpClient code generator for Business Central
Updated 21 November 2026
Getting an API call to work in a client is the easy half. Rewriting it in AL, with the right headers on the right object, the body encoded, the token acquired and the secret kept out of plain text, is where the afternoon goes. Ava writes that code from the request you just sent.
From a working request to a procedure
Send the request until it returns what you want. Then press Code and choose AL. Ava generates a procedure that makes the same call: the URL with its query parameters, the method, every enabled header, the body with its content type, and the authentication you configured. Copy it into a codeunit in your extension.
This is what a POST that creates a customer with OAuth2 client credentials looks like:
procedure CreateCustomer(var ResponseText: Text): Boolean
var
Client: HttpClient;
RequestMessage: HttpRequestMessage;
ResponseMessage: HttpResponseMessage;
Headers: HttpHeaders;
Content: HttpContent;
ContentHeaders: HttpHeaders;
RequestBody: Text;
AccessToken: SecretText;
begin
RequestMessage.SetRequestUri('https://api.businesscentral.dynamics.com/v2.0/<tenant-id>/sandbox/api/v2.0/companies(<company-id>)/customers');
RequestMessage.Method := 'POST';
RequestMessage.GetHeaders(Headers);
if not AcquireAccessToken(AccessToken) then
Error('Failed to acquire access token.');
Headers.Add('Authorization', SecretStrSubstNo('Bearer %1', AccessToken));
RequestBody := '{"displayName":"Adatum Corporation","type":"Company"}';
Content.WriteFrom(RequestBody);
Content.GetHeaders(ContentHeaders);
ContentHeaders.Remove('Content-Type');
ContentHeaders.Add('Content-Type', 'application/json');
RequestMessage.Content := Content;
if not Client.Send(RequestMessage, ResponseMessage) then
exit(false);
ResponseMessage.Content.ReadAs(ResponseText);
exit(ResponseMessage.IsSuccessStatusCode());
end;
A second procedure, AcquireAccessToken(var AccessToken: SecretText): Boolean, posts the client credentials to the Microsoft Entra token endpoint, reads access_token from the JSON answer into a SecretText, and returns false if anything went wrong. The main procedure calls it before adding the Authorization header.
The details it gets right
- Content-Type goes on the content, not the request. In AL the request's
HttpHeadersand the content'sHttpHeadersare different objects. AddContent-Typeto the wrong one and the runtime refuses it. The generated code removes the default and adds yours onContentHeaders. - Secrets stay secret. Tokens are held in
SecretTextand combined withSecretStrSubstNo, so they do not appear in debugger watches or telemetry as plain text. - Quotes are escaped. AL strings use single quotes, and a JSON body is full of double quotes and the occasional apostrophe. Ava escapes them, and splits a long body across lines so the compiler accepts it.
- Query parameters are encoded the same way Ava sends them, so
$filter=displayName eq 'Adatum'arrives at Business Central as it did from the client. - The result is yours to check. The procedure returns whether the status was a success and hands back the body text, so your code decides what to do with a 400 and its message.
Every authentication type
| Auth in Ava | Generated AL |
|---|---|
| Bearer token | An Authorization: Bearer header. |
| Basic | Codeunit Base64 Convert encodes the user and password into an Authorization: Basic header. |
| API key | The key as a header, or appended to the URL as a query parameter, whichever you chose. |
| OAuth2 client credentials | An AcquireAccessToken procedure that posts to the token endpoint and returns a SecretText. |
| OAuth2 authorization code | A token procedure built on Business Central's own OAuth2 codeunit, calling AcquireTokenByAuthorizationCode with your client ID, authority, redirect URI and scopes. It prompts the user to sign in, as this flow must. |
| OAuth2 password | A token procedure posting the username and password grant. |
The secret you entered in Ava is written into the code only when you generate it, from the server where it is encrypted. The browser never has it, so the code is generated server-side rather than from the masked value the browser shows.
Bodies
JSON, raw text, XML and form-encoded bodies are written as a Text and sent with the matching Content-Type. GraphQL queries are wrapped in the JSON envelope the server expects. For a file upload the generated code leaves a marked place to load the content from an InStream, because AL has no sensible way to embed a binary in source.
cURL as well
The same button produces a cURL command, with quoting that survives a POSIX shell, for a ticket, a script or a colleague who does not use Ava. Form-data and binary bodies are included, so the command sends what Ava sent.
What it is not
The generated procedure is a correct, readable starting point, not a framework. It does not retry, cache the token across calls, or page through @odata.nextLink; those are design decisions for your extension. The guide AL HttpClient example walks through a complete codeunit, including JSON parsing and error handling, if you want to see how the pieces fit together.
Questions
Which plan includes code generation?
AL and cURL code generation are Pro features. The Free plan includes every Business Central feature, such as Explore and request bodies from $metadata.
Does the generated AL compile?
It is written to compile as a procedure inside a codeunit on current Business Central versions, using SecretText and SecretStrSubstNo for tokens. You add it to your own object and give it a name; Ava names the procedure after your request.
Does it handle OAuth2 tokens?
Yes. For OAuth2 client credentials, the generated code includes an AcquireAccessToken procedure that posts to the Microsoft Entra token endpoint and returns the token as a SecretText.
Can I generate code for a custom API page?
Yes. Code is generated from whatever request you sent, so a call to api/<publisher>/<group>/<version> works exactly like one to the standard API.
