Skip to content
oRPC
Esc
navigateopen⌘Jpreview
On this page

Response Headers Plugin

Use ResponseHeadersHandlerPlugin to accumulate response headers in context.resHeaders and merge them into the final response.

Context Access

import type { ResponseHeadersHandlerPluginContext } from '@orpc/server/plugins'

interface ServerContext extends ResponseHeadersHandlerPluginContext {}

const const base: Builder<ServerContext & object, Record<never, never>>base = const os: Builder<DefaultInitialContext & object, Record<never, never>>
The oRPC procedure builder. Chain methods like `.input`, `.use`, and `.handler` to define procedures, then compose them into routers.
@see{@link https://orpc.dev/docs/procedure Procedure}
os
.Builder<DefaultInitialContext & object, Record<never, never>>.$context<ServerContext>(): Builder<ServerContext & object, Record<never, never>>$context<ServerContext>()
const const procedure: DecoratedProcedure<ServerContext & object, object, InitialInputSchema, Schema<void>, Record<never, never>, never>procedure = const base: Builder<ServerContext & object, Record<never, never>>base .Builder<ServerContext & object, Record<never, never>>.use<object, ServerContext & object, Record<never, never>>(middleware: Middleware<ServerContext & object, object, unknown, unknown, Record<never, never>>): BuilderWithMiddlewares<ServerContext & object, object, Record<never, never>>use(({ context: ServerContext & objectcontext, next: MiddlewareNext<unknown>
Invoke to continue the middleware chain.
next
}) => {
context: ServerContext & objectcontext.ResponseHeadersHandlerPluginContext.resHeaders?: Headers | undefined
Response headers as a Headers instance. This is injected by the Response Headers Plugin. When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.
resHeaders
?.Headers.set(name: string, value: string): void
The **`set()`** method of the Headers interface sets a new value for an existing header inside a Headers object, or adds the header if it does not already exist. [MDN Reference](https://developer.mozilla.org/docs/Web/API/Headers/set)
set
('x-request-id', 'req_123')
return
next: MiddlewareNext
<object>(options?: {
    context?: object | undefined;
} | undefined) => MiddlewareResult<object, unknown>
Invoke to continue the middleware chain.
next
()
}) .BuilderWithMiddlewares<ServerContext & object, object, Record<never, never>>['handler']<void>(handler: ProcedureHandler<ServerContext & object, unknown, void, ORPCErrorConstructorMap<Record<never, never>>>): DecoratedProcedure<ServerContext & object, object, InitialInputSchema, Schema<void>, Record<never, never>, never>handler(({ context: ServerContext & objectcontext }) => { function setCookie(headers: Headers | undefined, name: string, value: string, options?: SetCookieOptions): void
Sets a cookie in the response headers. Does nothing if `headers` is `undefined`.
@example```ts const headers = new Headers() setCookie(headers, 'sessionId', 'abc123', { httpOnly: true, maxAge: 3600 }) expect(headers.get('Set-Cookie')).toBe('sessionId=abc123; Max-Age=3600; Path=/; HttpOnly') ```@see{@link https://orpc.dev/docs/helpers/cookie Cookie Helpers}
setCookie
(context: ServerContext & objectcontext.ResponseHeadersHandlerPluginContext.resHeaders?: Headers | undefined
Response headers as a Headers instance. This is injected by the Response Headers Plugin. When set before the response is sent, these headers will be included in the response. If not set, no additional headers will be added.
resHeaders
, 'session_id', 'abc123', {
secure?: boolean | undefined
Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5). When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
secure
: true,
maxAge?: number | undefined
Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2). The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this, so if both are set, they should point to the same date and time.
maxAge
: 3600
}) })

Handler Setup

import { ResponseHeadersHandlerPlugin } from '@orpc/server/plugins'

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

Learn More

For implementation details, see the source code.

Last updated on August 6, 2026

Was this page helpful?