Creating Your First Application

Estimated reading time: 3 minutes

This tutorial will help guide you through the steps needed to create a simple self-hosted Ktor server application that responds to HTTP requests with Hello, World!. Ktor applications can be built using common build systems such as Maven or Gradle.

Table of contents:

Including the right dependencies

Ktor is split up into several groups of modules, allowing you to include only the functionality that you will need.

For a list of these modules, please check the Artifacts page. In this case, you only need to include ktor-server-netty.

These dependencies are hosted on Bintray and as such the right repositories need to be added to your build script.

For a more detailed guide on setting up build files with different build systems see:

Creating a self-hosted Application

Ktor allows applications to run within an Application Server compatible with Servlets such as Tomcat, or as an embedded application, using Jetty or Netty.

In this tutorial, you will learn how to self-host an application using Netty.

You can start by creating an embeddedServer, passing in the engine factory as the first argument, the port as the second argument and the actual application code as the fourth argument (third argument is the host which is 0.0.0.0 by default).

The code below defines a single route that responds to the GET verb on the URL / with the text Hello, world!

After defining the routes, you have to start the server by calling server.start, passing as argument a boolean to indicate whether you want the main thread of the application to block.

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

fun main(args: Array<String>) {
    val server = embeddedServer(Netty, 8080) {
        routing {
            get("/") {
                call.respondText("Hello, world!", ContentType.Text.Html)
            }
        }
    }
    server.start(wait = true)
}

If your server is just listening for HTTP requests and do not want to do anything else after that in the setup, you will normally call the server.start with wait = true.

Running the Application

Given that the entry point of your application is the standard Kotlin main function, you can simply run it, effectively starting the server and listening on the specified port.

Checking the localhost:8080 page in your browser, you should see the Hello, world! text.

Next Steps

This was the simplest example of getting a self-hosted Ktor application up and running. A recommended tour to continue learning Ktor on the server would be: