Skip to main content

TypeScript

  1. Setup your app folder
    mkdir reboot-hello && cd reboot-hello
  2. Install Reboot
    npm init -y && npm pkg set type="module" && npm install @reboot-dev/reboot && npx tsc --init --sourceMap --declaration --moduleResolution node --target es2020 --rootDir . --outDir dist
  3. Define your API using Zod
    mkdir -p ./api/hello/v1 && touch ./api/hello/v1/hello.ts
  4. Define your durable state data types and operations
    // ./api/hello/v1/hello.ts
    import { z } from "zod/v4";

    export const api = {
    Hello: {
    state: {
    messages: z.array(z.string()).default(() => []).meta({ tag: 1 }),
    },

    methods: {
    messages: {
    kind: "reader",
    request: {},
    response: {
    messages: z.array(z.string()).meta({ tag: 1 }),
    },
    },
    send: {
    kind: "writer",
    request: {
    message: z.string().meta({ tag: 1 }),
    },
    response: {},
    },
    },
    },
    };
  5. Create .ts implementation file
    mkdir -p ./backend/src && touch ./backend/src/hello_servicer.ts
  6. Implement HelloServicer
    import { ReaderContext, WriterContext } from "@reboot-dev/reboot";
    import { Hello } from "../../api/hello/v1/hello_rbt.js";

    export const helloServicer = Hello.servicer({
    messages: async (
    context: ReaderContext,
    state: Hello.State,
    request: Hello.MessagesRequest
    ): Promise<Hello.MessagesResponse> => {
    return { messages: state.messages };
    },

    send: async (
    context: WriterContext,
    state: Hello.State,
    request: Hello.SendRequest
    ): Promise<[Hello.State, Hello.SendResponse]> => {
    state.messages.push(request.message);
    return [state, {}];
    },
    });
  7. Create a main entrypoint
    touch ./backend/src/main.ts
  8. Implement main.ts
    import { Application, InitializeContext } from "@reboot-dev/reboot";
    import { Hello } from "../../api/hello/v1/hello_rbt.js";
    import { helloServicer } from "./hello_servicer.js";

    const initialize = async (context: InitializeContext) => {
    const hello = Hello.ref("reboot-hello");

    await hello.send(context, { message: "Hello, World!" });
    };

    new Application({
    servicers: [helloServicer],
    initialize,
    }).run();
  9. Create an .rbtrc file
    touch .rbtrc
  10. Add necessary flags to your .rbtrc file
    # .rbtrc
    generate api/
    generate --nodejs=api/

    dev run --name=hello
    dev run --watch=backend/src/**/*.ts
    dev run --nodejs
    dev run --application=backend/src/main.ts
  11. Run your app
    npx rbt dev run
  12. Query your app
    curl -XPOST http://localhost:9991/hello.v1.HelloMethods/Messages \
    -H "x-reboot-state-ref:hello.v1.Hello:reboot-hello"

Next steps

Well done! You made it!

This example is somewhat contrived as we only created one instance of your Hello data type, but in practice you are free to make as many of these as you like!

Check out these links to continue your exploration:

Hopefully, your wheels are starting to spin thinking of all of the possibilities!