/orpc:orpc-contractfrom orpc
Designs oRPC v2 APIs contract-first: defines API shape with `oc` from `@orpc/contract`, implements with `implement` from `@orpc/server`, and consumes from typesafe clients. Use when separating server/client packages, generating contracts from OpenAPI specs, or publishing typed SDKs to npm.
Ships in orpc. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Contract-first oRPC splits an API into two artifacts: a contract (schemas, errors, metadata, no business logic) defined with oc from @orpc/contract, and an implementation built from it with implement from @orpc/server. Server and client both depend on the contract, never on each other, so the API shape can live in its own package, be reviewed on its own, and ship to consumers as a typed SDK. Prefer it when server and client are separate packages or teams, when the shape starts from an existing OpenAPI spec, or when publishing a client to npm. Stay with the os-first flow when one codebase holds both sides and the client can import the router type directly; converting later is cheap because a plain router already works as a router contract (resolve lazy routers with unlazyRouter first).
This skill targets oRPC v2. The orpc skill carries the v2 install and version check plus core builder, middleware, serving, and client concepts. Pretrained oRPC knowledge describes v1 and is often wrong for v2: when unsure of any API below, fetch its docs page first (see Full documentation).
Every chain is optional and each call returns a new instance, so share base contracts freely. A contract has no .handler. Zod, Valibot, ArkType, and any other Standard Schema library work.
import { oc } from '@orpc/contract'
import * as z from 'zod'
export const contract = {
planet: {
list: oc.output(z.array(z.object({ id: z.number(), name: z.string() }))),
find: oc
.errors({ NOT_FOUND: {} })
.input(z.object({ id: z.number() }))
.output(z.object({ id: z.number(), name: z.string() })),
},
}
.output: without it clients infer the output as unknown.then, bind, valueOf, toString, toJSON.oc.meta(someMeta).router({...})..errors({ NOT_FOUND: {} }) declares typesafe errors; implementations throw them via errors.NOT_FOUND() and clients infer their shapes..input/.output calls stack schemas instead of replacing them: object input schemas compose into one flat value, output schemas pipe. Use this to extend a base contract without repeating fields.type utility from @orpc/contract: oc.input(type<{ value: number }>()), optionally with a mapping function as type<Input, Output>(fn)..meta(openapi({ method: 'GET', path: '/planets/{id}' })) from @orpc/openapi, exactly as on os; routing rules, prefix, and spec generation belong to the orpc-openapi skill.Infer types with InferRouterContractInputs, InferRouterContractOutputs, and InferRouterContractErrors from @orpc/contract.
implement turns the contract into an implementer that mirrors its shape and type-checks every handler; .router also enforces the contract at runtime.
import { implement } from '@orpc/server'
const implementer = implement(contract).$context<{ db: DB }>()
const listPlanets = implementer.planet.list.handler(async ({ context }) => context.db.list())
const findPlanet = implementer.planet.find.handler(async ({ input, context, errors }) => {
const planet = await context.db.find(input.id)
if (!planet) throw errors.NOT_FOUND()
return planet
})
export const router = implementer.router({
planet: { list: listPlanets, find: findPlanet },
})
.$context declares the initial context the procedures require, as on os..use(mw) before .handler. That runs after input validation (the contract already registered .input); to wrap validation, apply it at router level: implementer.use(mw) for every procedure, or implementer.planet.use(mw).list for a subtree. Router-level plus procedure-level .use can run the same middleware twice; use the dedupe pattern from the orpc skill.implementer.middleware(fn) creates middleware that infers the contract's typesafe errors. When not every procedure defines a code, guard with the in operator: if ('TOO_MANY_REQUESTS' in errors) throw errors.TOO_MANY_REQUESTS(). Any type-compatible middleware also works.RPCHandler (orpc skill) or OpenAPIHandler (orpc-openapi skill), call it in-process with call or createRouterClient from @orpc/server.RPCLink needs only the contract type; OpenAPILink takes the contract as a runtime value to read each procedure's route. Get the client types exactly right:
import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
import type { RouterContractClient } from '@orpc/contract'
import type { JsonifiedClient } from '@orpc/openapi'
import { OpenAPILink } from '@orpc/openapi/fetch'
// RPC protocol (server side is RPCHandler)
const rpcLink = new RPCLink({ origin: 'https://api.example.com', url: '/rpc' })
const client: RouterContractClient<typeof contract> = createORPCClient(rpcLink)
// OpenAPI protocol (OpenAPIHandler or any spec-compliant server)
const openapiLink = new OpenAPILink(contract, { origin: 'https://api.example.com', url: '/api' })
const apiClient: JsonifiedClient<RouterContractClient<typeof contract>> =
createORPCClient(openapiLink)
RouterClient<typeof router> from @orpc/server in the same positions.JsonifiedClient is required over OpenAPILink because OpenAPI serialization is one-way (a Date returns as a string); dropping it via Smart Coercion, plus OpenAPILink options and CORS caveats, are in the orpc-openapi skill.RouterContractClient<typeof contract, ClientContext>, then client.planet.find(input, { context: { token } }); link options like headers accept functions of that context.RouterContractClient<typeof contract> as a type from the server package so clients never import the contract module itself (still needed as a runtime value for OpenAPILink; ship the minified JSON below)..meta(meta.path([...])) and build per-procedure clients with createContractClientFactory from @orpc/contract (createContractJsonifiedClientFactory from @orpc/openapi when the link needs JsonifiedClient); fetch contract/client-factory before adopting it.When the contract is derived from a router, importing it on the client is heavy and may expose internals. Minify and export JSON instead:
import fs from 'node:fs'
import { minifyRouterContract } from '@orpc/contract'
import { unlazyRouter } from '@orpc/server'
const minified = minifyRouterContract(await unlazyRouter(router))
fs.writeFileSync('./contract.json', JSON.stringify(minified))
minifyRouterContract keeps only client-needed metadata. On the client, import the JSON and cast, since schemas do not survive serialization: new OpenAPILink(contract as typeof router, ...).
To publish a typed SDK to npm, export a factory that pairs the contract with a link:
import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
import type { RouterContractClient } from '@orpc/contract'
export function createMyApi(apiKey: string): RouterContractClient<typeof contract> {
const link = new RPCLink({
origin: 'https://example.com',
url: '/rpc',
headers: { 'x-api-key': apiKey },
})
return createORPCClient(link)
}
Bundle with tsdown --dts src/index.ts, point exports at dist types plus import entries, list @orpc/client and @orpc/contract as dependencies, and publish. Consumers get a fully typed client that works with every oRPC client integration (TanStack Query included). Fetch recipes/publish-client-to-npm for the complete package.json.
Use Hey API's orpc plugin instead of hand-writing the contract. Install @hey-api/openapi-ts@next as a dev dependency (oRPC v2 output requires the next tag until the next stable Hey API release) and create openapi-ts.config.ts:
import { defineConfig } from '@hey-api/openapi-ts'
export default defineConfig({
input: 'https://example.com/openapi.json', // local file or URL
output: 'src/contract',
plugins: [{ name: 'orpc', compatibilityVersion: '2', validator: 'zod' }],
})
Then run npx @hey-api/openapi-ts. It writes orpc.gen.ts (one procedure contract per operation, routed via .meta(openapi({...})) with inputStructure: 'detailed', plus a combined contract router) and zod.gen.ts. The generated files import @orpc/contract, @orpc/openapi, and zod, so install those too. From there, implement the contract on your own server, or point OpenAPILink at the existing spec-compliant server.
If this skill and a fetched docs page disagree, trust the page: this skill is a summary and v2 is still moving. The docs are served at https://orpc.dev (the v1 docs stay at https://v1.orpc.dev):
.md to any docs URL for that page's exact source markdown (for example https://orpc.dev/docs/contract/procedure.md)Pages to fetch when you need details beyond this skill:
contract/procedure, contract/router, contract/implementation, contract/generate-from-openapiclient/client-side, client/server-side, client/error-handling, openapi/linkrecipes/publish-client-to-npm, contract/client-factory, recipes/monorepo-setupWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub pleaseai/claude-code-plugins --plugin orpc4 skills in this plugin
/orpcBuild, serve, and call end-to-end typesafe APIs with oRPC v2. Use for any task in a project that depends on `@orpc/*` packages, even a one-procedure change: defining procedures with the os builder (.input/.output/.handler, any Standard Schema validator), assembling routers, middleware and context, typesafe errors with ORPCError, serving via RPCHandler on any runtime adapter, calling from server-side clients (call, createRouterClient) or client-side clients (createORPCClient with RPCLink), integrating TanStack Query, or streaming over SSE. Pretrained oRPC knowledge describes v1 and is wrong for v2, so load this skill even when the change looks trivial. Biases toward retrieval from the oRPC docs over pre-trained knowledge. For REST/OpenAPI exposure, prefer the orpc-openapi skill; for contract-first design, the orpc-contract skill; for tRPC or oRPC v1 migrations, the orpc-migrate skill./orpc-migrateMigrate existing codebases to current oRPC, covering tRPC to oRPC (incremental wrapping via the @orpc/trpc integration or a full rewrite with the concept mapping) and oRPC v1 to v2 (package renames, breaking changes, and a safe order of operations). Use when asked to migrate from tRPC to oRPC, convert or wrap a tRPC router, upgrade oRPC v1 to v2, fix oRPC v2 breaking changes, or swap `@trpc/*` packages for `@orpc/*` equivalents. Biases toward retrieval from the oRPC docs over pre-trained knowledge. Not for greenfield oRPC work or new features in an already-migrated codebase: use the orpc skill for those.Other plugins' skills for the same job