---
title: "tRPC with TypeBox Schemas"
sidebar: "tRPC"
description: "Use TypeBox schemas as tRPC input and output validators with no adapter: typed procedures, client inference, and outputs that never leak a password."
---

# tRPC

tRPC accepts any Standard Schema for `.input()` and `.output()`, so a TypeBox schema built with this package's `Type` works as a validator without an adapter. The client infers its types from the same schemas.

<!-- tabs -->

**bun**

```sh
bun add @trpc/server typebox-standard-schema
```

**npm**

```sh
npm install @trpc/server typebox-standard-schema
```

<!-- /tabs -->

## A complete router

```ts
import { initTRPC } from '@trpc/server'
import { Type } from 'typebox-standard-schema'

const User = Type.Object({
  id: Type.Readonly(Type.Integer()),
  email: Type.String({ format: 'email' }),
  password: Type.Writeonly(Type.String({ minLength: 8 })),
})

const t = initTRPC.create()

export const appRouter = t.router({
  createUser: t.procedure
    .input(Type.Write(User)) // { email, password }: a client-sent id is dropped
    .output(Type.Read(User)) // { id, email }: the password never leaves
    .mutation(({ input }) => db.insertUser(input)),

  userById: t.procedure
    .input(Type.Object({ id: Type.Integer() }))
    .output(Type.Read(User))
    .query(({ input }) => db.findUser(input.id)),
})

export type AppRouter = typeof appRouter
```

`db` stands for your data layer. `insertUser` returns the whole row, password hash included; the output validator checks it against `Type.Read(User)` and strips `password` before it goes on the wire. On the client, `trpc.createUser.mutate()` takes `{ email: string, password: string }` and resolves to `{ id: number, email: string }`.

A cut field that is present is still [checked against its own schema](/typebox-standard-schema/validation.md#pipeline) before it goes. A stored bcrypt hash passes `minLength: 8`, but a `maxLength` or `pattern` written for the plaintext password would reject the row; leave the column out of the row in that case.

## Every issue on the client

tRPC puts only the first issue's message on the wire. To send every issue with its path, for a form that marks each field, copy them from the error's cause in an `errorFormatter`:

```ts
const t = initTRPC.create({
  errorFormatter: ({ shape, error }) => ({
    ...shape,
    data: { ...shape.data, issues: error.code === 'BAD_REQUEST' ? (error.cause as { issues?: unknown } | undefined)?.issues : undefined },
  }),
})
```

Each issue carries `message`, `keyword`, `params` and `path`; see [Issue shape](/typebox-standard-schema/validation.md#issue-shape) for the full shape. The check on `BAD_REQUEST` keeps the `issues` of an error your own code threw from reaching the client.
