Defining request models
Request models define API endpoints and can have the following properties:
- Path variable in the URL path
- Query parameter in the URL query string
- Body parameter
Requirements:
- Must be annotated with
@Serializableand, by default, placed under<api-package>.models(scan packages can be customized via the@GenerateApiannotation) - Must have exactly one HTTP method annotation (
@GET,@POST,@PUT,@PATCH,@DELETE) - Must have a
@Returnsannotation specifying the response type - Class name must end with the
Requestsuffix (e.g.,GetUserRequest,CreateOrderRequest) - If the class has properties, it must be a
data class - Classes without properties (marker classes) must be
objectdeclarations - Cannot be
abstract,sealed, orinner
Basic request model example
import dev.kolibrium.api.ksp.annotations.GET
import dev.kolibrium.api.ksp.annotations.Returns
import kotlinx.serialization.Serializable
@GET("/users")
@Returns(UsersResponse::class)
@Serializable
object ListUsersRequest
Path parameters
Path variables are defined using curly braces {variableName} and substituted into the URL path.
@GET("/users/{id}/articles/{articleId}")
@Returns(Article::class)
@Serializable
data class GetUserArticleRequest(
@Path @Transient val id: Int = 0,
@Path @Transient val articleId: Int = 0
)
Requirements:
- Must be annotated with both
@Pathand@Transient - Must be one of:
String,Int,Long,Short,Float,Double,Boolean - Must have a matching path variable in the URL
Query parameters
Query parameters are appended to the URL as query strings.
@GET("/users")
@Returns(UserList::class)
@Serializable
data class ListUsersRequest(
@Query @Transient val page: Int? = null,
@Query @Transient val limit: Int? = null,
@Query @Transient val search: String? = null
)
Requirements:
- Must be annotated with both
@Queryand@Transient - Must be nullable (query parameters are optional)
- Only allowed on
GETandDELETErequests - Must be one of:
String?,Int?,Long?,Boolean?,Short?,Float?,Double?, orList<T>?where T is one of these types
Header parameters
Header parameters are sent as HTTP headers in the request. Unlike query parameters, headers are allowed on any HTTP method.
@GET("/users")
@Returns(UserList::class)
@Serializable
data class ListUsersRequest(
@Header(name = "X-Correlation-ID") @Transient val correlationId: String? = null,
@Header @Transient val accept: String? = null
)
By default, the property name is used as the HTTP header name. Use the name parameter to specify a different header name — this is useful when the HTTP header name contains characters that aren't valid Kotlin identifiers (e.g., X-Correlation-ID).
A null value means the header is not sent.
Requirements:
- Must be annotated with both
@Headerand@Transient - Must be nullable
- Must be one of:
String?,Int?,Long?,Short?,Float?,Double?,Boolean? - Cannot be combined with
@Pathor@Query - Header name must be a valid HTTP header name per RFC 7230
Body parameters
Body parameters are serialized as the request body (JSON).
@POST("/users")
@Returns(User::class)
@Serializable
data class CreateUserRequest(
var name: String? = null,
var email: String? = null,
var age: Int? = null
)
Requirements:
- Only allowed on
POST,PUT, andPATCHrequests - Should be
varto support the DSL builder pattern - Must be nullable or have a default value
- Not annotated with
@Path,@Query, or@Header