Skip to content

Client-side typesafe Spine schema usage

Call a Ktor API described with type-safety in common code.

Spine allows us to declare Ktor endpoints in code shared between the client and server.

Calling a fullstack endpoint

First, create a module that will contain the endpoint definition (see the api module), with the following API:

object Api : RootResource("v1") {
    object Users : StaticResource<Api>("users", Api) {
        val create by post()
            .request<CreateUserRequest>()
            .response<User>()
    }
}

Create a new module, with a dependency on dev.opensavvy.spine:client. Instantiate a Ktor client by following our tutorial. You can now call the endpoint like this:

val user = client.request(Api / Users / Users.create, CreateUserRequest(name = "John"))
    .bodyOrThrow()

Learn more

  • request calls an endpoint declared in shared code.

  • SpineResponse encapsulates multiple strategies for facing failures.

Packages

opensavvy.spine.client