feat: Support xAI API key for Grok nodes#13294
feat: Support xAI API key for Grok nodes#13294austin1997 wants to merge 5 commits intoComfy-Org:masterfrom
Conversation
This adds an optional `xai_api_key` input to all Grok image and video nodes. When provided, requests bypass the Comfy proxy and directly call the official xAI endpoints, allowing users to use their own keys and bypass Comfy org limits. Co-authored-by: austin1997 <18709560+austin1997@users.noreply.github.com>
- Strips leading/trailing whitespace - Removes accidental `Bearer ` prefixes from pasted keys This prevents xAI API rejection errors such as "Incorrect API key provided" when the key format is slightly off. Co-authored-by: austin1997 <18709560+austin1997@users.noreply.github.com>
- Strips leading/trailing whitespace - Removes accidental `Bearer ` prefixes from pasted keys This prevents xAI API rejection errors such as "Incorrect API key provided" when the key format is slightly off. Co-authored-by: austin1997 <18709560+austin1997@users.noreply.github.com>
…4855872412057 feat: add official xAI API key support to Grok nodes
📝 WalkthroughWalkthroughThe PR adds an optional 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
comfy_api_nodes/nodes_grok.py (2)
675-678: Clean up developer deliberation comments.These lines read like internal thought process rather than documentation. The first sentence is useful context, but the "Wait, if they are providing their own key..." part is confusing. Consider simplifying:
✏️ Suggested cleanup
- # We must use proxy to upload images temporarily even if they provide their own key for video generation - # because the API requires URLs and we use our proxy for image hosting during the request. - # Wait, if they are providing their own key to our backend for generation, - # `upload_images_to_comfyapi` relies on `comfyapi`. This is fine. + # Image uploads always use Comfy proxy for temporary hosting since the xAI API requires URLs.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@comfy_api_nodes/nodes_grok.py` around lines 675 - 678, Remove the internal deliberation style comments around the image proxy explanation in nodes_grok.py and replace them with a concise, clear comment: keep the first sentence that explains we must use the proxy to upload images temporarily for API URL requirements, remove the "Wait, if they are providing their own key..." conversational lines, and optionally add a short clarifying note that upload_images_to_comfyapi uses the comfyapi proxy for hosting during the request; ensure the comment is factual and concise.
144-152: Consider extracting API key handling into a reusable helper.This exact sanitization and path/header setup pattern is duplicated across all 6 Grok nodes. Extracting it would improve maintainability:
💡 Proposed helper function
def _prepare_xai_request(xai_api_key: str, proxy_path: str, direct_path: str) -> tuple[str, dict | None]: """Sanitize API key and return (path, headers) for xAI requests.""" xai_api_key = xai_api_key.strip() if xai_api_key.lower().startswith("bearer "): xai_api_key = xai_api_key[7:].strip() if xai_api_key: return direct_path, {"Authorization": f"Bearer {xai_api_key}"} return proxy_path, NoneUsage would become:
path, headers = _prepare_xai_request( xai_api_key, "/proxy/xai/v1/images/generations", "https://api.x.ai/v1/images/generations" )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@comfy_api_nodes/nodes_grok.py` around lines 144 - 152, Duplicate xAI API key sanitization and path/header selection (the xai_api_key.strip(), bearer-prefix removal, and setting path/headers) appears across all Grok nodes; factor it into a reusable helper (e.g., _prepare_xai_request(xai_api_key: str, proxy_path: str, direct_path: str) -> tuple[str, dict|None]) that performs the strip + "bearer " removal and returns (path, headers). Replace the repeated block that sets path and headers in each Grok node with a call to _prepare_xai_request(...), passing the existing proxy path "/proxy/xai/v1/images/generations" and direct "https://api.x.ai/v1/images/generations" (and analogous paths for other endpoints), and ensure callers use the returned path and headers variables (preserving current variable names xai_api_key handling is internal to the helper).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@comfy_api_nodes/nodes_grok.py`:
- Around line 675-678: Remove the internal deliberation style comments around
the image proxy explanation in nodes_grok.py and replace them with a concise,
clear comment: keep the first sentence that explains we must use the proxy to
upload images temporarily for API URL requirements, remove the "Wait, if they
are providing their own key..." conversational lines, and optionally add a short
clarifying note that upload_images_to_comfyapi uses the comfyapi proxy for
hosting during the request; ensure the comment is factual and concise.
- Around line 144-152: Duplicate xAI API key sanitization and path/header
selection (the xai_api_key.strip(), bearer-prefix removal, and setting
path/headers) appears across all Grok nodes; factor it into a reusable helper
(e.g., _prepare_xai_request(xai_api_key: str, proxy_path: str, direct_path: str)
-> tuple[str, dict|None]) that performs the strip + "bearer " removal and
returns (path, headers). Replace the repeated block that sets path and headers
in each Grok node with a call to _prepare_xai_request(...), passing the existing
proxy path "/proxy/xai/v1/images/generations" and direct
"https://api.x.ai/v1/images/generations" (and analogous paths for other
endpoints), and ensure callers use the returned path and headers variables
(preserving current variable names xai_api_key handling is internal to the
helper).
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 0fb4e18c-152c-4f89-bf0c-076988655ac6
📒 Files selected for processing (1)
comfy_api_nodes/nodes_grok.py
Add optional xAI API key for Grok API nodes. Allowing users to provide existing xAI API keys.
API Node PR Checklist
Scope
Pricing & Billing
If Need pricing update:
QA
Comms