Loading...
Loading...
xAI Grok API authentication and setup. Use when configuring xAI API access, setting up API keys, or troubleshooting authentication issues.
npx skill4agent add adaptationio/skrillz xai-authxai-# Add to .bashrc, .zshrc, or .env
export XAI_API_KEY="xai-your-key-here"curl https://api.x.ai/v1/models \
-H "Authorization: Bearer $XAI_API_KEY"import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1"
)from openai import OpenAI
client = OpenAI(
api_key="xai-your-key-here",
base_url="https://api.x.ai/v1"
)from xai_sdk import Client
client = Client(api_key=os.getenv("XAI_API_KEY"))# Just change base_url - everything else works the same
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1" # Only difference
)
response = client.chat.completions.create(
model="grok-3-fast", # Use Grok models
messages=[{"role": "user", "content": "Hello!"}]
)| Action | Cost |
|---|---|
| Grok 4.1 Fast (input) | $0.20/1M tokens |
| Grok 4.1 Fast (output) | $0.50/1M tokens |
| X Search / Web Search | $5/1,000 calls |
| Code Execution | $5/1,000 calls |
.env.xai# xAI API Configuration
XAI_API_KEY=xai-your-key-here
XAI_BASE_URL=https://api.x.ai/v1
XAI_DEFAULT_MODEL=grok-3-fast| Error | Cause | Solution |
|---|---|---|
| No credits on account | Add credits or enable free tier |
| Wrong or expired key | Generate new key at console.x.ai |
| Too many requests | Implement backoff, reduce frequency |
| Invalid model name | Check available models |
from openai import OpenAI, APIError, RateLimitError
client = OpenAI(
api_key=os.getenv("XAI_API_KEY"),
base_url="https://api.x.ai/v1"
)
try:
response = client.chat.completions.create(
model="grok-3-fast",
messages=[{"role": "user", "content": "Hello"}]
)
except RateLimitError:
print("Rate limit hit, waiting...")
time.sleep(60)
except APIError as e:
print(f"API error: {e}").env
.env.*
**/secrets.*| Model | Requests/Min | Tokens/Min |
|---|---|---|
| Grok 4.1 Fast | 60 | 100,000 |
| Grok 4 | 30 | 50,000 |
| Grok 3 Mini | 100 | 200,000 |
| Endpoint | Description |
|---|---|
| Chat completions |
| List available models |
| Agent Tools API |
xai-modelsxai-x-searchxai-sentiment