ComfyUI Server API routes: built-in GET, POST, and WebSocket endpoints for prompts, queue, history, models, and user data, plus how to add custom routes.
The server defines a series of get and post methods
which can be found by searching for @routes in server.py. When you submit a workflow
in the web client, it is posted to /prompt which validates the prompt and adds it to an execution queue,
returning either a prompt_id and number (the position in the queue), or error and node_errors if validation fails.
The prompt queue is defined in execution.py, which also defines the PromptExecutor class.
If you want to send a message from the client to the server during execution, you will need to add a custom route to the server.
For anything complicated, you will need to dive into the aiohttp framework docs, but most cases can
be handled as follows:
from server import PromptServerfrom aiohttp import webroutes = PromptServer.instance.routes@routes.post('/my_new_path')async def my_function(request): the_data = await request.post() # the_data now holds a dictionary of the values sent MyClass.handle_my_message(the_data) return web.json_response({})
Unless you know what you are doing, don’t try to define my_function within a class.
The @routes.post decorator does a lot of work! Instead, define the function as above
and then call a classmethod.
You can also define a @routes.get if you aren’t changing anything.
The client can use this new route by sending a FormData object with code something like this,
which would result in the_data, in the above code, containing message and node_id keys:
import { api } from "../../scripts/api.js";function send_message(node_id, message) { const body = new FormData(); body.append('message',message); body.append('node_id', node_id); api.fetchApi("/my_new_path", { method: "POST", body, });}