Skip to main content
ComfyUI provides a REST API that allows you to programmatically queue prompts and retrieve generated images. This guide shows you how to use ComfyUI from Python applications.

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
Generate your API key at platform.comfy.org and keep it secure.

API Endpoints

ComfyUI exposes several useful endpoints:
Queue a workflow for execution.Request body:
Retrieve execution history and outputs for a specific prompt.Returns: JSON object with execution details and output file references.
Download a generated image or file.Query parameters:
  • filename: Name of the file
  • subfolder: Subfolder path
  • type: File type (e.g., “output”, “temp”)
Real-time execution updates.Query parameters:
  • clientId: Unique client identifier
Events:
  • executing: Node execution status
  • progress: 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