End-to-end typesafe APIs made easy
@trpc/server
Create tRPC routers and connect them to a server.
Documentation
Full documentation for @trpc/server
can be found here
Installation
npm install @trpc/server@next
yarn add @trpc/server@next
pnpm add @trpc/server@next
We also recommend installing zod
to validate procedure inputs.
Basic Example
import { inferAsyncReturnType, initTRPC } from '@trpc/server';
import {
CreateHTTPContextOptions,
createHTTPServer,
} from '@trpc/server/adapters/standalone';
import { z } from 'zod';
function createContext(opts: CreateHTTPContextOptions) {
return {};
}
type Context = inferAsyncReturnType<typeof createContext>;
const t = initTRPC.context<Context>.create();
const appRouter = t.router({
greeting: t.procedure
.input(
z.object({
name: z.string(),
}),
)
.query(({ input }) => `Hello, ${input.name}!`),
});
export type AppRouter = typeof appRouter;
const { listen } = createHTTPServer({
router: appRouter,
createContext,
});
listen(2022);