kotlin-ktor-patterns
What it does
This code helps build web servers using the Kotlin language. It organizes the different parts of the server, such as routes (the paths users access), how data is sent and received, and how to protect access.
When to use it
- βWhen you are starting to build a website or web application with Kotlin.
- βWhen defining which addresses (URLs) your server will respond to.
- βWhen you need to configure who can access certain parts of your server.
Skill link
https://github.com/affaan-m/ECC/blob/main/pi/core/skills/kotlin-ktor-patterns/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βHow can I set up my Ktor server to accept data in JSON format?β
5 new skills a week. 1 minute to read.
How it works
It uses a set of 'recipes' (patterns) to organize the server code. These recipes show how to set up routes, handle security, and ensure data is formatted correctly.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Ktor Server Patterns
Comprehensive Ktor patterns for building robust, maintainable HTTP servers with Kotlin coroutines.
When to Activate
- Building Ktor HTTP servers
- Configuring Ktor plugins (Auth, CORS, ContentNegotiation, StatusPages)
- Implementing REST APIs with Ktor
- Setting up dependency injection with Koin
- Writing Ktor integration tests with testApplication
- Working with WebSockets in Ktor
Application Structure
Standard Ktor Project Layout
src/main/kotlin/
βββ com/example/
β βββ Application.kt # Entry point, module configuration
β βββ plugins/
β β βββ Routing.kt # Route definitions
β β βββ Serialization.kt # Content negotiation setup
β β βββ Authentication.kt # Auth configuration
β β βββ StatusPages.kt # Error handling
β β βββ CORS.kt # CORS configuration
β βββ routes/
β β βββ UserRoutes.kt # /users endpoints
β β βββ AuthRoutes.kt # /auth endpoints
β β βββ HealthRoutes.kt # /health endpoints
β βββ models/
β β βββ User.kt # Domain models
β β βββ ApiResponse.kt # Response envelopes
β βββ services/
β β βββ UserService.kt # Business logic
β β βββ AuthService.kt # Auth logic
β βββ repositories/
β β βββ UserRepository.kt # Data access interface
β β βββ ExposedUserRepository.kt
β βββ di/
β βββ AppModule.kt # Koin modules
src/test/kotlin/
βββ com/example/
β βββ routes/
β β βββ UserRoutesTest.kt
β βββ services/
β βββ UserServiceTest.kt
Application Entry Point
// Application.kt
fun main() {
embeddedServer(Netty, port = 8080, module = Application::module).start(wait = true)
}
fun Application.module() {
configureSerialization()
configureAuthentication()
configureStatusPages()
configureCORS()
configureDI()
configureRouting()
}
Routing DSL
Basic Routes
// plugins/Routing.kt
fun Application.configureRouting() {
routing {
userRoutes()
authRoutes()
healthRoutes()
}
}
// routes/UserRoutes.kt
fun Route.userRoutes() {
val userService by inject<UserService>()
route("/users") {
get {
val users = userService.getAll()
call.respond(users)
}
get("/{id}") {
val id = call.parameters["id"]
?: return@get call.respond(HttpStatusCode.BadRequest, "Missing id")
val user = userService.getById(id)
?: return@get call.respond(HttpStatusCode.NotFound)
call.respond(user)
}
post {
val request = call.receive<CreateUserRequest>()
val user = userService.create(request)
call.respond(HttpStatusCode.Created, user)
}
put("/{id}") {
val id = call.parameters["id"]
?: return@put call.respond(HttpStatusCode.BadRequest, "Missing id")
val request = call.receive<UpdateUserRequest>()
val user = userService.update(id, request)
?: return@put call.respond(HttpStatusCode.NotFound)
call.respond(user)
}
delete("/{id}") {
val id = call.parameters["id"]
?: return@delete call.respond(HttpStatusCode.BadRequest, "Missing id")
val deleted = userService.delete(id)
if (deleted) call.respond(HttpStatusCode.NoContent)
else call.respond(HttpStatusCode.NotFound)
}
}
}
Route Organization with Authenticated Routes
fun Route.userRoutes() {
route("/users") {
// Public routes
get { /* list users */ }
get("/{id}") { /* get user */ }
// Protected routes
authenticate("jwt") {
post { /* create user - requires auth */ }
put("/{id}") { /* update user - requires auth */ }
delete("/{id}") { /* delete user - requires auth */ }
}
}
}
Content Negotiation & Serialization
kotlinx.serialization Setup
// plugins/Serialization.kt
fun Application.configureSerialization() {
install(ContentNegotiation) {
json(Json {
prettyPrint = true
isLenient = false
ignoreUnknownKeys = true
encodeDefaults = true
explicitNulls = false
})
}
}
Serializable Models
@Serializable
β¦
FAQ
What is a 'route'?
A route is like a specific address on your web server. For example, '/users' might be a route to view a list of users.
What does 'authentication' mean?
Authentication is the process of verifying if a user is who they claim to be, usually by asking for a username and password or a token.
What is 'dependency injection' (DI)?
It's a way of organizing code where parts of the program receive the other parts they need, instead of creating them themselves. This makes the code easier to manage.
Details
Original description by the author
Ktor server patterns including routing DSL, plugins, authentication, Koin DI, kotlinx.serialization, WebSockets, and testApplication testing. Use when building a Ktor server β routing, plugins, auth, DI, serialization, or tests.
repo: affaan-m/ECC
Popularity
- GitHub stars
- 280k
- Ranking position
- #195
- License
- MIT
- Updated
- 10/15/2026
Comments
- β¦
More skills from this repository
accessibility
280kHelps your agent create interfaces that everyone can use. Ensures websites and apps work well for people with disabilities, like those who are blind or have reduced mobility.
agent-architecture-audit
280kMakes your agent check if everything is working correctly. It finds and fixes problems in AI systems, ensuring they perform as expected.
agent-eval
280kHelps your agent compare different AI coding tools. It measures which one is faster, cheaper, and more reliable for specific tasks.
agent-harness-construction
280kMakes your agent better understand how to perform tasks. It optimizes the tools and instructions the agent uses to increase its chances of success.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.