Content Delivery API Client
A Kotlin Multiplatform client for Storyblok's Content Delivery API built on the Ktor Client Plugin.
With automatic relation resolution and custom component deserialization.
Quick start
First, add the dependency to your project:
dependencies {
implementation("com.storyblok:content-api-client:0.6.0")
}Content copied to clipboard
Then create a client and fetch a story:
val client = StoryblokClient(
accessToken = "YOUR_ACCESS_TOKEN",
version = Draft
)
// client.story(...) returns a Flow that emits up-to two values:
// - the cached version (if available)
// - the latest version from the API (if different from the cached version)
val myStory = client.story("home").first()Content copied to clipboard
Define custom components for deserialization:
Use
@SerialNameon the class to set the component's technical name.Use
@JsonNameson a field whose name in Storyblok differs from the Kotlin property
@Serializable
@SerialName("page")
class Page(
val title: String,
@JsonNames("hero_image")
val heroImage: Asset,
val body: List<Component>
) : Component()
val client = StoryblokClient(
accessToken = "YOUR_ACCESS_TOKEN",
version = Draft,
serializersModule = SerializersModule {
polymorphic(Component::class, Page::class, Page.serializer())
}
)
client.story<Page>("home")
.collect { story -> println(story.content.title) }Content copied to clipboard
Fetch multiple stories:
client.stories(...) returns a Pager, whose flow emits pages as they are needed. The content_type parameter comes from the component the query is typed to, and the query DSL narrows, sorts and filters the result:
val articles = client.stories<Page> {
startsWith = "articles/"
sortByDescending(Story<*>::publishedAt)
filter { Page::title like "*space*" }
}Content copied to clipboard
Other resources
You can find the full guide to the Content Delivery API Client inside README.md.