Get Daily Exchange Rates via SMS Using Ballerina | by Malintha Ranasinghe | Aug, 2022

Stay financially up to date easily

Photo by Marga Santoso on Unsplash

I developed a service using Ballerina to receive daily exchange rates of fiat currencies. It allows you to subscribe to exchange rates of any conversions and receive an SMS each morning for the subscribed conversions. The following video depicts the functionality of the application:

Application functionality

I will walk you through the implementation of this application so that you can utilize this experience in building cool things with Ballerina. All right! Let’s first get started looking at the use case.

The service has two HTTP endpoints. One for subscribing to the service and the other for unsubscribing. When subscribing, a user should provide a mobile number and the list of conversions to be subscribed. When unsubscribing, the user has to only provide the mobile number with the HTTP request. The application should send an SMS each morning to each subscribed mobile number with respective conversions.

We should get to know the basics of Ballerina before we try to implement the application. Let’s first get to know about services and recurrent tasks and client objects in Ballerina. These constructs in Ballerina are important for us as we need to notify the subscribers recurrently each morning while providing the two HTTP endpoints for subscription management.

A service declaration in Ballerina represents a collection of remotely accessible resource methods attached to a particular listener. In our use case, the service should contain two methods for subscribing and unsubscribing, and it should be attached to an HTTP listener as depicted below:

Example conversions service

The following image depicts how we can run and test the endpoint.

There are a bunch of listeners provided by Ballerina corresponding to various transports. Refer to this example for a detailed guide.

In the use case above, it’s required to look up the conversion rates and notify the subscribers each morning about it. This can be done in Ballerina using recurrent jobs.

To create a recurrent task, a value of type Job should be provided. Job is an object type. Object types in Ballerina are somewhat similar to interfaces in java. An object type can be used to define a class using type inclusion similar to implementing an interface.

However, it should be noted that Ballerina is not an OOP language. The Ballerina-type system is structural. It uses the concept called shapes. Refer Ballerina type system for a detailed guide.

A class type in Ballerina is used to create values of the particular class type(object instances of that class). In the following code segment, I have defined a class called MyJob which can be scheduled and executed by a scheduler as a one-time or recurrent task.

Example Job

The init method is executed when a value of type MyJob is created which is similar to the constructor in a java class. The execute method is the implementation of the unimplemented method execute in the task:Job object type mentioned above.

We can use client objects to interact with remote services in Ballerina. Remote services can be any service available over a particular transport. For example, the http client in Ballerina allows us to interact with an HTTP remote service. We can use the http client to retrieve the exchange rate for two currencies, as depicted below:

Example client object

In the above implementation, we have used the http client to invoke the remote HTTP service provided by An API key should be obtained to invoke the endpoint. The API key can be specified as a configurable variable in Ballerina. Refer to configurable variables for examples.

As we are only interested in the result, the above program can be improved to clone the response with a type definition. Here’s the code:

Improved example for client objects

Now let’s see how we can use what we have seen in the background section to implement the original requirement. We can further improve the Job class as follows:

Job class for the use case

The Job class should maintain a set of subscribers to whom it must send messages each morning. Further, it should also define the logic for adding or removing a subscriber. ThegetRatesAndSendSMS() method is responsible for the most important task, which is to retrieve exchange rates from the external HTTP service and notify the subscribers.

Now, the hard part is done. Let’s improve our /conversions HTTP remote service a bit with this code:

Improved conversions service for the use case

The init method of the service called when the service object is initialized. In the init method, we have created a Job object and scheduled a recurrent task that will execute once a minute (a minute mocks a day). That’s it for the implementation.

Hurrah!!! You can try this out, as I have shown in the video above.

In this article, we have seen the strengths of the Ballerina programing language and how easy it is to write an application for an integration scenario.

The next step is to deploy this application with proper authentications and authorizations. Let’s meet on a new article where I will discuss how we can improve and deploy this application on Choreo.

Find the source code at

News Credit

%d bloggers like this: