GuidesBackends

Add a Kotlin backend

Add Kotlin routes with the @primate/kotlin module. Primate compiles your Kotlin/Wasm routes and loads them alongside other backend routes.

Use JDK 25 or later with current compatible Gradle and Kotlin releases. The Kotlin SDK run.primate:kotlin is published on Maven Central.

1) Install

Add the Primate adapter to your app and generate a Gradle wrapper. You need an installed Gradle once to generate the wrapper; subsequent builds use ./gradlew. Add Maven Central as the SDK repository.

$ npm install @primate/kotlin
gradle wrapper

2) Configure Gradle

Set up a Kotlin Multiplatform wasmJs executable. Include both routes and Primate's generated .primate-kotlin/src directory as Kotlin sources. Add the published Kotlin SDK as a dependency.

build.gradle.ktssettings.gradle.kts
@file:OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class)

plugins {
  kotlin("multiplatform") version "2.4.21"
}

layout.buildDirectory = file(".primate/kotlin-build")

kotlin {
  wasmJs {
    nodejs()
    binaries.executable()
  }

  sourceSets.wasmJsMain {
    kotlin.srcDir("routes")
    kotlin.srcDir(".primate-kotlin/src")
    dependencies {
      implementation("run.primate:kotlin:0.7.0")
      implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.11.0")
    }
  }
}

3) Configure Primate

Load the adapter in Primate's app configuration.

config/app.ts
import kotlin from "@primate/kotlin";
import app from "primate/app";

export default app({ modules: [kotlin()] });

4) Write a route

Declare a unique package and a main function that registers the route. routes/hello/Index.kt serves GET /hello.

Run npx primate dev from the app directory to start development, then visit /hello. Primate uses the application's Gradle wrapper and manages development compilation automatically.

For requests, responses, and sessions, see the Kotlin backend reference, backend languages and Response API.

routes/hello/Index.kt
package routes.hello

import run.primate.Route

fun main() {
  Route.get { "Hello from Kotlin" }
}