Cloudflare Workers Adapter
Use oRPC on Cloudflare Workers through the Fetch API Adapter, with the compatibility flags that make request cancellation and unhandled rejections behave.
Cloudflare Workers follow the Fetch API, so oRPC integrates through the Fetch API Adapter.
Basic
import { onError } from '@orpc/server'
import { RPCHandler } from '@orpc/server/fetch'
import { CORSHandlerPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new CORSHandlerPlugin()
],
interceptors: [
onError((error) => {
console.error(error)
}),
],
})
export default {
async fetch(request, env, ctx) {
const { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: { env, ctx } // Provide initial context if needed, can be an async function
})
if (matched) {
return response
}
return new Response('Not found', { status: 404 })
},
} satisfies ExportedHandler<Env>import { OpenAPIHandler } from '@orpc/openapi/fetch'
import { onError } from '@orpc/server'
import { CORSHandlerPlugin } from '@orpc/server/plugins'
const handler = new OpenAPIHandler(router, {
plugins: [
new CORSHandlerPlugin()
],
interceptors: [
onError((error) => {
console.error(error)
}),
],
})
export default {
async fetch(request, env, ctx) {
const { matched, response } = await handler.handle(request, {
prefix: '/api',
context: { env, ctx } // Provide initial context if needed, can be an async function
})
if (matched) {
return response
}
return new Response('Not found', { status: 404 })
},
} satisfies ExportedHandler<Env>Compatibility Flags
Use a compatibility_date of 2026-11-17 or later:
{
"compatibility_date": "2026-11-17"
}
On older dates, enable these flags:
enable_request_signal: abortsrequest.signalwhen the client disconnects, so procedures can stop work.unhandled_rejection_after_microtask_checkpoint(default from2026-03-03): prevents falseunhandledrejectionerrors.
Traces
Use the Cloudflare Workers Traces integration to record oRPC spans in Workers Traces.