DEV Community

Alex Spinov
Alex Spinov

Posted on

Ballerina Has a Free API: A Language Built for Cloud-Native Integration

Ballerina is a programming language by WSO2 designed specifically for cloud-native integration. It treats network services, APIs, and data as first-class concepts in the type system.

Why Ballerina Matters

Most languages bolt on HTTP, JSON, and API concepts through libraries. Ballerina makes them core language features. Network interactions are part of the type system, and sequence diagrams are auto-generated from code.

What you get for free:

  • Network-aware type system (JSON, XML, HTTP are built-in types)
  • Automatic sequence diagram generation from code
  • Built-in HTTP, GraphQL, gRPC, WebSocket clients/servers
  • Native data transformation (JSON to record, XML to JSON)
  • Built-in concurrency with workers
  • Automatic OpenAPI spec generation
  • Kubernetes and Docker deployment built in

Quick Start

# Install from ballerina.io
curl -fsSL https://dist.ballerina.io/downloads/latest/install.sh | bash

# Create project
bal new my_service
cd my_service

# Run
bal run

# Build Docker image
bal build --cloud=docker

# Build for Kubernetes
bal build --cloud=k8s
Enter fullscreen mode Exit fullscreen mode

HTTP Service

import ballerina/http;

type User record {|
    int id;
    string name;
    string email;
|};

service /api on new http:Listener(8080) {

    resource function get users() returns User[] {
        return [
            {id: 1, name: "Alice", email: "alice@example.com"},
            {id: 2, name: "Bob", email: "bob@example.com"}
        ];
    }

    resource function get users/[int id]() returns User|http:NotFound {
        User? user = findUser(id);
        if user is User {
            return user;
        }
        return http:NOT_FOUND;
    }

    resource function post users(@http:Payload User user)
            returns User|http:BadRequest {
        User|error created = createUser(user);
        if created is error {
            return http:BAD_REQUEST;
        }
        return created;
    }
}
Enter fullscreen mode Exit fullscreen mode

JSON as a Native Type

import ballerina/io;

public function main() {
    // JSON is a first-class type
    json payload = {
        "name": "Alice",
        "age": 30,
        "skills": ["Go", "Rust", "Ballerina"]
    };

    // Access fields safely
    json|error name = payload.name;
    if name is json {
        io:println("Name: ", name);
    }

    // Transform JSON to typed record
    type Person record {
        string name;
        int age;
        string[] skills;
    };

    Person|error person = payload.cloneWithType();
    if person is Person {
        io:println("Person: ", person.name, ", Age: ", person.age);
    }
}
Enter fullscreen mode Exit fullscreen mode

HTTP Client + Data Integration

import ballerina/http;
import ballerina/io;

type GithubRepo record {
    string name;
    string description;
    int stargazers_count;
    string language;
};

public function main() returns error? {
    http:Client github = check new ("https://api.github.com");

    GithubRepo[] repos = check github->get("/users/nicekid1/repos");

    // Filter and transform
    var topRepos = from var repo in repos
        where repo.stargazers_count > 0
        order by repo.stargazers_count descending
        limit 5
        select {name: repo.name, stars: repo.stargazers_count};

    io:println("Top repos: ", topRepos);
}
Enter fullscreen mode Exit fullscreen mode

Concurrent Workers

import ballerina/http;

public function main() returns error? {
    // Parallel API calls using workers
    worker weatherWorker returns json|error {
        http:Client client = check new ("https://api.weather.com");
        return client->get("/current?city=London");
    }

    worker newsWorker returns json|error {
        http:Client client = check new ("https://api.news.com");
        return client->get("/top-headlines");
    }

    // Wait for both
    json|error weather = wait weatherWorker;
    json|error news = wait newsWorker;
}
Enter fullscreen mode Exit fullscreen mode

GraphQL Service

import ballerina/graphql;

service /graphql on new graphql:Listener(9000) {
    resource function get user(int id) returns User|error {
        return findUser(id);
    }

    resource function get users() returns User[] {
        return getAllUsers();
    }

    remote function createUser(string name, string email) returns User|error {
        return createUser({name, email});
    }
}
Enter fullscreen mode Exit fullscreen mode

Useful Links


Building integration pipelines? Check out my developer tools on Apify for ready-made web scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)