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.$ npm install @primate/kotlin
gradle wrapper
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")
}
}
}pluginManagement {
repositories {
gradlePluginPortal()
mavenCentral()
}
}
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
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" }
}