kmp-ktor

Configure Ktor HttpClient for Kotlin Multiplatform with engines, auth, serialization, and MockEngine testing.

1|Updated Jun 1, 2026
One-click install
npx skills add https://github.com/chmonya-inc/dnd-manager --skill kmp-ktor-chmonya-inc
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kmp-ktor
Source: https://github.com/chmonya-inc/dnd-manager/tree/main/.agents/skills/kmp-ktor
Command: npx skills add https://github.com/chmonya-inc/dnd-manager --skill kmp-ktor-chmonya-inc

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Setting up networking in Kotlin Multiplatform projects requires juggling per-platform HTTP engines, serialization config, token refresh logic, and testable client factories. This Skill provides a complete, correct Ktor client setup so you avoid common pitfalls like leaking HttpClient instances, silently dropped JSON fields, and infinite token-refresh loops. ## Core Features & Use Cases - Per-platform engine wiring: Select OkHttp for Android, Darwin for iOS, CIO for Desktop, and JS for Web, with version catalog and source set configuration. - Bearer auth with automatic refresh: Use the Auth plugin with loadTokens, refreshTokens, and markAsRefreshTokenRequest to handle 401s without manual retry code. - Error mapping at the repository boundary: Convert ClientRequestException, ServerResponseException, and timeouts into domain DataError types so ViewModels never touch Ktor internals. - MockEngine testing: Inject HttpClientEngine so tests reuse the production client factory with mocked responses. - Use Case: You are building a KMP app targeting Android, iOS, and Desktop. Use this Skill to create a single shared HttpClient configuration with Koin DI, typed service classes, and unit tests that verify DTO mapping and 404 error handling. ## Quick Start Set up a Ktor HttpClient for my Kotlin Multiplatform project with bearer token authentication, kotlinx.serialization, and MockEngine tests.

Frequently Asked Questions about kmp-ktor

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up Ktor client in a Kotlin Multiplatform project?▼

Add ktor-client-core, content-negotiation, and kotlinx-json to commonMain, then add a platform engine per source set: OkHttp for Android, Darwin for iOS, CIO for Desktop, and JS for Web. Provide the engine via expect/actual or DI and create one shared HttpClient instance.

How to implement bearer token refresh with Ktor Auth plugin?▼

Install the Auth plugin with bearer config, implement loadTokens to read cached tokens and refreshTokens to call your refresh endpoint. Call markAsRefreshTokenRequest() inside refreshTokens so the refresh call skips the Auth plugin and avoids infinite refresh loops.

Which Ktor client engine should I use for each platform?▼

Use OkHttp on Android, Darwin (NSURLSession) on iOS, CIO or OkHttp on JVM/Desktop, and the JS engine for Web/Wasm targets. For tests on any platform, use MockEngine from ktor-client-mock.

Why are my Kotlin data class fields missing from serialized JSON?▼

kotlinx.serialization defaults to encodeDefaults = false, which omits any property equal to its declared default value. Set encodeDefaults = true in your Json configuration so protocol-required fields like jsonrpc or version are included in the payload.

How do I test Ktor HttpClient without a real server?▼

Inject HttpClientEngine into your client factory and pass MockEngine in tests. MockEngine lets you assert request paths and return canned responses with specific status codes, while reusing the production factory keeps plugin configuration identical.

Should ViewModels catch Ktor exceptions directly?▼

No. Catch ClientRequestException, ServerResponseException, HttpRequestTimeoutException, and IOException in the repository layer and map them to domain error types. This keeps ViewModels decoupled from networking internals so engines or HTTP clients can be swapped without UI changes.