UNPKG

@trpc/server

Version:

The tRPC server library

142 lines (109 loc) • 7.93 kB
<p align="center"> <a href="https://trpc.io/"><img src="https://assets.trpc.io/icons/svgs/blue-bg-rounded.svg" alt="tRPC" height="75"/></a> </p> <h3 align="center">tRPC</h3> <p align="center"> <strong>End-to-end typesafe APIs made easy</strong> </p> <p align="center"> <img src="https://assets.trpc.io/www/v10/v10-dark-landscape.gif" alt="Demo" /> </p> # `@trpc/server` > Create tRPC routers and connect them to a server. ## Documentation Full documentation for `@trpc/server` can be found [here](https://trpc.io/docs/router) ## Installation ```bash # npm npm install @trpc/server # Yarn yarn add @trpc/server # pnpm pnpm add @trpc/server # Bun bun add @trpc/server ``` We also recommend installing `zod` to validate procedure inputs. ## Basic Example ```ts import { initTRPC } from '@trpc/server'; import { CreateHTTPContextOptions, createHTTPServer, } from '@trpc/server/adapters/standalone'; import { z } from 'zod'; // Initialize a context for the server function createContext(opts: CreateHTTPContextOptions) { return {}; } // Get the context type type Context = Awaited<ReturnType<typeof createContext>>; // Initialize tRPC const t = initTRPC.context<Context>().create(); // Create main router const appRouter = t.router({ // Greeting procedure greeting: t.procedure .input( z.object({ name: z.string(), }), ) .query(({ input }) => `Hello, ${input.name}!`), }); // Export the app router type to be imported on the client side export type AppRouter = typeof appRouter; // Create HTTP server const { listen } = createHTTPServer({ router: appRouter, createContext, }); // Listen on port 2022 listen(2022); ``` ## Sponsors If you enjoy working with tRPC and want to support us, consider giving a token appreciation by [GitHub Sponsors](https://trpc.io/sponsor)! <!-- SPONSORS:LIST:START --> <!-- oxfmt-ignore-start --> <!-- markdownlint-disable --> <p align="center"> <a href="https://trpc.io/sponsor#sponsors"><img src="https://trpc.io/og/_/sponsor-cloud.png" width="600" alt="tRPC's sponsors, each drawn bigger the more they've given"/></a> </p> ### Silver partners <table> <tr> <td align="center"><a href="https://www.coderabbit.ai/?utm_source=github&amp;utm_medium=referral&amp;ref=trpc&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/132028505?v=4&amp;s=300" width="150" alt="CodeRabbit"/><br />CodeRabbit</a></td> <td align="center"><a href="https://greptile.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/140149887?v=4&amp;s=300" width="150" alt="Greptile"/><br />Greptile</a></td> <td align="center"><a href="https://serpapi.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://github.com/serpapi.png?s=300" width="150" alt="SerpApi"/><br />SerpApi</a></td> </tr> </table> ### Backers <table> <tr> <td align="center"><a href="https://unkey.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/138932600?v=4&amp;s=200" width="100" alt="Unkey"/><br />Unkey</a></td> <td align="center"><a href="https://github.com/hidrb"><img src="https://avatars.githubusercontent.com/u/77294655?v=4&amp;s=200" width="100" alt="Dr. B"/><br />Dr. B</a></td> <td align="center"><a href="https://github.com/ryanmagoon"><img src="https://avatars.githubusercontent.com/u/5327290?v=4&amp;s=200" width="100" alt="Ryan Magoon"/><br />Ryan Magoon</a></td> <td align="center"><a href="https://proxidize.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/70805857?v=4&amp;s=200" width="100" alt="Proxidize"/><br />Proxidize</a></td> <td align="center"><a href="https://liminity.se/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/179804668?v=4&amp;s=200" width="100" alt="Liminity AB"/><br />Liminity AB</a></td> <td align="center"><a href="https://bestkru.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/159320286?v=4&amp;s=200" width="100" alt="BestKru"/><br />BestKru</a></td> </tr> <tr> <td align="center"><a href="https://maxgreenwald.me/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/2615374?u=4c1402dd1e4e8ff7514f2e300adfe9b75ae76e85&amp;v=4&amp;s=200" width="100" alt="Max Greenwald"/><br />Max Greenwald</a></td> <td align="center"><a href="https://github.com/dmaykov"><img src="https://avatars.githubusercontent.com/u/6147048?u=8ae662ac99e91917062164de0d9404002b99cf2e&amp;v=4&amp;s=200" width="100" alt="Dmitry Maykov"/><br />Dmitry Maykov</a></td> <td align="center"><a href="https://chrisbradley.dev/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/11767079?u=e64f67faffd350af19aa896ff89a0708829e9a2a&amp;v=4&amp;s=200" width="100" alt="Chris Bradley"/><br />Chris Bradley</a></td> <td align="center"><a href="https://www.fanvue.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/72873652?v=4&amp;s=200" width="100" alt="fanvue"/><br />fanvue</a></td> <td align="center"><a href="https://aerius.se/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/107723282?u=2639caa14ce293a0b6202c5766a40bb99ac9697e&amp;v=4&amp;s=200" width="100" alt="Aerius Ventilation AB"/><br />Aerius Ventilation AB</a></td> <td align="center"><a href="https://automatio.ai/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/1984909?u=b7bf5bc40ed49df3c22f69d2da7b8d78709c49ed&amp;v=4&amp;s=200" width="100" alt="Stefan Smiljkovic"/><br />Stefan Smiljkovic</a></td> </tr> <tr> <td align="center"><a href="https://iamkhan.io/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/6490268?u=59a369dc23fca0ed9943e5f020ff27ca968704d9&amp;v=4&amp;s=200" width="100" alt="Kalle"/><br />Kalle</a></td> <td align="center"><a href="https://drizzle.team/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/108468352?v=4&amp;s=200" width="100" alt="Drizzle Team"/><br />Drizzle Team</a></td> <td align="center"><a href="https://www.spencermckenney.com/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/15722950?u=e9b60ab93918fb2352b6357571cd67b9004d91e6&amp;v=4&amp;s=200" width="100" alt="Spencer McKenney"/><br />Spencer McKenney</a></td> <td align="center"><a href="https://github.com/infodusha"><img src="https://avatars.githubusercontent.com/u/5677047?u=d503fccc70c9a66524639691b62853994335af0b&amp;v=4&amp;s=200" width="100" alt="Andrei Karushev"/><br />Andrei Karushev</a></td> <td align="center"><a href="https://www.venue.ink/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://images.opencollective.com/venue-ink/49dbae6/logo.png?height=200" width="100" alt="Venue.ink"/><br />Venue.ink</a></td> <td align="center"><a href="https://usevault.app/?ref=trpc&amp;utm_source=github&amp;utm_medium=referral&amp;utm_campaign=trpc"><img src="https://avatars.githubusercontent.com/u/203708130?v=4&amp;s=200" width="100" alt="Vault"/><br />Vault</a></td> </tr> </table> <!-- markdownlint-restore --> <!-- oxfmt-ignore-end --> <!-- SPONSORS:LIST:END -->