Python backend
Build a Reboot backend in Python, one file at a time. This guide covers the backend only; add a frontend with the React guide.
Prefer to have an agent do it? See Build with Claude Code or Build with Codex.
- Set up your app foldermkdir reboot-chat-room && cd reboot-chat-room
- Install Rebootpython3 -m venv ./.venv && source .venv/bin/activate && pip install reboot pydantic
- Make sure Docker is runningdocker ps
- Create a Pydantic
.pyfilemkdir -p ./api/chat_room/v1 && touch ./api/chat_room/v1/chat_room.py - Define your durable state data types and operations# ./api/chat_room/v1/chat_room.pyfrom reboot.api import (API,Field,Methods,Reader,Model,Writer,Type,)from typing import Optionalclass ChatRoomState(Model):messages: Optional[list[str]] = Field(tag=1)class MessagesResponse(Model):messages: list[str] = Field(tag=1)class SendRequest(Model):message: str = Field(tag=1)ChatRoomMethods = Methods(messages=Reader(request=None,response=MessagesResponse,description="Every message posted to the room so far.",mcp=None,),send=Writer(request=SendRequest,response=None,description="Post one message to the room.",mcp=None,),)api = API(ChatRoom=Type(state=ChatRoomState,methods=ChatRoomMethods,),)
- Create
.pyimplementation filemkdir -p ./backend/src && touch ./backend/src/chat_room_servicer.py - Implement
ChatRoomServicer# ./backend/src/chat_room_servicer.pyfrom chat_room.v1.chat_room import (MessagesResponse,SendRequest,)from chat_room.v1.chat_room_rbt import ChatRoomfrom reboot.aio.contexts import ReaderContext, WriterContextclass ChatRoomServicer(ChatRoom.Servicer):async def messages(self,context: ReaderContext,) -> MessagesResponse:return MessagesResponse(messages=self.state.messages or [])async def send(self,context: WriterContext,request: SendRequest,) -> None:if not self.state.messages:self.state.messages = []self.state.messages.append(request.message) - Create a
mainentrypointtouch ./backend/src/main.py - Implement
main.py# ./backend/src/main.pyimport asynciofrom chat_room.v1.chat_room_rbt import ChatRoomfrom chat_room_servicer import ChatRoomServicerfrom reboot.aio.applications import Applicationfrom reboot.aio.external import InitializeContextasync def initialize(context: InitializeContext):chat_room = ChatRoom.ref('reboot-chat-room')await chat_room.send(context,message='Hello, World!',)async def main():application = Application(servicers=[ChatRoomServicer],initialize=initialize,)await application.run()if __name__ == '__main__':asyncio.run(main()) - Create an
.rbtrcfiletouch .rbtrc - Add necessary flags to your
.rbtrcfilegenerate api/generate --python=backend/api/dev run --application-name=chat-roomdev run --watch=backend/src/**/*.pydev run --pythondev run --application=backend/src/main.py - Run your apprbt dev run
- Query your appcurl -XPOST http://localhost:9991/chat_room.v1.ChatRoomMethods/Messages \-H "x-reboot-state-ref:chat_room.v1.ChatRoom:reboot-chat-room"
Next steps
Well done! You made it!
This example is somewhat contrived: it creates just one instance of
your ChatRoom 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:
- Build a React frontend - create a React frontend that connects to your backend and displays your messages in real-time.
- Add signed-in users - declare a
Usertype and passoauth=toApplication; Reboot runs the sign-in flow and auto-constructs aUserfor each person. - Learn about transactions - so far,
you've seen
readerandwritermethods, but Reboot's power extends totransactions. Learn abouttransactionsand all of the other method kinds here. - Join us on Discord - to ask questions and let us know what you think!
Hopefully, your wheels are starting to spin thinking of all of the possibilities!