Routing organization
One of Ktor's strong points is its flexibility and that it does not enforce a single routing organization strategy. Instead, you can organize routes in a way that best fits the size and complexity of your application, and many projects combine the patterns described below.
This page shows common patterns for organizing routing code as your project grows.
Group by file
One way to organize routing is to place related routes in separate files. This keeps route definitions small and readable.
For example, if your application is managing customers and orders, you could split the routing logic between CustomerRoutes.kt and OrderRoutes.kt files:
Each file groups route handlers that belong to the same domain area. Then, you can register each group in your routing block:
This approach works well for small or medium-sized projects where each domain area only contains a few routes.
Group by package (folders)
As a file grows, it can become harder to navigate. To keep routing logic small and focused, you can distribute routing logic into multiple files inside a dedicated package:
Each file contains a small part of the routing logic, while the folder represents the domain.
This structure keeps each route definition small and easy to navigate as the number of endpoints increases. Therefore, it is ideal for large applications, such as APIs with many endpoints.
Group routes by path and nest resources
You can organize routes by grouping all handlers for the same path and nesting related resources. Nested routing is useful when an endpoint includes multiple operations on the same resource:
Grouping by path keeps related endpoints visually close and makes the routing structure easier to understand from an HTTP API standpoint.
Group by feature or domain
As your application grows, grouping by domain or feature becomes more scalable.
Each feature has its own package containing only the routing code relevant to that domain.
For example, the CustomerRoutes file from the above example structure may contain the following route definitions:
This pattern keeps feature boundaries clear and prevents routing files from growing too large, especially when each domain area contains many endpoints.