Skip to content
Back to skills

Kmp Api Endpoint

ASecurity

Use when adding, wiring or fixing one REST endpoint in a Kotlin Multiplatform shared module — "add a GET/POST/PUT/DELETE call", "integrate this API", "call /orders from shared code", an OTP or token endpoint — covering the Ktor request, @Serializable DTOs, mapper, repository, use case, Koin wiring and MockEngine tests, or when a new endpoint fails with MissingFieldException or JsonDecodingException, returns 404 because of a wrong path, or works on Android but not iOS.

  • 17 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 3, 2026
developmentrustgokotlinbashnodetestinggitapidatabasebackend

Works with

  • cursor
  • cli
  • api

Security analysis

A100/100

Pro scans all 2 files and shows the line behind each finding

Scanned December 3, 2026

npx -y skills add Kushpawan/kmp-mobile-skills --skill kmp-api-endpoint --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Kmp Api Endpoint?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Kmp Api Endpoint
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/kushpawan-kmp-api-endpoint/badge)](https://www.skillsdirectory.com/skills/kushpawan-kmp-api-endpoint)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: kmp-api-endpoint
description: Use when adding, wiring or fixing one REST endpoint in a Kotlin Multiplatform shared module — "add a GET/POST/PUT/DELETE call", "integrate this API", "call /orders from shared code", an OTP or token endpoint — covering the Ktor request, @Serializable DTOs, mapper, repository, use case, Koin wiring and MockEngine tests, or when a new endpoint fails with MissingFieldException or JsonDecodingException, returns 404 because of a wrong path, or works on Android but not iOS.
license: Apache-2.0
compatibility: Kotlin Multiplatform project (Android + iOS) with Ktor client 3.x (3.6 current) and kotlinx.serialization 1.7+ (1.13 current). iOS test runs need macOS with Xcode.
metadata:
  category: data
  platforms: android, ios
---

# Add one REST endpoint to a KMP app

A concrete recipe: one endpoint, from wire contract to tested repository, in the project's own shape.
The architecture behind each step — untrusted DTOs, one shared client, typed errors — is in
**kmp-data-layer**; this skill applies it file by file.

## When to use

- Adding a new GET, POST, PUT, PATCH or DELETE call to shared code.
- Adding a method to an existing API service, repository and use case.
- Fixing a freshly added endpoint: parse failures, wrong URL, missing auth header, untested errors.

## When not to use

- Designing the data layer, HttpClient, auth refresh or result type from scratch — use **kmp-data-layer** first.
- Caching the response in a database or syncing offline writes — **kmp-data-layer** (offline-first) and **kmp-room-database**.
- DI start-up problems (`NoDefinitionFoundException` at launch) — **kmp-koin-di**.

## Inputs — get these before writing code

| Input | Example | If missing |
|---|---|---|
| Method and path | `GET orders/{orderId}` | Ask. |
| Path and query parameters | `orderId: String`, `?expand=items` | Ask which are optional. |
| Request body sample | JSON | Required for POST/PUT/PATCH. |
| Response sample | Real JSON, ideally from the API docs or a recorded call | **Ask. Never invent field names.** |
| Error statuses | 404 unknown id, 409 already cancelled | Use the defaults in the error mapping. |
| Auth | bearer / none | Picks the authenticated or plain client. |
| Pagination | cursor / page+size / none | Shapes the domain return type. |

## Step 0 — Read the project first

1. **Find the closest existing endpoint and copy its shape.**
   ```bash
   grep -rln "client.get\|client.post\|HttpClient" --include=*.kt */src/commonMain | head
   ```
   Open one complete vertical (API → repository → use case → DI → tests). Note package layout, file
   names, the `toDomain()` style, whether use cases exist, and how tests are written.
2. **Detect the building blocks** — reuse each one, never add a parallel version:
   ```bash
   grep -rnE "sealed (class|interface) [A-Za-z]*(Result|Outcome)" --include=*.kt .    # result type
   grep -rnE "suspend fun <T> [A-Za-z]*(Call|Request)" --include=*.kt .               # error-boundary helper
   grep -rn "named(" --include=*.kt . | grep -i client                                 # client qualifiers
   grep -rnE "DispatcherProvider|MockEngine|class Fake" --include=*.kt . | head -20    # dispatchers, test helpers
   ```
3. **Check whether files already exist.** If `OrdersApi` exists, add a method; don't create `OrdersApi2`.
4. **Read the library versions** — they decide which pitfalls in **Version notes** apply:
   ```bash
   grep -nE "ktor|serialization|datetime|coroutines" gradle/libs.versions.toml
   ```

If the project has none of this, set up the data layer with **kmp-data-layer** first. The examples
below use its defaults: `ApiResult` / `DataError`, `safeApiCall`, `DispatcherProvider`, Koin with
`named("authenticated")`, package `com.example.app`.

## Workflow

Example endpoint: `GET orders/{orderId}` → one order with line items.

### 1. Response DTOs — `orders/data/remote/dto/OrderDto.kt`

```kotlin
@Serializable
data class OrderDto(
    @SerialName("id") val id: String? = null,
    @SerialName("status") val status: String? = null,
    @SerialName("total_minor") val totalMinor: Long? = null,
    @SerialName("currency") val currency: String? = null,
    @SerialName("placed_at") val placedAtEpochMillis: Long? = null,
    @SerialName("items") val items: List<OrderItemDto>? = null,
)

@Serializable
data class OrderItemDto(
    @SerialName("sku") val sku: String? = null,
    @SerialName("name") val name: String? = null,
    @SerialName("quantity") val quantity: Int? = null,
)
```

Every response field nullable, `= null`, explicit `@SerialName`. Enums arrive as `String?`.
Request DTOs (for POST/PUT) may be non-null. Done when the DTO round-trips the sample JSON in a test.

- **Timestamps and ids.** An ISO-8601 string can decode straight into `kotlin.time.Instant?` and a
  UUID string into `kotlin.uuid.Uuid?` — both serializers are stable since kotlinx.serialization
  1.12.0 — but one malformed value then fails the whole response. Unless the API guarantees the
  format, keep `String?` and parse in the mapper. Epoch numbers stay `Long?` (as `placed_at` here).
  Never add the deprecated `kotlinx.datetime.Instant`.
- **No validation in DTOs** (`init { require(...) }`, custom serializers that throw): the exception
  class it produces differs between kotlinx.serialization 1.12.x and 1.13.0. Validate in the mapper.

### 2. Domain model — `orders/domain/Order.kt`

```kotlin
data class Order(
    val id: String,
    val status: OrderStatus,
    val total: Money,
    val placedAtEpochMillis: Long,
    val items: List<OrderItem>,
)

data class Money(val amountMinor: Long, val currency: String)
data class OrderItem(val sku: String, val name: String, val quantity: Int)
enum class OrderStatus { Pending, Shipped, Delivered, Cancelled, Unknown }
```

Plain, non-null, no annotations. Money as minor units plus currency, never `Double`.

### 3. Mapper — `orders/data/OrderMappers.kt`

```kotlin
internal fun OrderDto.toDomainOrNull(): Order? {
    val id = id?.takeIf { it.isNotBlank() } ?: return null
    val amount = totalMinor ?: return null            // never default money to 0
    val currency = currency?.takeIf { it.length == 3 } ?: return null
    return Order(
        id = id,
        status = status.toOrderStatus(),
        total = Money(amountMinor = amount, currency = currency.uppercase()),
        placedAtEpochMillis = placedAtEpochMillis ?: 0L,
        items = items.orEmpty().mapNotNull { it.toDomainOrNull() },
    )
}

internal fun OrderItemDto.toDomainOrNull(): OrderItem? {
    val sku = sku?.takeIf { it.isNotBlank() } ?: return null
    val quantity = quantity?.takeIf { it > 0 } ?: return null
    return OrderItem(sku = sku, name = name.orEmpty(), quantity = quantity)
}

private fun String?.toOrderStatus(): OrderStatus = when (this?.lowercase()) {
    "pending" -> OrderStatus.Pending
    "shipped" -> OrderStatus.Shipped
    "delivered" -> OrderStatus.Delivered
    "cancelled" -> OrderStatus.Cancelled
    else -> OrderStatus.Unknown                       // new server values must not crash old apps
}
```

List each field as **required** (drop or fail) or **cosmetic** (fall back). Each rule gets a test.

### 4. API service — `orders/data/remote/OrdersApi.kt`

```kotlin
interface OrdersApi {
    suspend fun getOrder(orderId: String): OrderDto
}

internal class KtorOrdersApi(private val client: HttpClient) : OrdersApi {
    override suspend fun getOrder(orderId: String): OrderDto =
        client.get("orders/${orderId.encodeURLPathPart()}").body()
}
```

- Relative path, no leading `/` — it resolves against the client's base URL.
- **Encode path parameters.** `encodeURLPathPart()` (from `io.ktor.http`) escapes `/`, `?` and `#`;
  raw interpolation lets an id like `../admin` or `a?b=c` change the request.
- No `try`/`catch`, no mapping, no headers the shared client already adds.

### 5. Repository — `orders/domain/OrdersRepository.kt` + `orders/data/OrdersRepositoryImpl.kt`

```kotlin
interface OrdersRepository {
    suspend fun getOrder(orderId: String): ApiResult<Order>
}

class OrdersRepositoryImpl(
    private val ordersApi: OrdersApi,
    private val dispatchers: DispatcherProvider,
) : OrdersRepository {

    override suspend fun getOrder(orderId: String): ApiResult<Order> =
        safeApiCall(dispatchers.default) {
            ordersApi.getOrder(orderId).toDomainOrNull()
                ?: throw SerializationException("Order response is missing required fields")
        }
}
```

The error boundary maps HTTP status, transport and parse failures to `DataError` and rethrows
cancellation. An order missing required fields becomes `DataError.Serialization`, not a crash and
not a half-empty screen.

### 6. Use case — only if the project uses them

```kotlin
// orders/domain/GetOrderUseCase.kt
class GetOrderUseCase(private val ordersRepository: OrdersRepository) {
    suspend operator fun invoke(orderId: String): ApiResult<Order> {
        if (orderId.isBlank()) return ApiResult.Failure(DataError.Validation(serverMessage = null))
        return ordersRepository.getOrder(orderId)
    }
}
```

If the project's ViewModels call repositories directly, skip this file — a pass-through use case
adds a layer and tests nothing.

### 7. DI — `orders/di/OrdersModule.kt`

```kotlin
val ordersModule = module {
    single<OrdersApi> { KtorOrdersApi(client = get(named("authenticated"))) }
    single<OrdersRepository> { OrdersRepositoryImpl(ordersApi = get(), dispatchers = get()) }
    factoryOf(::GetOrderUseCase)
}
```

Add `ordersModule` to the app's module list (or the feature aggregator's `includes(...)`). Use the
plain client for endpoints that must not carry a token. Done when the DI verification test passes
(see **kmp-koin-di**) or the app starts and resolves the screen's ViewModel.

### 8. Tests — `commonTest/.../orders/`

Write these four, using the templates in [references/test-templates.md](references/test-templates.md):

| File | Covers |
|---|---|
| `OrderMapperTest` | every required-field drop, every fallback, unknown enum value |
| `KtorOrdersApiTest` | method, encoded path, query, headers, body; parses the sample JSON through the real client |
| `OrdersRepositoryImplTest` | success; 404 → `NotFound`; 401 → `Unauthorized`; malformed body → `Serialization`; offline → `NoConnection` |
| `FakeOrdersRepository` | a fake for ViewModel and use-case tests (not a test itself) |

Name tests `should_<expected>_when_<condition>` unless the project uses another convention.

### 9. Run the checks in **Verify**.

## Variations

| Need | Do this |
|---|---|
| Query parameters | `client.get("orders") { parameter("status", status) }` — a `null` value is skipped |
| Repeated query key | `client.get("orders") { url { parameters.appendAll("tag", tags) } }` |
| POST with JSON body | `client.post("orders") { contentType(ContentType.Application.Json); setBody(CreateOrderRequest(...)) }` |
| PATCH (partial update) | Request DTO with only the changed fields. With `explicitNulls = false` a `null` is **omitted**, so "clear this field" needs an explicit design (a `JsonObject` body or a dedicated flag) |
| 204 No Content | `client.delete("orders/${id.encodeURLPathPart()}")` and return `Unit`; do not call `.body()` |
| Pagination | DTO carries `next_cursor`; domain returns `Page<Order>(items, nextCursor)`; the ViewModel owns "load more" |
| Multipart upload | `client.submitFormWithBinaryData(url = "uploads", formData = formData { append("file", bytes, Headers.build { append(HttpHeaders.ContentDisposition, "filename=\"receipt.jpg\"") }) }) { timeout { requestTimeoutMillis = 120_000 } }` |
| Pre-signed upload URL (third-party host) | Absolute URL through a client **without** the bearer provider, or a bearer config whose `sendWithoutRequest` excludes that host |
| Retry-safe create | If the backend supports it, send a client-generated idempotency key header so a retried POST does not create a duplicate |
| Response wrapped in an envelope | `{"data": {...}}` → an `EnvelopeDto<T>(@SerialName("data") val data: T? = null)`; unwrap in the mapper |
| Token endpoint needing its own `Authorization` (Basic client credentials) | Call it through the plain client. With `markAsRefreshTokenRequest()` on the authenticated client, Ktor before 3.6.0 strips that header (see **Version notes**) |
| SMS OTP send / verify | The endpoints are ordinary POSTs; reading the code is platform UI. See **OTP endpoints and Android 17** |

## OTP endpoints and Android 17

Android 17 delays SMS one-time codes by three hours for apps that are not their intended reader:
standard-format OTP SMS for most apps targeting API 37, and WebOTP- or SMS Retriever-format
messages for every app that is not the verified recipient. During the delay the
`SMS_RECEIVED_ACTION` broadcast is withheld and SMS provider queries are filtered. An app that
reads the inbox (`READ_SMS` / `RECEIVE_SMS`) to auto-fill the code gets it three hours late.

- **Android:** use the **SMS Retriever API** (the backend appends the app's hash to the message; no
  SMS permission) or the **SMS User Consent API** (one-tap consent, no hash). Both are `androidMain`
  code behind an interface the shared ViewModel calls — see **kmp-expect-actual**. The shared
  send/verify endpoints do not change, except that SMS Retriever needs the backend to format the
  message with the hash.
- **iOS:** a native text field with `textContentType = .oneTimeCode` gets the system's AutoFill
  suggestion; check that your Compose Multiplatform text field exposes it before relying on it.
- **SMS-less verification on Android:** Google's
  [phone-number-verification](https://github.com/android/skills/tree/main/identity/digital-credentials/phone-number-verification)
  skill (Credential Manager with carrier TS.43 tokens) covers the client and the backend exchange.
- Never send the OTP or phone number to logs; the verify request and response stay out of `BODY`
  logging in every build.

## Version notes

Fixes that change what an endpoint needs (Ktor and kotlinx.serialization changelogs, December 2026).
On an older version, apply the workaround; upgrade only when asked.

| Library and issue | Fixed in | Symptom on older versions | If you stay on it |
|---|---|---|---|
| Ktor [KTOR-9827](https://youtrack.jetbrains.com/issue/KTOR-9827) | 3.6.0 | An `Authorization` header you set on a request marked `markAsRefreshTokenRequest()` is removed, so a token endpoint that requires it rejects the call | Call the token endpoint through the plain client, or make `sendWithoutRequest` return `false` for that path |
| Ktor [KTOR-9834](https://youtrack.jetbrains.com/issue/KTOR-9834) | 3.6.0 | Creating an `HttpClient` on Android eagerly initialises SLF4J; a start-up ANR was reported | Do not build the client on the main thread during app start |
| Ktor [KTOR-9891](https://youtrack.jetbrains.com/issue/KTOR-9891) | 3.6.0 | CIO on Kotlin/Native collapses above about 50 concurrent connections (reported on the CIO server) | Use the Darwin engine on iOS |
| Ktor KTOR-9905 | 3.6.2 | Following a redirect can carry `Cookie` and other sensitive headers to another host | Disable redirects on credentialed clients if the API never redirects across hosts |
| kotlinx.serialization 1.12.0 → 1.13.0 | 1.13.0 reverts | 1.12.x wraps non-serialization exceptions thrown while decoding (e.g. `IllegalArgumentException` from an `init` block) in `JsonDecodingException`, a `kotlinx.serialization.json.JsonException`; 1.13.0 and ≤1.11 let them through unwrapped. `MissingFieldException` is never wrapped (1.12 adds the JSON path to its message) | Name the version when triaging; catch `SerializationException` and Ktor's `ContentConvertException`, not a subclass |

## Quick reference — files for `GET orders/{orderId}`

| Layer | File (commonMain) | Contains |
|---|---|---|
| DTO | `orders/data/remote/dto/OrderDto.kt` | `OrderDto`, `OrderItemDto` |
| Domain | `orders/domain/Order.kt` | `Order`, `Money`, `OrderItem`, `OrderStatus` |
| Mapper | `orders/data/OrderMappers.kt` | `toDomainOrNull()` |
| API | `orders/data/remote/OrdersApi.kt` | interface + `KtorOrdersApi` |
| Repository | `orders/domain/OrdersRepository.kt`, `orders/data/OrdersRepositoryImpl.kt` | `ApiResult<Order>` |
| Use case | `orders/domain/GetOrderUseCase.kt` | only if the project uses them |
| DI | `orders/di/OrdersModule.kt` | bindings, added to the app module list |
| Tests | `commonTest/.../orders/*Test.kt` | mapper, API, repository, fake repository |

## Verify

Report each check as `PASS`, `FAIL` or `NOT RUN` (with the reason). Do not report a check you did
not run. Read `settings.gradle.kts` for the real module name; the Android host-test task is
`testDebugUnitTest` with `com.android.library` and `testAndroidHostTest` with
`com.android.kotlin.multiplatform.library`.

```bash
./gradlew :shared:testDebugUnitTest --tests "com.example.app.orders.*"     # fast JVM loop
./gradlew :shared:iosSimulatorArm64Test --tests "com.example.app.orders.*" # Kotlin/Native
./gradlew :shared:allTests                                                 # everything before you finish
./gradlew :composeApp:assembleDebug                                        # app still builds
```

Static checks (adjust `shared/src/commonMain` to the module you changed; review every hit):

```bash
grep -rnE "val [A-Za-z0-9_]+: [^?=]+(,|$)" --include=*Dto.kt shared/src/commonMain           # non-null DTO fields
grep -rnE 'client\.(get|post|put|patch|delete)\("[^"]*\$' --include=*.kt shared/src/commonMain | grep -v encodeURLPathPart   # unencoded path params
grep -rn "HttpClient(" --include=*.kt shared/src/commonMain                                   # clients outside the network module
```

## Common mistakes

| Mistake | Why it bites | Fix |
|---|---|---|
| DTO fields copied as non-null from the sample | The sample had every field; production data does not | Nullable, `= null`, decide in the mapper |
| Money or quantity defaulted to `0` | A missing total renders as "free" and may be acted on | Treat as required: drop or fail |
| `"orders/$id"` | Ids containing `/`, `?` or `#` rewrite the request | `"orders/${id.encodeURLPathPart()}"` |
| Path starts with `/` | Drops the base path (`/v1/`), request hits the wrong URL, 404 | Relative path |
| New `HttpClient` or `install(...)` in the feature | No auth, no timeouts, no log redaction | Inject the shared client by qualifier |
| `try`/`catch` in the API class | Errors mapped in two places, differently | Only the repository's error boundary maps |
| Catching `Exception` without rethrowing `CancellationException` | Cancelled screens keep running and report fake failures | Use the project's error boundary helper |
| Pass-through use case in a project without use cases | Extra layer, extra DI, no behaviour | Follow the project's layering |
| Tests with their own `Json { }` or a hand-built client | Pass while production parsing fails | Build the test client with the production factory and `MockEngine` |
| Only the happy path tested | 404, 401 and malformed bodies ship untested | The four repository error tests are mandatory |
| Triaging a parse failure without the kotlinx.serialization version | 1.12.x and 1.13.0 surface different exception classes for the same bad payload | Read the catalog version first (**Version notes**) |
| OTP auto-fill by reading the SMS inbox | Android 17 delays the message three hours; the user waits or gives up | SMS Retriever or SMS User Consent in `androidMain` |
| Endpoint added but module not registered | `NoDefinitionFoundException` at runtime, only on the screen that needs it | Add the module to the app's list; run the DI verification test |

## Related skills

- **kmp-data-layer** — the architecture these steps implement: client, DTO rules, `ApiResult`, offline-first.
- **kmp-koin-di** — module registration and the DI verification test.
- **kmp-room-database** — caching the response locally.
- **kmp-expect-actual** — the Android SMS Retriever / User Consent behind a shared interface.
- Android 17 SMS OTP changes: <https://developer.android.com/about/versions/17/behavior-changes-17>
  and <https://developer.android.com/about/versions/17/behavior-changes-all>
- Ktor changelog: <https://github.com/ktorio/ktor/blob/main/CHANGELOG.md>; kotlinx.serialization
  changelog: <https://github.com/Kotlin/kotlinx.serialization/blob/master/CHANGELOG.md>
- Ktor client testing: <https://ktor.io/docs/client-testing.html>
- Ktor requests (parameters, bodies, forms): <https://ktor.io/docs/client-requests.html>

Files in this skill

  • SKILL.md20.2 KB
  • references/test-templates.md6.9 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.