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:

Since xAI is OpenAI-compatible, this is the most reliable method:

  1. In n8n, navigate to:

    • Click on Settings (gear icon)
    • Select Credentials
    • Click Add Credential
  2. Search for and select:

    • Type “OpenAI” in the search box
    • Select OpenAI credentials
  3. 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)

  1. In n8n, navigate to:

    • Click on Settings (gear icon)
    • Select Credentials
    • Click Add Credential
  2. Search for and select:

    • Type “xAI” in the search box
    • Select xAI credentials
  3. 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:

  1. Add an AI Agent node:

    • Drag and drop the AI Agent node into your canvas
    • Configure the agent type (Conversational, ReAct, Tools, etc.)
  2. 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:

  1. 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
  2. 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:

  1. Manual Trigger:

    • Add input field for user message
  2. AI Agent Node:

    • Agent Type: Conversational Agent
    • Chat Model: xAI Grok Chat Model
      • Model: grok-beta
      • Temperature: 0.7
      • Max Tokens: 2000
    • System Message: Your custom system prompt
  3. 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 model
  • grok-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

  1. Start with low temperature (0.3-0.7) for factual tasks
  2. Use higher temperature (0.8-1.5) for creative tasks
  3. Set appropriate max tokens to control costs
  4. Implement error handling in your workflows
  5. 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:

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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

Next Steps

  1. Experiment with different Grok models
  2. Combine with other n8n nodes for powerful automations
  3. Build AI agents with custom tools
  4. Implement RAG workflows with vector databases

Last Updated: November 17, 2025