Kotlin

Ktor Server — Hello World

admin by @admin ADMIN
5d ago
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
Ktor lets you stand up an HTTP server in a few lines. Pick an engine (Netty / Jetty / CIO), declare routes, run. Coroutine-native end to end.
Kotlin
Raw
// build.gradle.kts:
//   implementation("io.ktor:ktor-server-core:2.3.+")
//   implementation("io.ktor:ktor-server-netty:2.3.+")

import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*

fun main() {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!")
            }
            get("/health") {
                call.respondText("OK")
            }
            get("/greet/{name}") {
                val name = call.parameters["name"] ?: "stranger"
                call.respondText("Hello, $name!")
            }
        }
    }.start(wait = true)
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.