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 keeps your integrations aligned with the API contract and removes the need to write boilerplate code.

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. In the canvas, click + Add Artifact (or Add Artifact manually if the integration has no artifacts yet), 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 SpecificationSelect Import From OpenAPI Specification
  4. Click Select File and choose your OpenAPI specification file (YAML or JSON).

  5. Enter a Service Type Name. Optionally, expand Advanced Configurations to choose between the Shared Listener (Port 9090) and a Custom Listener.

  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. In the canvas, click + Add Artifact (or Add Artifact manually if the integration has no artifacts yet).

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

  3. Select Connect via API Specification. In the Import API Specification step, keep Specification Type as OpenAPI, enter a Connector Name, choose your OpenAPI specification file under Import Specification File, and click Save Connector.

    Client generation from OpenAPIClient generation from OpenAPI
  4. In the Create Connection step, configure the optional connection settings:

    • Config: The configurations to use when initializing the connector.
    • Service Url: URL of the target service.
    Connection details configurationConnection 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 the ⋮ (more options) icon in the service header.

  3. Select Export OpenAPI Spec.

    Export OpenAPI from serviceExport OpenAPI from service
  4. Choose the folder to save the specification in, and click Select OAS Save Location.

Command reference​

Generate flags​

bal openapi -i <openapi-spec-path> [options]
FlagAliasRequiredDefaultDescription
-i, --input—Yes—Path to the OpenAPI specification file (YAML or JSON)
--mode—NoBothGeneration mode: client, service, or omit for both
-o, --output—NoCurrent directoryOutput directory for generated files
--tags—NoAll tagsComma-separated list of tags to include
--operations—NoAll operationsComma-separated list of operation IDs to include
--nullable—NofalseGenerate nullable types for optional fields
--client-methods—NoresourceClient method type: resource or remote
--with-tests—NofalseGenerate test skeletons for the client or service
--with-service-contract—NofalseGenerate a service contract type for compile-time service validation
--license—No—Path to a license header file to include in generated files
--status-code-binding—NofalseGenerate status-code-specific return types
--single-file—NofalseGenerate 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. The tool derives the endpoints, operations, and request/response types from the spec, so you don't have to define them manually as you would with a generic HTTP connector.

  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. The generated service contract type is validated at compile time, so your implementation can't drift from the agreed contract.

  1. Author your OpenAPI spec.
  2. Generate service stubs: bal openapi -i my-api.yaml --mode service --with-service-contract
  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
string—string
stringdatestring
stringdate-timestring
stringbytebyte[]
stringbinarybyte[]
integerint32int
integerint64int
numberfloatfloat
numberdoubledecimal
boolean—boolean
array—T[]
object—record {}
oneOf—Union type
allOf—Type inclusion

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