Express Adapter
Example app
| Description | Links |
|---|---|
| Express server & procedure calls with Node.js. |
How to add tRPC to existing Express project
1. Install deps
bashyarn add @trpc/server zod
bashyarn add @trpc/server zod
Zod isn't a required dependency, but it's used in the sample router below.
AI Agents
If you use an AI coding agent, install tRPC skills for better code generation:
bashnpx @tanstack/intent@latest install
bashnpx @tanstack/intent@latest install
2. Create a tRPC router
Implement your tRPC router. A sample router is given below:
server.tstsimport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();export constappRouter =t .router ({getUser :t .procedure .input (z .string ()).query ((opts ) => {opts .input ; // stringreturn {id :opts .input ,name : 'Bilbo' };}),createUser :t .procedure .input (z .object ({name :z .string ().min (5) })).mutation (async (opts ) => {// use your ORM of choicereturn {id : '1', ...opts .input };}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
server.tstsimport {initTRPC } from '@trpc/server';import {z } from 'zod';export constt =initTRPC .create ();export constappRouter =t .router ({getUser :t .procedure .input (z .string ()).query ((opts ) => {opts .input ; // stringreturn {id :opts .input ,name : 'Bilbo' };}),createUser :t .procedure .input (z .object ({name :z .string ().min (5) })).mutation (async (opts ) => {// use your ORM of choicereturn {id : '1', ...opts .input };}),});// export type definition of APIexport typeAppRouter = typeofappRouter ;
If your router file starts getting too big, split your router into several subrouters each implemented in its own file. Then merge them into a single root appRouter.
3. Use the Express adapter
tRPC includes an adapter for Express out of the box. This adapter lets you convert your tRPC router into an Express middleware.
server.tstsimport {initTRPC } from '@trpc/server';import * astrpcExpress from '@trpc/server/adapters/express';importexpress from 'express';// created for each requestconstcreateContext = ({req ,res ,}:trpcExpress .CreateExpressContextOptions ) => ({}); // no contexttypeContext =Awaited <ReturnType <typeofcreateContext >>;constt =initTRPC .context <Context >().create ();constappRouter =t .router ({// [...]});constapp =express ();app .use ('/trpc',trpcExpress .createExpressMiddleware ({router :appRouter ,createContext ,}),);app .listen (4000);
server.tstsimport {initTRPC } from '@trpc/server';import * astrpcExpress from '@trpc/server/adapters/express';importexpress from 'express';// created for each requestconstcreateContext = ({req ,res ,}:trpcExpress .CreateExpressContextOptions ) => ({}); // no contexttypeContext =Awaited <ReturnType <typeofcreateContext >>;constt =initTRPC .context <Context >().create ();constappRouter =t .router ({// [...]});constapp =express ();app .use ('/trpc',trpcExpress .createExpressMiddleware ({router :appRouter ,createContext ,}),);app .listen (4000);
Your endpoints are now available via HTTP!
| Endpoint | HTTP URI |
|---|---|
getUser | GET http://localhost:4000/trpc/getUser?input=INPUT where INPUT is a URI-encoded JSON string. |
createUser | POST http://localhost:4000/trpc/createUser with req.body of type {name: string} |