Fetch / Edge Runtimes Adapter
You can create a tRPC server within any edge runtime that follow the WinterCG, specifically the Minimum Common Web Platform API specification.
Some of these runtimes includes, but not limited to:
- Cloudflare Workers
- Deno Deploy
- Vercel Edge Runtime (& Next.js Edge Runtime)
This also makes it easy to integrate into frameworks that uses the web platform APIs to represent requests and responses, such as:
- Astro (SSR mode)
- Remix
- SolidStart
Example apps
| Description | Links | 
|---|---|
| Cloudflare Workers example | Source | 
| Deno Deploy example | Source | 
| Next.js Edge Runtime example | Source | 
| Vercel Edge Runtime example | Source | 
How to use tRPC server with an edge runtime
tRPC provides a fetch adapter that uses the native Request and Response APIs as input and output. The tRPC-specific code is the same across all runtimes, the only difference being how the response is returned.
tRPC includes an adapter for the native Fetch API out of the box. This adapter lets you convert your tRPC router into a Request handler that returns Response objects.
Required Web APIs
tRPC server uses the following Fetch APIs:
- Request,- Response
- fetch
- Headers
- URL
If your runtime supports these APIs, you can use tRPC server.
Fun fact: that also means you can use a tRPC server in your browser!
Common setup
Install dependencies
You can skip this step if you use Deno Deploy.
- npm
- yarn
- pnpm
- bun
sh
sh
sh
sh
sh
sh
sh
sh
Zod isn't a required dependency, but it's used in the sample router below.
Create the router
First of all you need a router to handle your queries, mutations and subscriptions.
A sample router is given below, save it in a file named router.ts.
router.ts
router.tsts
router.tsts
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.
Create the context
Then you need a context that will be created for each request.
A sample context is given below, save it in a file named context.ts:
context.ts
context.tsts
context.tsts
Runtimes-specific setup
Astro
src/pages/trpc/[trpc].tsts
src/pages/trpc/[trpc].tsts
Cloudflare Worker
You need the Wrangler CLI to run Cloudflare Workers.
Create Cloudflare Worker
server.tsts
server.tsts
Run wrangler dev server.ts and your endpoints will be available via HTTP!
| Endpoint | HTTP URI | 
|---|---|
| getUser | GET http://localhost:8787/trpc/getUserById?input=INPUTwhere INPUTis a URI-encoded JSON string. | 
| createUser | POST http://localhost:8787/trpc/createUserwith req.bodyof typeUser | 
Deno Oak
This assumes you have Deno installed and setup. Refer to their getting started guide for more information.
Update the imports in router.ts
router.tsts
router.tsts
Update the imports in context.ts
context.tsts
context.tsts
Use fetchRequestHandler with Oak in app.ts
app.tsts
app.tsts
Deno Deploy
This assumes you have Deno installed and setup. Refer to their getting started guide for more information.
See our example Deno Deploy app for a working example.
Update the imports in router.ts
router.tsts
router.tsts
Update the imports in context.ts
context.tsts
context.tsts
Create Deno Deploy Function
server.tsts
server.tsts
Run deno run --allow-net=:8000 --allow-env ./server.ts and your endpoints will be available via HTTP!
| Endpoint | HTTP URI | 
|---|---|
| getUser | GET http://localhost:8000/trpc/getUserById?input=INPUTwhere INPUTis a URI-encoded JSON string. | 
| createUser | POST http://localhost:8000/trpc/createUserwith req.bodyof typeUser | 
Next.js Edge Runtime
See a full example here.
Remix
app/routes/trpc/$trpc.tsts
app/routes/trpc/$trpc.tsts
SolidStart
src/routes/api/trpc/[trpc].tsts
src/routes/api/trpc/[trpc].tsts
Vercel Edge Runtime
See the official Vercel Edge Runtime documentation for more information.
See our example Vercel Edge Runtime app for a working example.
Install dependencies
- npm
- yarn
- pnpm
- bun
sh
sh
sh
sh
sh
sh
sh
sh
Create Edge Runtime Function
server.tsts
server.tsts
Run edge-runtime --listen server.ts --port 3000 and your endpoints will be available via HTTP!
| Endpoint | HTTP URI | 
|---|---|
| getUser | GET http://localhost:3000/trpc/getUserById?input=INPUTwhere INPUTis a URI-encoded JSON string. | 
| createUser | POST http://localhost:3000/trpc/createUserwith req.bodyof typeUser |