Basic API Usage
The simplest way to use ComfyUI from Python is through HTTP requests to the API endpoint.Queuing a Prompt
Here’s a minimal example of how to queue a workflow for execution:basic_api_example.py
To get the API format for your workflow, use File → Export (API) in the ComfyUI interface.
WebSocket API with Image Retrieval
For production use cases where you need to know when execution is complete and retrieve the generated images, use the WebSocket API:websocket_example.py
Using Comfy API Keys
If your workflow uses API nodes that connect to external services, you can provide your Comfy API key:Learn More
Read the full API nodes documentation
API Endpoints
ComfyUI exposes several useful endpoints:POST /prompt
POST /prompt
Queue a workflow for execution.Request body:
GET /history/{prompt_id}
GET /history/{prompt_id}
Retrieve execution history and outputs for a specific prompt.Returns: JSON object with execution details and output file references.
GET /view
GET /view
Download a generated image or file.Query parameters:
filename: Name of the filesubfolder: Subfolder pathtype: File type (e.g., “output”, “temp”)
WebSocket /ws
WebSocket /ws
Real-time execution updates.Query parameters:
clientId: Unique client identifier
executing: Node execution statusprogress: Generation progress updates- Preview images (binary data)
Exporting Workflows
To get the API format of your workflow:1
Design your workflow
Create and test your workflow in the ComfyUI web interface.
2
Export as API
Go to File → Export (API) to get the JSON format.
3
Use in Python
Load the JSON and modify parameters as needed before queuing.
Best Practices
Use WebSockets
Always use WebSocket connections in production to track execution status and handle errors properly.
Handle Errors
Implement proper error handling for network failures and execution errors.
Manage Resources
Close WebSocket connections when done to avoid connection timeouts.
Unique IDs
Always use unique prompt IDs to avoid conflicts with concurrent requests.
Next Steps
Headless Mode
Learn how to run ComfyUI without the web interface
Docker Deployment
Deploy ComfyUI in containers for production