Server-Side Kotlin with Ktor

May 21 2019 · Video Course (2 hrs, 31 mins) · Intermediate

Learn how to use the Ktor framework to build a Kotlin web app and API, and see how to deploy the app to the cloud and also run the app in a Docker container.

4.9/5 7 Ratings


  • Kotlin 1.3, macOS 10.14, IntelliJ IDEA 2019.1

Getting Started with Ktor


5:35 Free
Let's introduce Server-Side Kotlin and Ktor, discuss our sample project, and review all that you'll be learning in the course.

Hello, Ktor:

8:03 Free
You'll create a Ktor app from scratch in IntelliJ IDEA and add simple Hello World routes to the app.

App Routes

1:57 Free
In order to keep your code organized, you'll refactor the routes you've written so far so that each route has its own source file.
Test your knowledge about building app routes in Ktor by adding an about route to the web app, then see a solution.
Add a repository interface and in-memory repository implementation in order to save model EmojiPhrases in between requests to the app.

API Post Endpoint

1:06 Free
Create a POST endpoint to which you can send new EmojiPhrases, then test the endpoint using curl and a REST client app.

Get Route

2:00 Free
Add a GET route to the web app that will let you retrieve and see EmojiPhrases that have been add to the repository.
Introduce FreeMarker templating into the app and create a simple HTML page for showing your EmojiPhrases.


Review all that you've done in Part 1 of the course, and take a look ahead at where we'll be going in Part 2 and beyond.

Beautification and Type-Safe Routing

See what we'll be covering in Part 2: adding basic authentication, using Bootstrap components for styling, and adding type-safe routing.
Install the Ktor Authentication feature into your app and add Basic Authentication to the POST route and GET route for phrases.
Build an HTML form to let users post new EmojiPhrases directly from the app instead of using other REST clients.
Get an introduction to the Bootstrap open-source front-end framework, and review the Bootstrap code that has been added to the app FreeMarker templates.
Setup static content in your Ktor web app, and complete the process of adding Bootstrap to the Home and Phrases pages of the app.
Test your knowledge of FreeMarker and the use of Bootstrap by completing the EmojiPhrases app About page, then see a solution.
Install the Ktor Locations feature into the app, then use the feature to switch to using type-safe routing.


Review all that you've done in Part 2 of the course, and take a look ahead at where we'll be going in Part 3 and beyond.

Persistence and Sessions

See what we'll be covering in Part 3 of the course, adding true data persistence using Exposed and PostgreSQL and building in real authentication via sessions.
Add the Exposed SQL library framework from JetBrains into the app, and use it with an in-memory H2 database and with database connection pooling.


Switch from using H2 with Exposed to instead using PostgreSQL, adding true data persistence so that data is saved between different runs of the app.
Learn about user authentication via cookies and sessions, understand the purpose of password hashing, and begin to add user sessions into the course app.
Continue adding sessions into the course app by creating the code for the sign up and sign in pages of the app.
Finish adding sessions into the app by checking for active sessions on the different pages of the app, and verifying a session when users post new data.


Review all that we've done in Part 3 of the course and take a look at where we'll be going in Parts 4 and 5.

JWT Auth and the API

See what we'll be covering in Part 4 of the course, adding JSON Web Tokens for API authentication and completing a phrases API for the app.
Learn about JSON Web Tokens and how they are used, and then add a login endpoint from which API users can obtain a JWT.
Create a GET endpoint in the API from which API users can request the EmojiPhrases stored in the app repository.
Create a POST endpoint in the API to which API users can send new EmojiPhrases, and test adding data in the Postman REST client.


Review all that we've done in Part 4 of the course and take a look at where we'll be going in Part 5.


See what we'll be covering in Part 5 of the course, deploying our Ktor app to Heroku, creating a Docker image, and running the Docker image in a container.
See how to deploy your Ktor app to Heroku, connect Heroku to GitHub, and setup a PostgreSQL database on Heroku for your app.
Add some build flags to support Dockerizing your Ktor app, then write a Dockerfile and use it to build a Docker image of the app.
Run your Ktor app Docker image in a container, including connecting the app container to a Docker container running PostgreSQL.


Review all that you've done to build a deployable Ktor web app and API, both backed by a PostgreSQL database, and take a look at where to go from here.

Who is this for?

This course is for Kotlin and Android developers interested in learning how to use Kotlin on the server to build a web app and a web API. It expects you to be familiar with the Kotlin language and either Android Studio or IntelliJ IDEA. You'll also need basic knowledge of how HTTP works and have some experience with use of the command-line.

Covered concepts

  • Creating a Ktor web app
  • Building a REST API with Ktor
  • Type-safe routing in a Ktor app
  • HTML Templating with FreeMarker
  • Using Bootstrap with FreeMarker
  • The Exposed SQL Library
  • Using PostgreSQL with Ktor
  • Session and Cookie Authentication
  • JSON Web Token Authentication
  • Deploying a Ktor app to Heroku
  • Dockerizing a Ktor app