# Deploying your Ktor app to Railway

I recently had to deploy a Ktor project to [Railway](https://railway.app/) for my hobby project. I'll share the how-to through this article.

### Introduction

Ktor is a framework that allows us to build web applications in Kotlin, built and backed by Jetbrains. It's open-source, lightweight, and flexible, making it an excellent choice for building web applications in the JVM ecosystem.

### Setting up the project

We'll be deploying a project that is generated for us by Ktor plugin, it is a simple `Hello World!` app. You can check it out [live here](https://ktor-demo-production.up.railway.app/) or check [GitHub repo here](https://github.com/imabhishekkumar/ktor-demo).

I've generated the project using the Ktor plugin but you can also generate it via [Ktor Project Generator](https://start.ktor.io/).

1. Configure your project as required. Here I've added name and website, you can leave it as default. I am using <mark>Gradle Kotlin</mark> as the build system.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674539304044/1ab7e05c-3058-41b7-9e28-f26ed2d8e05b.jpeg align="center")

1. Add `Routing` plugin for handling incoming requests.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674539316232/4ffe25bb-9759-48c0-9ccf-78d90f961e09.jpeg align="center")

1. Once the project is created, you can run the app and should get this in the terminal -
    

`INFO ktor.application - Responding at` [`http://127.0.0.1:8080`](http://127.0.0.1:8080)

When you open the link it will open the browser and say `Hello World!`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674539569525/abf2f1ae-845e-46ea-8612-98724bef998b.jpeg align="center")

I've added the `Application.kt` and `Routing.kt` file for reference below -

`Application.kt -`

```kotlin
package abhishekkumar.me

import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import abhishekkumar.me.plugins.*

fun main() {
    embeddedServer(Netty, port = 8080, host = "0.0.0.0", module = Application::module)
        .start(wait = true)
}

fun Application.module() {
    configureRouting()
}
```

<mark>Note:</mark> By default the Port is `8080` and host is `0.0.0.0`, you can change it in `Application.kt` file.

`Routing.kt -`

```kotlin
package abhishekkumar.me.plugins

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

fun Application.configureRouting() {
    routing {
        get("/") {
            call.respondText("Hello World!")
        }
    }
}
```

1. I'll be deploying this app from Github, so I've created a repository and pushed my code ([link](https://github.com/imabhishekkumar/ktor-demo)).
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743602069/381b5304-952e-491a-8292-84402902fe6d.jpeg align="center")

All done from here, now let's head over to [Railway dashboard](https://railway.app/dashboard).

1. Select `New Project` and `Choose Deploy from Github repo`
    
2. Select your GitHub repo, I have selected my repository `imabhishekkumar/ktor-demo`
    
3. Click on `Add variables`
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674745747581/acc17006-1986-4a09-bb24-9351b437eb95.png align="center")

We will add `PORT` variable that we have configured in the project. By default it's `8080`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743621282/2850bc48-26a3-4ef4-a0de-cfefa9dccf83.jpeg align="center")

In the `Settings` tab, we'll add the build command and deploy command for Ktor as per the documentation [here](https://ktor.io/docs/fatjar.html#build)

Build - `gradle clean buildFatJar`

Deploy- `gradle runFatJar`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743643966/f9ed9356-59eb-41b4-8ea0-0939b4af9ffe.jpeg align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743667764/a654a4b4-1bdb-4921-9295-28e568445930.jpeg align="center")

Once that is done, we want to add a domain to make our service available to public internet. Railyway provides free domain for us. In the `Settings` tab, click on `Generate Domain` or use your custom domain.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743679080/0497df3d-6db1-4437-9fe6-1d37063d621c.jpeg align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1674743837797/91f024d7-7254-4b48-a55d-45278a705ee8.jpeg align="center")

Once that is done, our project is hosted and accessible via the domain.

Here's my dummy project - [ktor-demo-production.up.railway.app](http://ktor-demo-production.up.railway.app)

---

I hope this article was helpful. If you have any questions, feel free to reach out!

Follow for more articles about Ktor (as I explore) and Mobile development.

Connect with me -

[**Github**](https://github.com/imabhishekkumar) | [**LinkedIn**](https://www.linkedin.com/in/abhishekkumar01/) | [**Twitter**](https://twitter.com/_abhishekkumarr)
