Skip to main content

Zod

You can use Zod to define your API for TypeScript backends.

To declare a data type called ChatRoom with methods messages and send, put the following in a .ts file:

import { z } from "zod/v4";
import { reader, writer } from "@reboot-dev/reboot-api";

export const ChatRoom = {
state: {
messages: z.array(z.string()).default(() => []).meta({ tag: 1 }),
},
methods: {
messages: reader({
request: {},
response: {
messages: z.array(z.string()).meta({ tag: 1 }),
},
}),
send: writer({
request: {
message: z.string().meta({ tag: 1 }),
},
response: z.void(),
}),
},
};

export const api = {
ChatRoom,
};
important

Reboot requires zod/v4.

important

All fields must include a tag in their .meta(), e.g., .meta({ tag: 1 }). This is required for safe backwards compatibility.

Depending on its kind, a method might be able to only read (e.g., reader) or both read and write (e.g., writer) the state.

In the example above, ChatRoom has just one reader and one writer method, but it can have any number of reader, writer, transaction, and workflow methods.

Every transaction also says how it holds the lock on its own state while it runs, with mode: exclusive() or mode: shared(), both imported from @reboot-dev/reboot-api. exclusive() queues concurrent callers of the state and is the choice for a transaction that writes its own state, which is most of them; shared() lets callers proceed concurrently while none of them writes it and is the choice for a transaction that mostly reads its own state while writing others. See Exclusive or shared.

To learn more about how you implement each data type's methods see Implement your API.