Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 38 ยท Getting Started

What Is Ktor?

Meet Ktor, the coroutine-based Kotlin framework for servers and clients from JetBrains. Learn what it does, how it differs from Spring, and what you will build.

Ktor is a framework for building asynchronous servers and clients in Kotlin. JetBrains created it, and it is written in Kotlin from the first line: it uses coroutines for concurrency, lambdas with receivers for its configuration, and extension functions for almost everything you add to it. If you already know Kotlin, a Ktor program reads like ordinary Kotlin code and not like a pile of annotations and XML.

This course teaches the server side of Ktor: routing, plugins, JSON, authentication, databases, WebSockets, testing and deployment. In this first lesson you will see what Ktor is, how it compares with other frameworks, and the small set of ideas that the rest of the course builds on.

About the examples. Ktor itself is a set of Gradle dependencies, and our in-browser runner has no Gradle, no Maven Central and no Ktor jars. So every lesson shows the real Ktor code in read-only blocks, and next to it gives a runnable stand-in: a small plain-Kotlin program that demonstrates the same idea (routing tables, pipelines, JWT signing, and so on). Stand-ins are always labelled. They teach how the machinery works; the real code shows how you spell it in Ktor.

What Ktor gives you

Ktor is deliberately small. The core does very little: it receives an HTTP request, runs it through a pipeline, and sends a response. Everything else is a plugin you opt into.

What Ktor is

Coroutine based Handlers are suspend functions
Plugins Install only what you need
Kotlin DSL Routes are plain code

No annotations, no classpath scanning.

1 / 3

A Ktor server application typically has these parts:

  • An engine that listens on a port and speaks HTTP (Netty, CIO, Jetty, or Tomcat).
  • An application with one or more modules, which are just functions that configure it.
  • Routing: a tree of URL patterns and handlers.
  • Plugins: JSON conversion, authentication, sessions, logging, compression, CORS, and many more.

Ktor also ships an HTTP client with the same coroutine style. The Kotlin track's Ktor HTTP server and client lesson shows a first taste of both.

The smallest Ktor server

This is a complete Ktor 3 server. It is read-only here, because the runner cannot download the dependencies:

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

fun main() {
    embeddedServer(Netty, port = 8080) {
        routing {
            get("/") {
                call.respondText("Hello, Ktor!")
            }
        }
    }.start(wait = true)
}

Read it slowly, because every Ktor program is a variation on it:

  1. embeddedServer(Netty, port = 8080) creates a server using the Netty engine.
  2. The trailing lambda configures the application.
  3. routing { ... } installs the routing plugin and gives you a DSL to register routes.
  4. get("/") { ... } handles GET /. The block is a suspend lambda; call is the current request/response pair.
  5. call.respondText(...) is a suspend function that writes the response.
  6. start(wait = true) starts the engine and blocks the main thread so the program keeps running.

Stand-in: a server as "a function from request to response"

Strip away the engine and the plugins and a web framework is a mapping from a request to a response. The stand-in below models that idea in plain Kotlin, with a route table keyed by method and path. This is the mental model behind routing { get("/") { ... } }.

Kotlin Stand-in: routes are handlers in a table

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Notice miniServer { get(...) }: that is a lambda with receiver, the same Kotlin feature behind embeddedServer { routing { get(...) } }. The next lessons build on it.

Ktor compared with other frameworks

Ktor Spring Boot
Language Kotlin first Java first, Kotlin supported
Style Explicit DSL code Annotations and auto-configuration
Concurrency Coroutines Threads (or WebFlux with Reactor)
Startup Fast, small footprint Heavier
Batteries Pick plugins yourself Large integrated ecosystem

Neither is "better": Spring has a huge ecosystem; Ktor gives you control and a very light core. If you like seeing exactly what your server does, Ktor suits you. If you know Spring already, compare with the Spring Boot lessons.

Why coroutines matter

A traditional server dedicates a thread to each request. A Ktor handler is a coroutine: when it waits for a database or a remote API, it suspends and frees its thread for another request. A few threads can serve thousands of concurrent calls. The lesson on how Ktor handles a request shows this in action, and the deployment chapter returns to it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Ktor use to run handlers concurrently?

    Ktor handlers are suspend functions, so they can wait without blocking a thread.
  2. 2. What is a Ktor plugin?

    The core is minimal; plugins add features and are installed with install(...).
  3. 3. In embeddedServer(Netty, port = 8080) { ... }, what is Netty?

    Netty is one of several engines (CIO, Jetty and Tomcat are others).
  4. 4. Why do the lessons in this course show stand-ins?

    Real Ktor code is shown read-only; stand-ins make the underlying mechanics runnable.

Summary

  • Ktor is a small, coroutine-based Kotlin framework for servers and clients.
  • The core is an engine plus a pipeline; features come from plugins.
  • Routes are written as Kotlin code with lambdas with receivers.
  • A server is conceptually a function from a request to a response.
  • Stand-ins in this course are runnable Kotlin programs that show the mechanics; real Ktor code is read-only.

Next: Project Setup with Gradle