Wrap the API Behind the Repository
Hide Ktor and serialization from callers.
Wrap the API Behind the Repository is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Now Build the Real Thing
With the interface set, you write a concrete implementation that actually fetches data behind the scenes.
Implement the Interface
Create a class that uses implements the repository interface, promising to fulfill every method it declares.
class UserRepositoryImpl : UserRepositoryInject the Ktor Client
Pass the HttpClient into the constructor instead of creating it inside, so it can be shared and replaced in tests.
class UserRepositoryImpl(private val client: HttpClient) : UserRepositoryHide the Network Call
Inside each method, make the Ktor request and return the result. Callers see clean data, never the request itself.
override suspend fun getUsers() = client.get(url).body<List<User>>()Keep URLs Inside
Store base URLs and paths within the implementation. The rest of the app stays blissfully unaware of any endpoint.
One Place to Change
If the API path changes, you edit only this class. The interface and every caller stay untouched.
Serialization Stays Hidden
The body call turns JSON into models here, so serialization never leaks out to the UI layer.
val user: User = client.get(url).body()Same Code, Both Apps
This implementation lives in commonMain, so the same fetching code powers Android and iOS identically.
Swap Without Breaking Callers
Because it satisfies the interface, you can replace it with a caching version later and no caller changes.
Wire It With DI
A dependency injection setup hands callers this implementation through the interface type, keeping things loosely coupled.
single<UserRepository> { UserRepositoryImpl(get()) }The Full Wrapper
Here is the complete wrapper: it takes a client, hides the request, and returns plain domain models.
class UserRepositoryImpl(
private val client: HttpClient
) : UserRepository {
override suspend fun getUsers() =
client.get("/users").body<List<User>>()
}Quick Check
What is the main job of the repository implementation?
Recap
The implementation takes an HttpClient, performs the request, and returns domain models, keeping all networking sealed off from callers.
Frequently asked questions
Is the “Wrap the API Behind the Repository” lesson free?
Yes — the full text of “Wrap the API Behind the Repository” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.
What will I learn in “Wrap the API Behind the Repository”?
Hide Ktor and serialization from callers. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Multiplatform Academy?
No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Wrap the API Behind the Repository” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Multiplatform Academy lesson?
Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Define a Repository Interface
- Wrap the API Behind the Repository
- Map DTOs to Domain Models
- Expose Results as Flow