
Generates HTTP client code at compile-time using annotations, inspired by Retrofit and Ktorfit. Features include no runtime dependencies, type-safe headers, and advanced customization options.
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
[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" }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: String after @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
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.
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
[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" }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: String after @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
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.