Merging and Nesting Routers
Learn to organize a growing tRPC API by composing multiple sub-routers into a single nested app router.
Merging and Nesting Routers is a free tRPC End-to-End Type Safe APIs lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the tRPC End-to-End Type Safe APIs learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Split Routers?
As your API grows, one giant router becomes hard to read. tRPC lets you build small focused sub-routers and merge them.
A Users Sub-Router
Group all user-related procedures together.
export const userRouter = router({
list: publicProcedure.query(() => getUsers()),
byId: publicProcedure.input(z.string()).query(({ input }) => getUser(input)),
});A Posts Sub-Router
Define another router for posts.
export const postRouter = router({
list: publicProcedure.query(() => getPosts()),
create: publicProcedure.input(z.object({ title: z.string() })).mutation(({ input }) => addPost(input)),
});Nesting Routers
Combine sub-routers under namespaces in the main router.
export const appRouter = router({
user: userRouter,
post: postRouter,
});Calling Nested Procedures
The client path mirrors the nesting structure.
await client.user.list.query();
await client.post.create.mutate({ title: "Hi" });Deep Nesting
You can nest several levels for clear domains.
export const appRouter = router({
admin: router({
user: userRouter,
report: reportRouter,
}),
});Type Inference Flows Through
No matter how deeply you nest, full type inference and autocomplete reach every procedure on the client.
Merging vs Nesting
Nesting puts a router under a namespace key. Merging with t.mergeRouters flattens two routers into one with no extra namespace.
const merged = t.mergeRouters(userRouter, postRouter);When to Merge
Merge when procedures naturally belong at the same level; nest when you want clear domain prefixes like user.* and post.*.
Avoiding Name Collisions
Merging two routers that share a procedure name causes a conflict. Nesting avoids this because each lives under its own namespace.
Keeping Files Small
Place each sub-router in its own file and import them into one root router file. This keeps modules focused and makes the API structure easy to navigate.
Quick Check
Test your router composition knowledge.
Recap
You learned to scale a tRPC API cleanly:
- Build small sub-routers per domain
- Nest them under namespaces for clear paths
- Merge when procedures share a level
Composition keeps large APIs organized while preserving full type safety.
Frequently asked questions
Is the “Merging and Nesting Routers” lesson free?
Yes — the full text of “Merging and Nesting Routers” is free to read here on the web, and the tRPC End-to-End Type Safe APIs course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the tRPC End-to-End Type Safe APIs course, upgrade to CoddyKit PRO.
What will I learn in “Merging and Nesting Routers”?
Learn to organize a growing tRPC API by composing multiple sub-routers into a single nested app router. You practise tRPC End-to-End Type Safe APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start tRPC End-to-End Type Safe APIs?
No prior experience is required. tRPC End-to-End Type Safe APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Merging and Nesting Routers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this tRPC End-to-End Type Safe APIs lesson?
Yes. Every tRPC End-to-End Type Safe APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Structuring with tRPC Routers
- Implementing Query Procedures
- Developing Mutation Procedures
- Merging and Nesting Routers