Sending SMS in Kotlin: sms-client-kotlin Library API
Modern mobile applications and server solutions often require integration with SMS gateways to send notifications, confirmations, or marketing messages. For the Kotlin language, there is a convenient library sms-client-kotlin that provides a simple and type-safe API for working with popular SMS providers. In this article, we will thoroughly examine its architecture, methods, and usage examples.
What is sms-client-kotlin?
The sms-client-kotlin library is a lightweight HTTP client written in pure Kotlin using coroutines. It abstracts working with the REST API of various SMS providers (e.g., Twilio, Vonage, SMS-Center) and provides a unified interface for sending messages. Key advantages:
- Asynchrony — all requests are executed via Kotlin coroutines.
- Type safety — strict typing of request and response parameters.
- Modularity — support for multiple providers through a single
SmsSenderinterface. - Easy setup — minimal number of dependencies.
Installation and Setup
To add the library to your project, use Gradle. Add the dependency to the build.gradle.kts file:
dependencies { implementation("com.github.sms-client:sms-client-kotlin:1.2.0") implementation("io.ktor:ktor-client-core:2.3.0") implementation("io.ktor:ktor-client-cio:2.3.0") // or another engine}After synchronizing the project, you can create a client instance. Example initialization for the Twilio provider:
import sms.client.SmsClientimport sms.client.provider.twilio.TwilioConfig
fun main() { val config = TwilioConfig( accountSid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", authToken = "your_auth_token_here", fromNumber = "+1234567890" )
val client = SmsClient.create(config)}Main API Methods
The library provides several key methods for sending and managing SMS:
1. Sending a Single Message
The sendSms method takes the recipient's number and the message text. It returns an SmsResult object with information about the sending.
suspend fun sendSingleMessage(client: SmsClient) { val result = client.sendSms( to = "+79876543210", message = "Hello! This is a test message from Kotlin." )
when (result) { is SmsResult.Success -> println("Sent! ID: ${result.messageId}") is SmsResult.Failure -> println("Error: ${result.errorMessage}") }}2. Batch Sending
For mass mailing, use the sendBatch method. It takes a list of recipients and a common text (or individual messages).
suspend fun sendBatch(client: SmsClient) { val recipients = listOf( BatchRecipient("+79111111111", "10% discount on your first order!"), BatchRecipient("+79222222222", "Your confirmation code: 1234") )
val batchResult = client.sendBatch(recipients) println("Successfully sent: ${batchResult.successCount}, errors: ${batchResult.failureCount}")}3. Checking Delivery Status
The getDeliveryStatus method allows you to get the current status of a previously sent message by its identifier.
suspend fun checkStatus(client: SmsClient, messageId: String) { val status = client.getDeliveryStatus(messageId) println("Status of message $messageId: ${status.status} (${status.description})")}Error and Exception Handling
The library uses standard Kotlin mechanisms for error handling. All network errors