Skip to main content

OpenAPI Tool

The bal openapi tool bridges OpenAPI specifications and Ballerina code. It generates type-safe Ballerina service stubs and client connectors from OpenAPI (Swagger) YAML or JSON files, and can also export an OpenAPI specification from an existing Ballerina service. This ensures your integrations conform to API contracts and eliminates boilerplate.

Generating a Ballerina service from OpenAPI

Create a service skeleton that matches an OpenAPI specification. The generated code includes resource functions, request/response types, and validation constraints.

  1. Click the + Add Artifacts button in the canvas or click + next to Entry Points in the sidebar.

  2. In the Artifacts panel, select HTTP Service under Integration as API.

  3. Select Import From OpenAPI Specification under Service Contract.

    Select Import From OpenAPI Specification

  4. Browse or enter the path to your OpenAPI specification file (YAML or JSON).

  5. Configure the Service Base Path and listener settings.

  6. Click Create.

  7. WSO2 Integrator generates the service with resource function stubs, request/response types, and validation constraints matching your OpenAPI specification.

Generating a Ballerina client from OpenAPI

Create a type-safe HTTP client that wraps all API operations defined in the spec.

  1. Click the + Add Artifacts button in the canvas.

  2. In the Artifacts panel, select Connection under Other Artifacts.

  3. Select Connect Via API Specification and provide the OpenAPI spec file.

    Client generation from OpenAPI

  4. In the Create Connection step, configure the connection details. Expand Advanced Configurations to set the following optional fields:

    • Config — The configurations to use when initializing the connector.
    • Service Url — URL of the target service.

    Connection details configuration

  5. Enter a Connection Name for the generated client (for example, openapiClient).

  6. Click Save Connection.

  7. WSO2 Integrator generates a type-safe client connector with methods matching each API operation.

Generating both service and client

# Generate service and client together
bal openapi -i openapi.yaml

# This creates both:
# openapi_service.bal -- Service stub
# client.bal -- Client connector
# types.bal -- Shared types

Exporting OpenAPI from a Ballerina service

Generate an OpenAPI specification from an existing Ballerina HTTP service:

  1. Open the HTTP service in the Service Designer.

  2. Click Export in the service header toolbar.

  3. Select Export OpenAPI Specification.

    Export OpenAPI from service

  4. Choose the output location.

  5. Click Select OAS Save Location.

Command reference

Generate flags

bal openapi -i <openapi-spec-path> [options]
FlagAliasRequiredDefaultDescription
-i, --inputYesPath to the OpenAPI specification file (YAML or JSON)
--modeNoBothGeneration mode: client, service, or omit for both
-o, --outputNoCurrent directoryOutput directory for generated files
--tagsNoAll tagsComma-separated list of tags to include
--operationsNoAll operationsComma-separated list of operation IDs to include
--nullableNofalseGenerate nullable types for optional fields
--client-methodsNoresourceClient method type: resource or remote
--with-testsNofalseGenerate test skeletons for the client or service
--with-service-contractNofalseGenerate a service contract type for compile-time service validation
--licenseNoPath to a license header file to include in generated files
--status-code-bindingNofalseGenerate status-code-specific return types
--single-fileNofalseGenerate all types and client/service in a single file

Export flags

bal openapi -i <ballerina-service-file> [options]
FlagRequiredDefaultDescription
-s, --serviceNoAll servicesBase path of the service to export
-o, --outputNoCurrent directoryOutput directory for the specification file
--jsonNofalseExport in JSON format (default is YAML)

Customizing generated code

Filtering by tags or operations

# Generate only order-related operations
bal openapi -i openapi.yaml --mode service --tags orders

# Generate specific operations
bal openapi -i openapi.yaml --mode client --operations getOrders,createOrder

Handling nullable fields

# Treat all optional fields as nullable
bal openapi -i openapi.yaml --nullable

Workflow examples

Connecting to an external API

If the API provider supplies an OpenAPI spec, generate a type-safe client from it — no need to manually configure endpoints, methods, or request/response types. Everything is inferred from the spec. Without this, you would fall back to a generic HTTP connector and handle all of that yourself.

  1. Obtain the OpenAPI spec from the API provider.
  2. Generate the client: bal openapi -i partner-api.yaml --mode client
  3. Use the generated client and types in your integration logic.

Contract-first service design

Design your API as an OpenAPI spec first, then generate service stubs from it. Fill in the implementation knowing you cannot drift from the agreed contract.

  1. Author your OpenAPI spec.
  2. Generate service stubs: bal openapi -i my-api.yaml --mode service
  3. Implement the generated resource functions.

Publishing your API

Once your service is built, export its OpenAPI specification and share it with consumers. They can then use it to generate their own type-safe clients, just as you would when connecting to an external API.

  1. Export the spec from your service: bal openapi -i service.bal
  2. Share the generated spec with your API consumers.

OpenAPI to Ballerina type mapping

OpenAPI typeOpenAPI formatBallerina type
stringstring
stringdatestring
stringdate-timestring
stringbytebyte[]
stringbinarybyte[]
integerint32int
integerint64int
numberfloatfloat
numberdoubledecimal
booleanboolean
arrayT[]
objectrecord {}
oneOfUnion type
allOfIntersection type

What's next

  • GraphQL Tool — Generate GraphQL services from SDL schemas
  • gRPC Tool — Generate gRPC services from Protocol Buffer definitions
  • Flow Diagram editor — Build logic visually on top of generated stubs