How to Write a REST API in Kotlin in 30 Minutes: A Practical Guide
Kotlin is a modern programming language that is rapidly gaining popularity in backend development. Thanks to its concise syntax, full compatibility with Java, and powerful tools such as the Ktor framework, creating a REST API becomes a fast and enjoyable process. In this article, we will practically demonstrate how to write a simple but fully functional REST API in Kotlin in literally half an hour. You will learn how to set up a project, create endpoints, handle JSON, and start the server.
This article is intended for developers familiar with the basics of Kotlin but does not require deep knowledge of backend development. All code will be accompanied by detailed explanations.
1. Project Setup and Adding Dependencies
First, we need to create a new Kotlin project. We will use Gradle, the most popular build system for Kotlin. If you are using IntelliJ IDEA, select "New Project" -> "Kotlin" -> "JVM | IDEA". Otherwise, create the project folder manually.
First, open the build.gradle.kts file and add the following dependencies for Ktor and JSON serialization:
plugins { kotlin("jvm") version "1.9.22" kotlin("plugin.serialization") version "1.9.22" application}
repositories { mavenCentral()}
dependencies { implementation("io.ktor:ktor-server-core:2.3.7") implementation("io.ktor:ktor-server-netty:2.3.7") implementation("io.ktor:ktor-server-content-negotiation:2.3.7") implementation("io.ktor:ktor-serialization-kotlinx-json:2.3.7") implementation("ch.qos.logback:logback-classic:1.4.14")}
application { mainClass.set("ApplicationKt")}After synchronizing Gradle, we are ready to write code. Note that we are using the kotlinx.serialization plugin for working with JSON — this is the standard approach in Kotlin.
2. Creating the Data Model and Main Server File
For the REST API, we need a data model. Let it be a simple to-do list. Create a file Todo.kt:
import kotlinx.serialization.Serializable
@Serializabledata class Todo( val id: Int, val title: String, val completed: Boolean = false)Now let's create the main file Application.kt (or Main.kt). In it, we will configure the server and start it:
import io.ktor.server.engine.*import io.ktor.server.netty.*import io.ktor.server.application.*import io.ktor.server.routing.*import io.ktor.server.response.*import io.ktor.server.request.*import io.ktor.serialization.kotlinx.json.*import io.ktor.server.plugins.contentnegotiation.*
fun main() { embeddedServer(Netty, port = 8080, host = "0.0.0.0") { configureSerialization() configureRouting() }.start(wait = true)}
fun Application.configureSerialization() { install(ContentNegotiation) { json() }}
fun Application.configureRouting() { routing { get("/") { call.respondText("Hello, REST API on Kotlin!") } }}This code starts the server on port 8080. We have included the ContentNegotiation module for automatic JSON serialization/deserialization. Run the application — you should see a welcome message in your browser at http://localhost:8080.
3. Implementing CRUD Endpoints (Create, Read, Update, Delete)
Now let's add full REST routes for managing tasks. We will use a mutableListOf list to store data in memory (in a real project, you would replace this with a database).
Let's create a file TodoRepository.kt:
import kotlinx.coroutines.sync.Muteximport kotlinx.coroutines.sync.withLock
class TodoRepositor