Ktorgen is a 100% compile-time code generator for creating HTTP clients using Ktor Client and interface annotations, inspired by Retrofit and Ktorfit.
🔹 No runtime dependencies — you only need the annotations and the compiler in your build.
🔹 100% compatible with Kotlin, Kotlin Multiplatform, KSP 2, and Ktor Client.
🔹 100% Ktor configuration — no unnecessary overhead or wrappers added.
🔹 Annotation retention: SOURCE, BINARY (only for RequiresOptIn).
🔹 Generated code annotated with @Generated and @Suppress to avoid warnings and exclude of analysis tools.
🔹 Support suspend fun, Coroutines Flow and Result out of the box.
🔹 Optional type-safe headers, using Headers.ContentTypes or Headers.Companion.*
🔹 Optional inheritance between interfaces (use delegation to implement).
🔹 Experimental support for vararg parameters and Pair type.
📚 See more in documentation page.
Install KSP plugin
Install Ktor Client Core and an Engine
- Version Catalog
[versions]
ktorgen = "<current-version>"
[libraries]
ktorgen-annotations = { group = "io.github.kingg22", name = "ktorgen-annotations", version.ref = "ktorgen" }
ktorgen-compiler = { group = "io.github.kingg22", name = "ktorgen-compiler", version.ref = "ktorgen" }- Gradle for Kotlin JVM (KMP projects see below)
dependencies {
implementation("io.github.kingg22:ktorgen-annotations:<current-version>")
ksp("io.github.kingg22:ktorgen-compiler:<current-version>")
}| Feature | Retrofit | Ktorfit | ktorgen 🚀 |
|---|---|---|---|
| Based on Ktor Client | ❌ | ✅ | ✅ |
| Runtime dependencies | ✅ (Reflection, converters, adapters) | ✅ (Converts) | ❌ |
| Retrofit-like annotations | ✅ (Runtime retention) | ✅ + |
✅ with smart use that reduces unnecessary annotations |
| Type-safe headers | ❌ | ❌ | ✅ with Headers.ContentTypes and Headers.Companion.* |
| Cookie support | ✅ with @Cookie |
||
Using an @Part without @Multipart |
❌ | ❌ | ✅ (smart use, your intended use is understood) |
| Customizing the generated code | Limited | Limited | ✅ Using options in @KtorGen and @KtorGenFunction |
| Synchronous request | ✅ | ✅ | ❌ Ktor Client don't offer synchronous request, only for Flow |
BREAKING CHANGES:
-
Since PR #1, Header annotations have been different:
Before this PR, the API of annotation is "identical" to Retrofit annotations.
Before on functions
@Headers("Content-Type: application/json")after@Header("Content-Type", "application/json")Before on parameters
@Header("Content-Type") param: Stringafter@HeaderParam("Content-Type") param: String
Migrating is as simple as:
Changing annotation imports to io.github.kingg22.ktorgen.http.*
Comma-separated header annotations are now repeatable and type-safe.
Before:
@Headers("Content-Type: application/json", "Accept: application/json")
suspend fun request(@Header("Authentication") token: String): StringAfter:
@Header("Content-Type", "application/json")
@Header("Accept", "application/json")
suspend fun request(@HeaderParam("Authentication") token: String): StringPassing your own Ktor HttpClient to the implementations, like this: fun UserRoute(client) for interface UserRoute and generated class _UserRouteImpl.
Real-life migration example: deezer-client-kt
- Add a matrix compatibility test on CI (Kotlin versions, KSP versions, Ktor Client versions) to know the range of compatibility.
- Improve support for the default behavior of KSP in KMP projects.
- Add better sample projects.
- Improve the internal state of the processor to avoid unnecessary state-sharing between rounds. Includes: logging, tracing, validation, deferred symbols, etc.
- Add more options to customize the generated code. Includes: generated annotations, custom KDoc, etc.
- Add more options to the behavior of the processor. Includes: fail-fast, no-processing, no-tracing, etc.
- Add performance benchmarks.
-
Add test for Fragment annotation -
Add test for Cookie annotation - Resolve knowable issues described in #27
- Resolve issues related to unresolved references,
multi-round processing, unexpected errors.EDIT: Most unresolved symbols are caused by google/ksp#2668 and platform-specific issues caused by Optional expectation of some annotations.
See more in GitHub Page
Open to contributions 🚀
First usage needs to open in samples folder, the IDE will sync and compile automatically all.
This repository is a fork of Ktorfit and Retrofit annotations, with my own changes and additions. It is not affiliated with Ktor, JetBrains, Kotlin, Ktorfit, or Retrofit. Credits to their respective authors.
License: Apache 2.0, same as Retrofit and Ktorfit.