Serverless functions are great especially when used as backend for mobile apps. A while ago I wrote a blog post showing you how to Send an SMS message from Android and always wanted to change its backend application to not only use Kotlin, but also be a serverless function.

Having a backend is important because we’re using an API and while Twilio is a REST API and you could make an HTTP request to it directly, you would need to store your Twilio credentials inside your app. This is not secure because an attacker can extract your account credentials very easily from it.

Let’s look at how to change our backend to be a serverless function that is hosted on IBM’s Cloud Function.

Our tools

I will be using IntelliJ IDEA with the Gradle Plugin for the code but feel free to use your preferred IDE as long as it works well with Gradle.

A Twilio Account and a Twilio Phone Number – Sign up for free!

An IBM Bluemix Account – Sign up free. Also, make sure you follow their instructions for getting the CLI installed and working.

If you just want to skip to the code, feel free to clone this repository and just follow the deployment procedure.

Create the project

Open up IntelliJ IDEA and click File → New.. → Project → Gradle → Kotlin (Java). Click “Next” and give it a Group ID and an Artifact ID . The group ID is usually a reverse domain such as com.twilio but you can use whatever you want here. The artifact ID is the the project name, which we will call ServerlessSMS .

In the next screen, make sure you check the “use auto-import” option, click “Next” again and choose the location for your project. I stored mine in ~/Projects/Kotlin/ServerlessSMS and clicked “Finish”.