kotlin-ktor-patterns

Implements Ktor server patterns for routing, authentication, dependency injection, and testing.

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/freedom909/real-estate-saas --skill kotlin-ktor-patterns-freedom909
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: kotlin-ktor-patterns
Source: https://github.com/freedom909/real-estate-saas/tree/main/.trae/skills/kotlin-ktor-patterns
Command: npx skills add https://github.com/freedom909/real-estate-saas --skill kotlin-ktor-patterns-freedom909

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building Ktor HTTP servers involves many cross-cutting concerns—routing structure, JWT authentication, error handling, CORS, serialization, and dependency injection—that are easy to wire up inconsistently. This Skill provides proven, copy-ready patterns so you configure each plugin correctly the first time. ## Core Features & Use Cases - Routing & Plugin Setup: Standard project layout with route grouping, authenticated route blocks, ContentNegotiation with kotlinx.serialization, StatusPages error handling, and CORS configuration. - JWT Authentication & Koin DI: Complete JWT install/validate/challenge setup, principal extraction helpers, and Koin module definitions for repositories and services. - WebSockets & Testing: WebSocket chat pattern with connection management, plus testApplication integration tests including authenticated route testing. - Use Case: You are scaffolding a new Kotlin REST API with user accounts. Use this Skill to generate the Application module, JWT auth plugin, user routes with validation, and integration tests in one pass. ## Quick Start Use the kotlin-ktor-patterns skill to scaffold a Ktor server with JWT authentication, Koin dependency injection, and testApplication tests for a users REST API.

Frequently Asked Questions about kotlin-ktor-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I set up JWT authentication in a Ktor server?▼

Install the Authentication plugin with a jwt provider, configure a verifier using HMAC256 with your secret, issuer, and audience, then validate credentials and return a JWTPrincipal. Protect routes by wrapping them in an authenticate("jwt") block.

How to structure routes in a Ktor application?▼

Define routes as extension functions on Route in separate files, then register them inside a central routing block in configureRouting. Group related endpoints with route("/path") and separate public endpoints from authenticated ones using authenticate blocks.

Does Ktor support dependency injection with Koin?▼

Yes, Ktor supports Koin via the Koin plugin installed in the Application module. Define modules with single bindings for repositories and services, then access them in routes using the by inject<T>() delegate.

How do I test Ktor routes with testApplication?▼

Use the testApplication block, configure your application modules inside the application lambda, and make requests with the built-in client. For JSON bodies, create a client with ContentNegotiation installed and assert on response status and deserialized bodies.

Why does my Ktor server return 500 instead of a structured error?▼

Unhandled exceptions bypass your response format unless you install the StatusPages plugin. Register exception handlers for specific types like IllegalArgumentException and a fallback Throwable handler that logs the error and responds with a consistent error envelope.

Can Ktor handle WebSocket connections with multiple clients?▼

Yes, install the WebSockets plugin and use the webSocket route block to iterate over incoming frames. Track connections in a synchronized set, snapshot it under lock before broadcasting, and remove the session in a finally block to avoid leaks.