Next.js Integration
Next.js is a leading React framework for server-rendered apps. oRPC works with both the App Router and Pages Router. For additional context, refer to the Node Integration and Fetch Server Integration guides.
INFO
oRPC also supports Server Action out-of-the-box.
App Router
ts
import { RPCHandler } from '@orpc/server/fetch'
const handler = new RPCHandler(router)
async function handleRequest(request: Request) {
const { response } = await handler.handle(request, {
prefix: '/rpc',
context: {}, // Provide initial context if needed
})
return response ?? new Response('Not found', { status: 404 })
}
export const GET = handleRequest
export const POST = handleRequest
export const PUT = handleRequest
export const PATCH = handleRequest
export const DELETE = handleRequest
INFO
The handler
can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or another custom handler.
Pages Router
ts
import { RPCHandler } from '@orpc/server/node'
const handler = new RPCHandler(router)
export const config = {
api: {
bodyParser: false,
},
}
export default async (req, res) => {
const { matched } = await handler.handle(req, res, {
prefix: '/rpc',
context: {}, // Provide initial context if needed
})
if (matched) {
return
}
res.statusCode = 404
res.end('Not found')
}
WARNING
Next.js default body parser blocks oRPC raw‑request handling. Ensure bodyParser
is disabled in your API route:
ts
export const config = {
api: {
bodyParser: false,
},
}
INFO
The handler
can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or another custom handler.