Skip to content

Cloudflare Workers Integration

Cloudflare Workers provide a serverless execution environment for building fast, globally distributed applications that follow the Fetch API. For additional context, refer to the Fetch Server Integration guide.

Basic

ts
import { RPCHandler } from '@orpc/server/fetch'
import { CORSPlugin } from '@orpc/server/plugins'

const handler = new RPCHandler(router, {
  plugins: [
    new CORSPlugin()
  ]
})

export default {
  async fetch(request: Request, env: any, ctx: ExecutionContext): Promise<Response> {
    const url = new URL(request.url)

    if (url.pathname.startsWith('/rpc')) {
      const { response } = await handler.handle(request, {
        prefix: '/rpc',
        context: {} // Provide initial context if needed
      })

      if (response) {
        return response
      }
    }

    return new Response('Not found', { status: 404 })
  }
}

INFO

The handler can be any supported oRPC handler, such as RPCHandler, OpenAPIHandler, or another custom handler.

Released under the MIT License.