kotlin-ktor-patterns

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

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill kotlin-ktor-patterns-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-ktor-patterns
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/kotlin-ktor-patterns
Command: npx skills add https://github.com/Femad-6/my-skills --skill kotlin-ktor-patterns-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building a Ktor HTTP server requires wiring together many plugins—routing, serialization, JWT authentication, CORS, error handling, and dependency injection—and getting each one right involves boilerplate and subtle configuration details that are easy to get wrong. ## Core Features & Use Cases - Routing and Plugin Setup: Provides complete patterns for the Ktor routing DSL, ContentNegotiation with kotlinx.serialization, StatusPages error handling, and CORS configuration. - JWT Authentication and Koin DI: Shows how to configure JWT authentication with protected routes and wire services and repositories through Koin modules. - Integration Testing: Demonstrates testApplication-based tests, including authenticated route testing with generated JWTs. - Use Case: When scaffolding a new Kotlin REST API, apply these patterns to get a working server with authenticated CRUD endpoints, consistent JSON error responses, and integration tests in one pass. ## Quick Start Ask the AI to create 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 the credential payload. Wrap protected routes in an authenticate("jwt") block and extract claims via JWTPrincipal.

How to use Koin dependency injection in Ktor routes?

Install the Koin plugin in your Application module with your defined modules, then use 'by inject<T>()' inside route extension functions to resolve services. Define repositories and services as singles in a Koin module for constructor injection.

How do I test Ktor routes with testApplication?

Use the testApplication block, configure your application modules inside it, and make requests with the built-in client. For JSON bodies, create a client with ContentNegotiation installed and use setBody with serializable request objects.

Does Ktor ContentNegotiation support kotlinx.serialization?

Yes, install ContentNegotiation and call json() with a configured Json instance to enable kotlinx.serialization. You can control pretty printing, leniency, unknown key handling, and null encoding through the Json builder options.

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

Unhandled exceptions bypass your response format unless you install StatusPages. Register exception handlers for specific types like IllegalArgumentException and a fallback Throwable handler to return consistent ApiResponse error bodies.

When should I use WebSockets instead of REST in Ktor?

Use WebSockets when clients need real-time bidirectional communication such as chat or live updates, since REST requires polling. Install the WebSockets plugin, configure ping period and frame size, and manage connections with a synchronized set.