DEV Community

AkhilProto
AkhilProto

Posted on

Building a REST API with Ballerina: A Beginner’s Walkthrough

Building a REST API with Ballerina: A Beginner’s Walkthrough

In today’s world, APIs are at the heart of software development, and building them can be both exciting and challenging. If you're tired of dealing with overly complex setups and boilerplate code, Ballerina might just be the solution you're looking for.

This tutorial will walk you through building a simple REST API using Ballerina, a programming language built for integration and cloud-native applications. Let's dive in!

Why Use Ballerina for APIs?

Ballerina is designed for API-centric development, with built-in support for creating RESTful services with minimal effort. Key features include:

  • Easy-to-read syntax
  • Type-safe handling of data
  • Built-in concurrency and error handling
  • Cloud-native and microservice-ready

Step 1: Install Ballerina

Before we start, you'll need to install Ballerina. You can download it from Ballerina’s official website.

Once installed, verify the installation by running:

ballerina -v
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Your First Ballerina Project

Let’s start by creating a new Ballerina project. In your terminal, run:

ballerina new my_api_project
cd my_api_project
Enter fullscreen mode Exit fullscreen mode

This will create a new directory for your API project.

Step 3: Writing the REST API

Now, let’s create a simple REST API. Open the main.bal file in the my_api_project folder, and replace its contents with the following:

import ballerina/http;

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

    // GET method to retrieve a message
    resource function get message() returns json {
        json response = { "message": "Hello, Ballerina!" };
        return response;
    }

    // POST method to send data
    resource function post message(http:Request req) returns json {
        json requestPayload = check req.getJsonPayload();
        return { "received": requestPayload };
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Running Your API

To run your API, go back to the terminal and execute:

ballerina run my_api_project
Enter fullscreen mode Exit fullscreen mode

Your API will now be running on http://localhost:8080/api/message. Let’s test it out!

Step 5: Testing the API

GET Request

Open your browser or use Postman to send a GET request to:

http://localhost:8080/api/message
Enter fullscreen mode Exit fullscreen mode

You should get the following response:

{
    "message": "Hello, Ballerina!"
}
Enter fullscreen mode Exit fullscreen mode

POST Request

Using Postman or Curl, send a POST request to the same endpoint with a JSON payload. Here's an example using Curl:

curl -X POST http://localhost:8080/api/message -H "Content-Type: application/json" -d '{"name": "Ballerina"}'
Enter fullscreen mode Exit fullscreen mode

The API will respond with:

{
    "received": {
        "name": "Ballerina"
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 6: Conclusion

And there you have it—a fully functional REST API using Ballerina! With just a few lines of code, you were able to set up a GET and POST route, making it easy to interact with your service. Ballerina’s simplicity and built-in features make it a fantastic choice for developers looking to build APIs quickly and efficiently.

Now it’s your turn to expand this API with more routes, data validation, or even integrate it with a database.


If you found this tutorial helpful, let me know your thoughts in the comments! Or, if you've built something with Ballerina, share your experience!

Top comments (0)