QuickStart
Ktor is a framework to easily build connected applications – web applications, HTTP services, mobile and browser applications.Modern connected applications need to be asynchronous to provide the best experience to users, and Kotlin coroutines provideawesome facilities to do it in an easy and straightforward way.
While not yet entirely there, the goal of Ktor is to provide an end-to-end multiplatform application framework for connected applications. Currently, JVM client and server scenarios are supported, as well as JavaScript, iOS and Android clients, and we are working on bringing server facilities to nativeenvironments, and client facilities to other native targets.
Table of contents:
Set up a Ktor project
You can set up a Ktor project using Maven, Gradle, start.ktor.io and the IntelliJ Plugin.
The plugin allows you to create a Ktor project as well as start.ktor.io, but with the additional convenience of being fully integrated in the IDE.
1) In a first step, you can configure the project to generate and select features to install:
2) In a second step, you can configure the project artifacts:
And that’s it. A new project will be created and opened inside your IDE.
Hello World
A simple hello world in Ktor looks like this:
- Here you define a plain callable main method.
- Then you create an embedded server using Netty as the back-end that will listen on port 8080.
- Installs the routing feature with a block where you can define routes for specific paths and HTTP methods.
- Actual routes: In this case, it will handle a GET request for the path
/demo
, and will reply with aHELLO WORLD!
message. - Actually start the server and wait for connections.
Main.kt
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, port = 8080) {
routing {
get("/") {
call.respondText("Hello World!", ContentType.Text.Plain)
}
get("/demo") {
call.respondText("HELLO WORLD!")
}
}
}
server.start(wait = true)
}
Accessing your application
Since you have a main method, you can execute it with your IDE. That will open a HTTP server,listening on http://127.0.0.1:8080, You can try opening it with your favorite web browser.
If that doesn’t work, maybe your computer is using that port already. You can try changing theport 8080 (in line 10) and adjust it as needed.
At this point you should have a very simple Web Back-end running, so you can make changes,and see the results in your browser.
Since you have configured a Gradle project with the application plugin and the mainClassName
,you can also run it from a terminal using ./gradlew run
on Linux/Mac, or gradlew run
on a Windows machine.
Walkthroughs
- Creating Your First Application
- Generate a Ktor project
- Migrating 0.9.2 → 1.0.0-beta-3
- Migrating 1.0.0-beta-3 → 1.0.0
- Migrating 1.0.1 → 1.1.1
- Migrating 1.1.1 → 1.1.2
- Migrating 1.1.2 → 1.1.3
- Migrating 1.1.3 → 1.1.4
- Migrating 1.1.5 → 1.2.0
- Migrating 1.2.2 → 1.2.3
- Migrating 1.2.3 → 1.2.4
- Migrating 1.2.4 → 1.3.0
- API Documentation
- Guides: How to create a plain website using ktor
- Guides: How to create an API using ktor
- Guides
- Guides: How to implement a chat with WebSockets
- Code Style
- Guides: How to implement an OAuth login with Google
- Guides: How to get a free certificate and use SSL with Ktor
- List of Artifacts
- Coroutines
- Creating Docker Container
- Setting up a Gradle Build
- Setting up Project in IntelliJ IDEA
- Setting up a Maven Build
- Migration
- Frequently Asked Questions