Skip to content
You are reading the v2 docs, currently in beta.V1 docs
oRPC
Esc
navigateopen⌘Jpreview
On this page

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
    })

    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
    })

    if (matched) {
      return response
    }

    return new Response('Not found', { status: 404 })
  },
} satisfies ExportedHandler<Env>

Compatibility Flags

oRPC forwards request.signal to every procedure call so handlers can stop work when the client disconnects. Workers only abort that signal when the enable_request_signal flag is set, so add it to your Wrangler configuration:

{
  "compatibility_date": "2026-07-01",
  "compatibility_flags": ["enable_request_signal"]
}

If your compatibility_date is before 2026-03-03, also add unhandled_rejection_after_microtask_checkpoint. Without it, Workers can report false unhandledrejection errors for promises that are handled a microtask later.

Last updated on September 6, 2026

Was this page helpful?