Skip to main content

Functions

Function artifacts encapsulate reusable logic that can be called from any integration artifact. By default, every function is added to the functions.bal file in the project root.

Adding a function

  1. Click + next to Functions in the sidebar. Alternatively, click + Add Artifact in the Design panel, then click Function under Other Artifacts or Library Artifacts.

    Artifacts page with the Function option highlighted under Other Artifacts

  2. Fill in the Create New Function form.

    Create New Function form with Name, Description, Public, Parameters, and Return Type fields

    FieldDescription
    NameA unique identifier for the function (for example, validateOrder). Required.
    DescriptionOptional description of the function's purpose.
    PublicSelect Make visible across the project to allow use from other integrations.
    ParametersClick + Add Parameter to define each input. Each parameter requires a name and a type, and optionally a description.
    Return TypeThe type of the value returned by the function. Leave empty for functions that return nothing.
  3. Click Create. The function is added to functions.bal and opens in the flow designer canvas, where you add the integration steps as the function body.

Editing a function

To open and edit a function's flow view, click its name in the sidebar under Functions.

To change the function's name, description, parameters, or return type, click Configure in the top-right of the flow view.

Function flow view with a function selected in the sidebar and the Configure button highlighted

Project organization

By default, all functions are added to a single functions.bal file at the project root. For larger projects, you can split functions into additional .bal files grouped by domain.

my-integration/
├── functions.bal # Default file for all functions
├── types.bal
├── connections.bal
└── main.bal

To split functions across multiple files, create new .bal files at the project root and move related functions into them.

my-integration/
├── functions.bal # General-purpose functions
├── validation.bal # Input validation functions
├── types.bal
├── connections.bal
└── main.bal

Best practices

PracticeDescription
Typed parametersUse specific record types instead of json or anydata.
Error returnsReturn error? for operations that can fail.
Isolated functionsMark functions as isolated. The compiler then verifies there is no unsafe access of shared mutable state, making them safe to call concurrently.
Descriptive namesStart function names with a verb (for example, validateOrder, calculateTotal).

What's next

  • Types — Define record types for function parameters and return values.
  • Data mapper — Transform data between record types using a visual canvas.
  • Connections — Reuse connection configurations across integration artifacts.
  • Configurations — Externalize values such as endpoints and credentials.