kmp-ktor

Configure Ktor HttpClient for Kotlin Multiplatform and Android networking with auth, serialization, and testing.

Updated Jul 1, 2026
One-click install
npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kmp-ktor-w0lzard
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kmp-ktor
Source: https://github.com/w0lzard/Wolzard-s-Marketplace/tree/main/plugins/personal-skills/skills/kmp-ktor
Command: npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kmp-ktor-w0lzard

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Setting up HTTP networking in Kotlin Multiplatform projects requires juggling per-platform engines, serialization config, token refresh logic, and testable architecture — and small misconfigurations (like missing encodeDefaults or mixed expectSuccess models) cause hours of debugging. This Skill provides a complete, consistent Ktor client setup that works across Android, iOS, Desktop, and Web. ## Core Features & Use Cases - Per-platform engine wiring: Select OkHttp, Darwin, CIO, or JS engines per source set with version catalog and Gradle dependency snippets. - Bearer auth with automatic refresh: Configure the Ktor Auth plugin with loadTokens, refreshTokens, and markAsRefreshTokenRequest() to handle 401s without infinite loops. - Repository-layer error mapping: Map ClientRequestException, ServerResponseException, and timeouts to domain DataError types, or use the sealed ApiResult<T> + safeRequest pattern for richer error surfaces. - MockEngine testing: Inject HttpClientEngine so tests reuse the production client factory with MockEngine for deterministic network tests. - Use Case: You're building a KMP app targeting Android and iOS and need a shared networking layer with token-based auth, JSON serialization, and unit tests — this Skill gives you the full client factory, service layer, DI setup (Koin or Hilt), and test patterns. ## Quick Start Set up a Ktor HttpClient for my KMP project with bearer token authentication, kotlinx.serialization, and a MockEngine test for the user repository.

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. Provide the engine via expect/actual or DI and create one shared HttpClient instance.

How do I refresh bearer tokens automatically with Ktor?

Install Ktor's Auth plugin with the bearer block, implementing loadTokens to read cached tokens and refreshTokens to call your refresh endpoint. Call markAsRefreshTokenRequest() inside refreshTokens so the refresh call itself skips the Auth plugin and avoids infinite loops.

Ktor vs Retrofit for Android networking — which should I use?

Retrofit is Android-only and mature for JVM projects, while Ktor client runs on Android, iOS, Desktop, and Web from shared Kotlin Multiplatform code. Choose Ktor when sharing networking across platforms; choose Retrofit for Android-only codebases with existing Retrofit investment.

Why are my serialized JSON fields missing in Ktor requests?

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

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

Inject HttpClientEngine into your client factory and pass MockEngine in tests, returning canned responses per request path. Reuse the production createHttpClient factory so plugin configuration like ContentNegotiation and expectSuccess matches production behavior.

Should I use expectSuccess true or false in Ktor?

Use expectSuccess = true with try/catch at the repository boundary when mapping exceptions to domain errors, or false with manual status inspection when using a sealed ApiResult wrapper. Pick one model per project — mixing both creates unreachable branches that hide bugs.