ComfyUI/script_examples
2026-06-08 18:07:57 +08:00
..
basic_api_example.py Update comment in api example. (#9708) 2025-09-03 18:43:29 -04:00
README.md docs: add API entry points for local script examples 2026-06-08 18:07:57 +08:00
websockets_api_example_ws_images.py Add ws.close() to the websocket examples (#5020) 2024-09-22 04:59:10 -04:00
websockets_api_example.py Allow the prompt request to specify the prompt ID. (#8189) 2025-07-14 14:48:31 -04:00

API Script Examples

This folder contains small Python examples for driving a local ComfyUI server from code.

Files

  • basic_api_example.py: submits a workflow exported from File -> Export (API).
  • websockets_api_example.py: submits a workflow, waits for completion over websocket, and then fetches generated images from history.
  • websockets_api_example_ws_images.py: streams image bytes directly through the websocket with SaveImageWebsocket instead of saving files to disk first.

What These Examples Use

These scripts target the local scripting routes exposed by server.py:

  • POST /prompt
  • GET /history/{prompt_id}
  • GET /queue
  • GET /view
  • GET /ws

For the newer documented REST API, see openapi.yaml. In particular:

  • POST /api/prompt
  • GET /api/queue
  • GET /api/jobs/{job_id}

Usage Notes

  1. Start ComfyUI locally, which defaults to 127.0.0.1:8188.
  2. Load or create a workflow in the UI.
  3. Export the workflow with File -> Export (API).
  4. Replace the sample prompt_text in one of these scripts with the exported JSON if needed.
  5. Make sure the referenced models and nodes are available in your local ComfyUI install.

Dependencies

  • basic_api_example.py uses only the Python standard library.
  • The websocket examples also require websocket-client.

Install it with:

pip install websocket-client