-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
190: Building a Simple Note-Taking App with Ktor Backend
How should I set up my Note model for JSON?
When you're building a Ktor backend, the first thing I always suggest is getting your data models sorted. Since we're making a note-taking app, we need a Note class. But here's the catch: Ktor doesn't magically know how to turn a Kotlin object into JSON. You need the kotlinx.serialization library for that.
I usually keep it simple. You'll want a unique ID, a title, and the actual body of the note. I've added the @Serializable annotation here—without this, Ktor will throw an error the moment you try to respond with a note object.
import kotlinx.serialization.Serializable
@Serializable
data class Note(
val id: Int,
val title: String,
val content: String
)
One pro tip: if you're planning to let users update notes later, you might want to make the fields optional or use a separate "Request" class, but for this simple version, a standard data class is perfect.
Where do I put the logic to store and retrieve the notes?
You could technically throw your notes into a list right inside the routing block, but that's a recipe for messy code. I prefer creating a separate "Repository" class. It keeps the data logic separate from the HTTP logic, which makes your life much easier when you eventually decide to move from an in-memory list to a real database like PostgreSQL or MongoDB.
Since Ktor is asynchronous, be careful with a standard ArrayList. I'll use a synchronized list here to prevent the app from crashing if two people try to save a note at the exact same millisecond.
import java.util.Collections
class NoteRepository {
private val notes = Collections.synchronizedList(mutableListOf<Note>())
private var currentId = 1
fun getAllNotes(): List<Note> = notes
fun addNote(title: String, content: String): Note {
val newNote = Note(currentId++, title, content)
notes.add(newNote)
return newNote
}
}
What does the routing code look like for the API endpoints?
Now we tie it all together. In your Application.module, you'll define your routes. You need a GET endpoint to fetch all notes and a POST endpoint to create a new one. I find it helpful to instantiate the repository outside the routing block so the state persists as long as the server is running.
Notice how we use call.receive<Note>(). This is where Ktor's ContentNegotiation plugin does the heavy lifting, converting the incoming JSON body directly into our Kotlin object.
routing {
val repository = NoteRepository()
route("/notes") {
get {
val allNotes = repository.getAllNotes()
call.respond(allNotes)
}
post {
val noteRequest = call.receive<Note>()
val createdNote = repository.addNote(noteRequest.title, noteRequest.content)
call.respond(HttpStatusCode.Created, createdNote)
}
}
}
I used HttpStatusCode.Created (201) for the POST request because it's more descriptive than a generic 200 OK. It tells the client, "Yes, I actually created a new resource for you."
📋 Practical Task
Implement a Note Deletion Endpoint
Now that you have the basic "Read" and "Create" functionality working, it's time to handle deletions. Your task is to extend the Note-Taking app by adding a DELETE endpoint.
- Modify the
NoteRepositoryclass to include adeleteNote(id: Int): Booleanmethod that removes a note by its ID and returnstrueif it was found and removed, orfalseotherwise. - Add a new route
delete("/{id}")inside the/notesblock. - The route should extract the
idfrom the path parameters, call the repository, and respond withHttpStatusCode.NoContent(204) if the deletion was successful, orHttpStatusCode.NotFound(404) if the note didn't exist.
There are no comments for now.