Setting up Grok (xAI) in n8n
Overview
This guide will help you integrate xAI’s Grok model into your n8n workflows for AI-powered automation.
Prerequisites
- An n8n instance (Cloud or self-hosted)
- xAI API key from x.ai/api
Step 1: Create xAI Credentials
⚠️ Important: Connection Test Issue
If you get a “Couldn’t connect with these settings - Forbidden” error when saving xAI credentials, this is normal! xAI’s API may not support n8n’s connection test. Use the workaround below:
Recommended Method: Use OpenAI Credentials (OpenAI-Compatible)
Since xAI is OpenAI-compatible, this is the most reliable method:
-
In n8n, navigate to:
- Click on Settings (gear icon)
- Select Credentials
- Click Add Credential
-
Search for and select:
- Type “OpenAI” in the search box
- Select OpenAI credentials
-
Configure the credential:
- Credential Name: Give it a descriptive name (e.g., “xAI Grok via OpenAI”)
- API Key: Paste your xAI API key (starts with
xai-) - Base URL: Enter
https://api.x.ai/v1 - Click Save
Alternative Method: Native xAI Credentials (May show connection error)
-
In n8n, navigate to:
- Click on Settings (gear icon)
- Select Credentials
- Click Add Credential
-
Search for and select:
- Type “xAI” in the search box
- Select xAI credentials
-
Configure the credential:
- Credential Name: Give it a descriptive name (e.g., “xAI Grok API”)
- API Key: Paste your xAI API key
- Click Save (ignore the connection error if it appears)
- Test in a real workflow to verify it works
Step 2: Use Grok in Your Workflow
Option A: Using xAI Grok Chat Model (For AI Agent workflows)
The xAI Grok Chat Model is a sub-node that connects to AI Agent or Chain nodes:
-
Add an AI Agent node:
- Drag and drop the AI Agent node into your canvas
- Configure the agent type (Conversational, ReAct, Tools, etc.)
-
Connect the Grok Chat Model:
- In the AI Agent node, look for the Chat Model parameter
- Click to add a sub-node
- Select xAI Grok Chat Model
- Choose your saved xAI credentials
- Configure the model (e.g.,
grok-beta,grok-vision-beta)
Option B: Using OpenAI-Compatible Endpoint
xAI’s API is OpenAI-compatible, so you can also use the OpenAI node:
-
Create OpenAI credentials with custom base URL:
- Add new OpenAI credentials
- Set Base URL:
https://api.x.ai/v1 - Set API Key: Your xAI API key
-
Use the OpenAI node:
- Add an OpenAI node to your workflow
- Select your custom xAI credentials
- Choose the operation (e.g., “Message a Model”, “Create Image”)
Step 3: Example Workflow - Simple Chat with Grok
Here’s a basic example workflow structure:
[Manual Trigger] → [xAI Grok Chat Model within AI Agent] → [Output]
Configuration:
-
Manual Trigger:
- Add input field for user message
-
AI Agent Node:
- Agent Type: Conversational Agent
- Chat Model: xAI Grok Chat Model
- Model:
grok-beta - Temperature: 0.7
- Max Tokens: 2000
- Model:
- System Message: Your custom system prompt
-
Process the response:
- Use the output from the AI Agent for further processing
Available Grok Models
According to xAI’s API:
grok-beta- Standard Grok modelgrok-vision-beta- Grok with vision capabilities
Advanced Use Cases
1. RAG (Retrieval Augmented Generation):
[Trigger] → [Vector Store] → [AI Agent + Grok] → [Response]
2. Multi-step Agent Workflow:
[Trigger] → [AI Agent + Grok + Tools] → [Execute Actions] → [Response]
3. Content Generation:
[Schedule Trigger] → [Get Data] → [Grok via OpenAI Node] → [Post to CMS]
Configuration Parameters
Chat Model Parameters:
- Model: Select the Grok model version
- Temperature: 0.0 (focused) to 2.0 (creative)
- Max Tokens: Maximum response length
- Sampling Temperature: Controls randomness
- Top P: Nucleus sampling parameter
- Frequency Penalty: Reduces repetition
- Presence Penalty: Encourages topic diversity
Tips & Best Practices
- Start with low temperature (0.3-0.7) for factual tasks
- Use higher temperature (0.8-1.5) for creative tasks
- Set appropriate max tokens to control costs
- Implement error handling in your workflows
- Test with Manual Trigger before automating
Troubleshooting
Common Issues:
“Couldn’t connect with these settings - Forbidden” Error:
This error typically occurs when adding xAI credentials. Here’s how to fix it:
-
Double-check your API key:
- Go to console.x.ai or x.ai/api
- Navigate to API Keys section
- Verify you’re copying the complete API key (usually starts with
xai-) - Generate a new API key if needed
-
Important: xAI credentials might not support connection testing
- n8n tries to test credentials when you save them
- xAI’s API might not have a test endpoint
- Workaround: Try saving the credentials anyway and test in an actual workflow
-
Alternative: Use OpenAI credentials instead Since xAI is OpenAI-compatible:
- Create OpenAI credentials in n8n
- Set Base URL:
https://api.x.ai/v1 - Set API Key: Your xAI API key (starts with
xai-) - This often bypasses the connection test issue
-
Verify API key permissions:
- Ensure your xAI account is active
- Check if your API key has the necessary permissions
- Some accounts may have restricted access
-
Check your xAI account status:
- Verify your xAI account has available credits
- Ensure you’ve accepted the terms of service
- Check if there are any account restrictions
Authentication Error:
- Verify your API key is correct
- Ensure the key has not expired
- Check your xAI account has credits
- Make sure you’re using the correct API key format
Rate Limiting:
- Implement retry logic in workflows
- Add delays between requests
- Check your xAI rate limits
Model Not Available:
- Verify the model name is correct
- Check xAI’s documentation for available models
- Ensure your API tier has access to the model
Resources
- n8n Documentation - xAI Grok Chat Model
- xAI API Documentation
- n8n AI Workflows
- xAI Credentials Setup
Next Steps
- Experiment with different Grok models
- Combine with other n8n nodes for powerful automations
- Build AI agents with custom tools
- Implement RAG workflows with vector databases
Last Updated: November 17, 2025