Skip to content

Commit 0bae87a

Browse files
committed
feat: Add support for CIVITAI_TOKEN and HF_TOKEN environment variables
Closes #280. Adds support for the standardized HuggingFace and CivitAI API token environment variables. This provides a safe and easy way to provide transient secrets without leaking them to disk (nothing gets written to the config). It also makes it much easier to integrate Comfy-CLI with scripting, since the user no longer has to infinitely provide the "--set-X-token" flags every single time they run the command anymore (which they had to do in the past, just to be sure that the latest token was always used). The order of priority is `--set-X-token`, then the environment variables (if they exist), and lastly the static config fallback values. This new feature is now documented in the README and in various error messages. The code has also been refactored, and the error messages have been improved for clarity and consistency.
1 parent d63ce4f commit 0bae87a

3 files changed

Lines changed: 11 additions & 18 deletions

File tree

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,11 @@ the bisect tool can help you pinpoint the custom node that causes the issue.
173173

174174
- Model downloading
175175

176-
`comfy model download --url <URL> ?[--relative-path <PATH>] ?[--set-civitai-api-token <TOKEN>]`
176+
`comfy model download --url <URL> ?[--relative-path <PATH>] ?[--set-civitai-api-token <TOKEN>] ?[--set-hf-api-token <TOKEN>]`
177177

178-
- URL: CivitAI, huggingface file url, ...
178+
- URL: CivitAI, HuggingFace file url, etc...
179+
- You can also specify your API tokens via the `CIVITAI_TOKEN` and `HF_TOKEN` environment variables. The order of priority is `--set-X-token` (always highest priority), then the environment variables if they exist, and lastly your config's stored tokens from previous `--set-X-token` usage (which remembers your most recently set token values).
180+
- Tokens provided via the environment variables are never stored persistently in your config file. They are intended as a way to easily and safely provide transient secrets.
179181

180182
- Model remove
181183

comfy_cli/command/models/models.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def download(
163163
_ctx: typer.Context,
164164
url: Annotated[
165165
str,
166-
typer.Option(help="The URL from which to download the model", show_default=False),
166+
typer.Option(help="The URL from which to download the model.", show_default=False),
167167
],
168168
relative_path: Annotated[
169169
Optional[str],
@@ -183,15 +183,15 @@ def download(
183183
Optional[str],
184184
typer.Option(
185185
"--set-civitai-api-token",
186-
help="Set the CivitAI API token to use for model listing.",
186+
help="Set the CivitAI API token to use for model downloading.",
187187
show_default=False,
188188
),
189189
] = None,
190190
set_hf_api_token: Annotated[
191191
Optional[str],
192192
typer.Option(
193193
"--set-hf-api-token",
194-
help="Set the HuggingFace API token to use for model listing.",
194+
help="Set the HuggingFace API token to use for model downloading.",
195195
show_default=False,
196196
),
197197
] = None,
@@ -202,17 +202,8 @@ def download(
202202
local_filename = None
203203
headers = None
204204

205-
if set_civitai_api_token is not None:
206-
config_manager.set(constants.CIVITAI_API_TOKEN_KEY, set_civitai_api_token)
207-
civitai_api_token = set_civitai_api_token
208-
else:
209-
civitai_api_token = config_manager.get(constants.CIVITAI_API_TOKEN_KEY)
210-
211-
if set_hf_api_token is not None:
212-
config_manager.set(constants.HF_API_TOKEN_KEY, set_hf_api_token)
213-
hf_api_token = set_hf_api_token
214-
else:
215-
hf_api_token = config_manager.get(constants.HF_API_TOKEN_KEY)
205+
civitai_api_token = config_manager.get_or_override("CIVITAI_TOKEN", constants.CIVITAI_API_TOKEN_KEY, set_civitai_api_token)
206+
hf_api_token = config_manager.get_or_override("HF_TOKEN", constants.HF_API_TOKEN_KEY, set_hf_api_token)
216207

217208
is_civitai_model_url, is_civitai_api_url, model_id, version_id = check_civitai_url(url)
218209
is_huggingface_url, repo_id, hf_filename, hf_folder_name, hf_branch_name = check_huggingface_url(url)
@@ -281,7 +272,7 @@ def download(
281272
if is_huggingface_url and check_unauthorized(url, headers):
282273
if hf_api_token is None:
283274
print(
284-
"Unauthorized access to Hugging Face model. Please set the HuggingFace API token using --set-hf-api-token"
275+
"Unauthorized access to Hugging Face model. Please set the Hugging Face API token using `comfy model download --set-hf-api-token` or via the `HF_TOKEN` environment variable"
285276
)
286277
return
287278
else:

comfy_cli/file_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def parse_json(input_data):
3636
msg_json = parse_json(message)
3737
if msg_json is not None:
3838
if "message" in msg_json:
39-
return f"Unauthorized download ({status_code}).\n{msg_json['message']}\nor you can set civitai api token using `comfy model download --set-civitai-api-token <token>`"
39+
return f"Unauthorized download ({status_code}).\n{msg_json['message']}\nor you can set a Civitai API token using `comfy model download --set-civitai-api-token` or via the `CIVITAI_TOKEN` environment variable"
4040
return f"Unauthorized download ({status_code}), you might need to manually log into a browser to download this"
4141
elif status_code == 403:
4242
return f"Forbidden url ({status_code}), you might need to manually log into a browser to download this"

0 commit comments

Comments
 (0)