Skip to content
Agentmart
Submit a tool

add-api-service

Add a Ktor-backed network request end-to-end — request/response DTOs, an API service, a repository, and a ViewModel call.

Skill by KotlinFoundation

IndexedMIT★ 28
Write a review

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/kotlinfoundation-add-api-service

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

Add a Ktor-backed network request end-to-end — request/response DTOs, an API service, a repository, and a ViewModel call. Use when the user wants to fetch or post data over HTTP, call any REST/third-party API, or extend an AI provider call (OpenAI/Replicate route through AiTransport — this skill explains how). To deploy the Cloud Functions AI proxy itself, use integrate-web-proxy instead.

Use cases: backend, devops, data

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md and code. Checked Oct 26, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/add-api-service

What's inside

Skill name
add-api-service
Description
Add a Ktor-backed network request end-to-end — request/response DTOs, an API service, a repository, and a ViewModel call. Use when the user wants to fetch or post data over HTTP, call any REST/third-party API, or extend an AI provider call (OpenAI/Replicate route through AiTransport — this skill explains how). To deploy the Cloud Functions AI proxy itself, use integrate-web-proxy instead.
License
MIT
Instructions
92 lines in SKILL.md · view on GitHub

Files (1)

  • SKILL.md

SKILL.md (first lines)

# Add an API service (network request)

Generic Ktor call to **any** URL — this is not tied to the project's own backend. Follow the layering:
DTOs → API service → repository (`Result` wrapping) → ViewModel.

Paths below are under `shared/src/commonMain/kotlin/com/kotlinfoundation/koko/`.

> **AI services are special.** The OpenAI/Replicate services route through `AiTransport` (proxy vs direct
> mode) rather than calling the client directly. In **proxy** mode the response is the
> `{statusCode, errorMessage, data}` envelope (`AiApiBaseResponse<T>`); in **direct** mode the provider
> returns its **raw body at the top level** and `AiTransport` re-wraps it into the same envelope — so the
> DTOs (`T`) stay identical. If you add a new AI provider, mirror this: pass your `directUrl` + auth
> headers + key-readiness to `AiTransport.execute`. Non-AI services just use the Ktor client directly, as
> below.

## 1. DTOs — `data/source/remote/request/` and `data/source/remote/response/`

`*Request` / `*Response` suffixes are required. All DTOs `@Serializable` + `@SerialName`. Response DTOs
carry an `asDomain()` mapper. **Return raw types, never `Result`** — repositories do the wrapping.

```kotlin
// response/GetJobsResponse.kt
@Serializable
data class GetJobsResponse(
    @SerialName("jobs") val jobs: List<JobResponse>,
) {
    fun asDomain(): List<Job> = jobs.map { it.asDomain() }
}

@Serializable
data class JobResponse(
    @SerialName("id") val id: String,
    @SerialName("title") val title: String,
) {
    fun asDomain(): Job = Job(id = id, title = title)
}
```

## 2. API service — `data/source/remote/apiservices/`

Take the shared `HttpClient` (base URL + JSON + logging are already configured in
`data/source/remote/HttpClientFactory.kt`). Return raw DTOs; let exceptions propagate.

```kotlin
class JobApiService(private val httpClient: HttpClient) {
    suspend fun getJobs(request: GetJobsRequest): GetJobsResponse =
        httpClient.post("/jobs") { setBody(request) }.body()
}
```
Register in `root/Di.kt` `dataModule` alongside the existing `factoryOf(::ApiService)`:
```kotlin
factoryOf(::JobApiService)
```

## 3. Repository — `data/repository/`

Concrete class (no interface). Wrap in `Result` via `backgroundExecutor.execute { }` — it already
catches, logs, and returns `Result.failure`, so **no redundant try-catch**.

```kotlin
class JobRepository(
    private val jobApiService: JobApiService,
    private val backgroundExecutor: BackgroundExecutor = BackgroundExecutor.IO,
) {
    suspend fun getJobs(page: Int, limit: Int): Result<List<Job>> =
        backgroundExecutor.execute {
            val response = jobApiService.getJobs(GetJobsRequest(page, limit))
            Result.success(response.asDomain())
        }
}
```
Register in `dataModule`: `single { JobRepository(get()) }`.

## 4. Call from a ViewModel

Inject the repository, call the suspend function inside `viewModelScope`, fold the `Result` into UiState.

## Testing

Use Ktor `MockEngine` to stub responses/errors — never hit the real network in unit tests
(`shared/src/commonTest/`). See `AiGuidelines/tech/api_services.md`.

Validate with the `run-quality-gates` skill.

## Talking to Firebase?

If the endpoint is backed by Firestore — cloud sync, cross-device state, a server-authoritative balance
— start from `sync-data-firebase`, not here. No Firebase client SDK supports the `wasmJs` target, so the
approach has to be decided with the developer before any code is written. This skill still describes the
client layering once that decision is made.

From KotlinFoundation/kmp-contest-starter-kit, MIT.

More from KotlinFoundation/kmp-contest-starter-kit

Signals

Stars
28
Forks
13
Open issues
1
Last commit (this folder)
Aug 10, 2026 (2 months ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from KotlinFoundation

Cite this page

add-api-service skill. Agent Mart. https://agentmart.dev/skills/kotlinfoundation-add-api-service (last updated 2026-10-24)