Skip to main content

Types

Type artifacts define the data structures used throughout your integration. They ensure type safety across services, event handlers, and transformations. Define named types once and reuse them across all artifacts in your project.

The visual designer exposes five type kinds: Record, Enum, Service Class, Union, and Array. Each kind maps to a Ballerina type definition in source.

Adding a type

  1. Open or create an integration/library in WSO2 Integrator.

  2. Click + next to Types in the sidebar (or click + Add Type from the Types canvas).

    WSO2 Integrator sidebar showing the project structure with Types listed

  3. In the New Type panel, choose Create from scratch or Import to generate a type from sample JSON or XML.

    New Type creation form showing Kind and Name fields

    FieldDescription
    KindThe type kind: Record, Enum, Service Class, Union, or Array.
    NameA unique name for the type (for example, Address).
    Members / Fields / Resource MethodsThe structural members for the selected kind. The label changes to match the kind.
    Advanced OptionsPer-kind options such as Allow Additional Fields, Is Readonly Type, and Accessible by Other Integrations.
  4. Click Save. The type is added to your project and appears in the type diagram.

Alternatively, click + Add Artifact in the Design panel and click Type under Other Artifacts or Library Artifacts.

Type diagram

Click View Type Diagram next to Types in the sidebar to open the visual type diagram. The canvas renders all types in your project as nodes, with arrows showing relationships between the types.

Type diagram canvas showing the visual representation of types in the project

Use the toolbar buttons at the bottom left to zoom in, zoom out, fit the diagram to the screen, or export it as an image.

Type kinds

Record

A record defines a mapping type with named fields and specific types for each field.

Select Record from the Kind dropdown, then add one row per field using +.

For each field:

ControlDescription
NameThe field name.
TypeThe field type (built-in or user-defined).
Inline record ({})Define a record-typed field's type as a nested record inline, instead of referencing an existing type.
Optional (?)Marks the field as optional. The field may be absent at runtime.
Delete (Trash)Remove the field.

Expand a field row to set:

OptionDescription
Default ValueThe default value to use for the field when a value is not specified explicitly.
DescriptionDocumentation for the field. Added as Ballerina field documentation.
ReadonlyMarks the field as immutable. The value set to the field must be immutable and the field cannot be assigned to once a value of this type is created.

Record type creation field options

Advanced Options:

OptionDescription
Allow Additional FieldsGenerates an open record that accepts fields that are not explicitly defined. Leave unchecked for a closed record that accepts only the defined fields.
Is Readonly TypeMarks the entire type as immutable. Fields cannot be assigned to once a record value is created, and the values set for all the fields must also be immutable.
Accessible by Other IntegrationsAdds the public qualifier so the type can be imported from other packages.

Enum

An enum defines a fixed set of named string values.

Select Enum from the Kind dropdown, then add each member using +.

For each member:

ControlDescription
Enum member nameThe member identifier (for example, PENDING).
Constant ExpressionThe string value bound to the member. Leave empty to use the member name as the value.

Service class

A service class defines an object with resource and remote methods. It is commonly used to model GraphQL object types, where each resource method becomes a field resolver.

Create a service class

Select Service Class from the Kind dropdown, then add one row per resource method using +.

For each resource method:

ControlDescription
NameThe resource path/name (for example, orders).
TypeThe return type of the resource method.
+ Add ParameterAdd an input parameter. Each parameter takes a Parameter Name, Parameter Type, and an optional Default Value.
Delete (Trash)Remove the resource method.

Edit the service class

The editing experience for a service class differs from other kinds. Click Edit on the type to open the Service Class Designer panel.

Click on Edit in the Service Class Designer panel to edit the name or the description of the class.

ControlDescription
Class NameRename the service class.
DescriptionFree-text documentation rendered as Ballerina documentation.

Class Variables: Click + Variable to open the Add Variable panel.

ControlDescription
Variable NameThe variable identifier.
Variable TypeThe variable type.
Default ValueThe initial value assigned at construction.

Methods: Click + Method to open the Add Method panel. Choose the method kind from the dropdown (resource or remote), then fill in:

ControlDescription
Resource Path/Function NameThe resource path (for resources) or method name (for remote methods).
DescriptionDocumentation for the method.
ParametersUse + Add Parameter to add inputs (name, type, optional default value).
Return TypeThe method return type.

Use the pencil and trash icons next to each existing method to edit or remove it.

Click on Constructor: init to modify the initializer method.

Service Class Designer showing Class Variables and Methods sections

Union

A union type lets a value take one (or more) of several member types. It is used to model alternatives, including error returns (T|error) or optional values.

Select Union from the Kind dropdown, then add each member type using +. Members can be built-in types or other user-defined, named types.

Advanced Options:

OptionDescription
Is Readonly TypeMakes the type immutable. A value belongs to the union type only if it belongs to at least one member type and is immutable.
Accessible by Other IntegrationsAdds the public qualifier so the type can be imported from other packages.

Array

An array defines an ordered list of values of a single member type.

Select Array from the Kind dropdown.

ControlDescription
Type of the ArrayThe member type (required).
Size of the ArrayA fixed length. Leave empty for an unbounded array.

Advanced Options:

OptionDescription
Is Readonly TypeMakes the array immutable. Any members specified when creating the array must also be immutable.
Accessible by Other IntegrationsAdds the public qualifier so the type can be imported from other packages.

Generate type from JSON or XML

Types can be generated from sample JSON or XML values instead of manually defining each field.

  1. In the New Type creation view, switch to the Import tab.

  2. Specify the source data format and the name of the type to generate.

    FieldDescription
    FormatThe source data format: JSON or XML.
    NameA unique name for the type (for example, Employees).

    New Type creation form showing type generation from JSON

  3. Click Save. The types are added to your project and appear in the type diagram.

Map types

The visual designer does not expose Map as a Kind. To model dynamic key/value data, define a record instead:

  • For a known set of keys, define the fields in a closed record (record {| ... |}).
  • For arbitrary string keys and plain data values, use an open record with Allow Additional Fields enabled.

If you are editing source directly, the equivalent Ballerina form is map<T> or record {| T...; |}. For example, map<string> or record {| string...; |} is the equivalent for a string-valued map.

Best practices

PracticeDescription
Closed recordsLeave Allow Additional Fields unchecked unless you actually need extra keys.
Reuse across artifactsDefine types once and reference them from services, event handlers, and functions.
Prefer Readonly over conventionsMark fields or whole types Readonly for immutable data, instead of relying on convention.
Use records instead of mapsWhere finer control is required over fields, use records instead of maps.

What's next

  • Connections — Define reusable connections to external systems.
  • Configurations — Externalize values such as endpoints and credentials.
  • Functions — Encapsulate reusable logic in Ballerina functions.
  • Data mapper — Map between record types visually.