> ## Documentation Index
> Fetch the complete documentation index at: https://www.orionjs.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Server Configuration

> Configuring and starting the tRPC server

## Overview

`@orion-js/trpc` provides procedure helpers (queries, mutations, paginated queries), service decorators, and `registerTRPCRoute` to mount your router on Orion HTTP.

## Install Dependencies

```bash theme={null}
pnpm add @orion-js/trpc @trpc/server superjson
```

## Server Setup

```typescript theme={null}
import {initTRPC} from '@trpc/server'
import {
  defaultErrorFormatter,
  registerTRPCRoute,
  type TRPCContext,
} from '@orion-js/trpc'
import type {TRPCRouterRecord} from '@trpc/server'
import {deserialize as superjsonDeserialize, serialize as superjsonSerialize} from 'superjson'

const transformer = {
  serialize: superjsonSerialize,
  deserialize: superjsonDeserialize,
}

const t = initTRPC.context<TRPCContext>().create({
  transformer,
  errorFormatter: defaultErrorFormatter,
})

export function startTrpc<T extends TRPCRouterRecord>(procedures: T) {
  const appRouter = t.router(procedures)

  registerTRPCRoute({appRouter, path: '/trpc'})

  return {router: appRouter}
}
```

## Type Inference Requirement

To get native transformer inference on client types (so `httpBatchLink({transformer})` is accepted), enable `strictNullChecks`:

```json theme={null}
{
  "compilerOptions": {
    "strictNullChecks": true
  }
}
```

## Error Formatting

`@orion-js/trpc` exports `defaultErrorFormatter` which enriches tRPC error shapes with Orionjs-specific data (validation errors, user error codes, permission errors). Use it with your own `initTRPC` instance:

```typescript theme={null}
import {defaultErrorFormatter} from '@orion-js/trpc'

const t = initTRPC.create({
  errorFormatter: defaultErrorFormatter,
})
```

You can also use `mapErrorToTRPCError` and `getErrorData` directly for custom error handling.

## Server-Side Caller

Create a server-side caller for testing or internal use:

```typescript theme={null}
const {router} = startTrpc(procedures)
const caller = t.createCallerFactory(router)({viewer: null})

// Call procedures directly
const example = await caller.getExample({exampleId: '123'})
```
