HTTP
oRPC includes built-in HTTP support, making it easy to expose RPC endpoints in any environment that speaks HTTP.
Server Adapters
Adapter | Target |
---|---|
fetch | MDN Fetch API (Browser, Bun, Deno, Cloudflare Workers, etc.) |
node | Node.js built-in http /http2 |
ts
import { createServer } from 'node:http' // or 'node:http2'
import { RPCHandler } from '@orpc/server/node'
import { CORSPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new CORSPlugin()
]
})
const server = createServer(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')
})
server.listen(3000, '127.0.0.1', () => console.log('Listening on 127.0.0.1:3000'))
ts
import { RPCHandler } from '@orpc/server/fetch'
import { CORSPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new CORSPlugin()
]
})
Bun.serve({
async fetch(request: Request) {
const { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
if (matched) {
return response
}
return new Response('Not found', { status: 404 })
}
})
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 { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
if (matched) {
return response
}
return new Response('Not found', { status: 404 })
}
}
ts
import { RPCHandler } from '@orpc/server/fetch'
import { CORSPlugin } from '@orpc/server/plugins'
const handler = new RPCHandler(router, {
plugins: [
new CORSPlugin()
]
})
Deno.serve(async (request) => {
const { matched, response } = await handler.handle(request, {
prefix: '/rpc',
context: {} // Provide initial context if needed
})
if (matched) {
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.
Client Adapters
Adapter | Target |
---|---|
fetch | MDN Fetch API (Browser, Node, Bun, Deno, Cloudflare Workers, etc.) |
ts
import { RPCLink } from '@orpc/client/fetch'
const link = new RPCLink({
url: 'http://localhost:3000/rpc',
headers: () => ({
'x-api-key': 'my-api-key'
}),
// fetch: <-- polyfill fetch if needed
})
INFO
The link
can be any supported oRPC link, such as RPCLink, OpenAPILink, or another custom handler.
INFO
This only shows how to configure the http link. For full client examples, see Client-Side Clients.