Sending SMS in Kotlin with the smslib4k Library
In modern web development, integrating SMS notifications remains a popular task: two-factor authentication, order status alerts, reminders. For the Kotlin language, there is a convenient library smslib4k that provides a unified interface for working with popular SMS providers via REST API.
The library is written in pure Kotlin using coroutines (kotlinx.coroutines) and supports asynchronous message sending. It abstracts the differences between providers (Twilio, Vonage, SMS-Center, SMS-Rai, etc.), allowing the developer to focus on business logic.
Installation and Setup
To use smslib4k, add the dependency to build.gradle.kts:
repositories { mavenCentral()}
dependencies { implementation("io.github.smslib4k:smslib4k-core:2.1.0") // For a specific provider, e.g., Twilio: implementation("io.github.smslib4k:smslib4k-twilio:2.1.0")}After Gradle sync, the library is ready to use.
Basic Client Initialization
Each provider requires its own credentials. Let's look at the setup for the SMS-Center service (a popular Russian provider):
import io.github.smslib4k.core.SmsClientimport io.github.smslib4k.providers.smscenter.SmsCenterProvider
fun main() { val client = SmsClient( provider = SmsCenterProvider( login = "your_login", password = "your_password" ) ) println("SMS client initialized")}For Twilio, the structure will be similar, but it will require accountSid and authToken.
Sending a Single SMS
The library supports both synchronous and asynchronous sending. It is recommended to use coroutines:
import kotlinx.coroutines.runBlockingimport io.github.smslib4k.core.model.SmsMessage
fun main() = runBlocking { val client = createClient() // function from the previous example
val message = SmsMessage( to = "+79161234567", text = "Hello! This is a test message from Kotlin." )
val result = client.send(message) println("Status: ${result.status}") println("Message ID: ${result.messageId}")}The send method returns an SmsResult object containing the status (SUCCESS, FAILED, PENDING) and a unique identifier from the provider.
Bulk Sending
To send the same text to multiple numbers, use sendBatch:
val recipients = listOf( "+79161234567", "+79261234568", "+79361234569")
val batchResult = client.sendBatch( to = recipients, text = "Promotion! 20% off all items until the end of the week.")
batchResult.forEach { (phone, result) -> println("$phone: ${result.status}")}The library automatically handles provider limits on requests per second using a built-in rate-limiter.
Configuring Additional Parameters
Many providers support advanced options: sender name (alpha name), transactional flag, message time-to-live. In smslib4k, this is implemented via a builder:
val message = SmsMessageBuilder() .to("+79161234567") .text("Your verification code: 1234") .sender("MyApp") // alpha name (if allowed by the provider) .ttl(300) // time-to-live in seconds .isTransactional(true) // priority delivery .build()
client.send(message)Error Handling
The library throws exceptions for network issues or invalid credentials. It is recommended to wrap calls in try-catch:
t Also in library
Swift WebSocket Libraries: Starscream and URLSessionWebSocketTask
Working with Excel in Go (Golang): Excelize, xlsx, tealeg libraries
JWT Tokens in C++: Implementation and Verification with jwt-cpp
Java Standard Library: Complete Overview and Code Examples