kmp-network-layer

Scaffolds a Ktor 3 HTTP client layer with bearer auth and typed error handling for Kotlin Multiplatform projects.

2|Updated Jun 6, 2026
One-click install
npx skills add https://github.com/ronjunevaldoz/kmp-agent-skills --skill kmp-network-layer-ronjunevaldoz
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kmp-network-layer
Source: https://github.com/ronjunevaldoz/kmp-agent-skills/tree/main/skills/kmp-network-layer
Command: npx skills add https://github.com/ronjunevaldoz/kmp-agent-skills --skill kmp-network-layer-ronjunevaldoz

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Setting up a shared HTTP client in a Kotlin Multiplatform project requires wiring platform engines, authentication, serialization, and error handling consistently across Android, iOS, Desktop, and Web — a repetitive and error-prone task this Skill automates. ## Core Features & Use Cases - Multiplatform Ktor 3 client setup: Generates platform engine factories (OkHttp, Darwin, CIO, Js) for Android, iOS, Desktop (JVM), Web (JS), and WasmJs targets. - Bearer auth with token refresh: Configures Ktor's Auth plugin with race-condition-safe automatic token refresh and a platform-agnostic TokenStorage interface. - Typed error handling: Provides a NetworkResult<T> sealed type and safeRequest extension that map HTTP and network failures to explicit results instead of thrown exceptions. - Use Case: When adding a new feature that calls a REST API in a KMP app, run this Skill to scaffold :core:network with Koin wiring, then call client.safeRequest from repository implementations and test with MockEngine. ## Quick Start Ask the agent to set up the Ktor network layer with bearer auth and NetworkResult error handling in the :core:network module of your KMP project.

Frequently Asked Questions about kmp-network-layer

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

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

Create a shared HttpClient factory in commonMain using an expect/actual platformEngine per target: OkHttp on Android, Darwin on iOS, CIO on Desktop JVM, and Js on Web and WasmJs. Install ContentNegotiation, HttpTimeout, Auth, and Logging plugins, then provide the client as a Koin singleton.

How to handle bearer token refresh with Ktor client?▼

Use Ktor's built-in Auth plugin with the bearer provider. Its loadTokens and refreshTokens blocks read from a TokenStorage interface, and refresh is race-condition-safe so only one refresh fires even when multiple requests fail with 401 simultaneously.

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

Use OkHttp for Android, Darwin for iOS, CIO for Desktop JVM, and the Js engine for both Web JS and WasmJs targets. Each is declared in its platform source set and exposed through a common expect/actual factory.

Does Ktor client support Server-Sent Events on multiplatform?▼

Yes, Ktor ships an official multiplatform SSE client plugin that works across all KMP targets. It supports reconnection configuration and maps incoming events onto Kotlin Flow, but note that the Compression plugin skips SSE responses by default.

When should I use kRPC instead of a Ktor REST client?▼

Use kRPC when both client and server are Kotlin and you control the contract, especially for bidirectional streaming. If the project already has a kRPC transport for a backend, extend that service interface instead of adding a parallel REST client to the same server.

How do I test Ktor client requests without a real server?▼

Use Ktor's MockEngine in commonTest to return canned responses with specific status codes and bodies. This lets you verify the safeRequest contract and NetworkResult mapping for success, HTTP error, and network failure cases without network access.