xAI & Grok API Integration
Guidelines for discovering, configuring, and building software with xAI and Grok APIs, SDKs, and endpoints.
Because xAI models and SDK capabilities evolve rapidly, treat the vendored documentation in resources/auto/ as the single source of truth for available endpoints, model identifiers, API parameters, and feature support.
OpenAI Compatibility & Client Setup
xAI APIs provide OpenAI SDK compatibility. Most integration tasks use official OpenAI client libraries configured with xAI endpoints:
Base URL: https://api.x.ai/v1
Environment Variable: XAI_API_KEY
SDK Setup Example (TypeScript / JavaScript):
import OpenAI from "openai"
const openai = new OpenAI({
apiKey: process.env.XAI_API_KEY,
baseURL: "https://api.x.ai/v1"
})
SDK Setup Example (Python):
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("XAI_API_KEY"),
base_url="https://api.x.ai/v1",
)
Documentation & API Option Lookup Protocol
When configuring xAI API integration for a task, follow this lookup protocol:
Query Vendored Documentation:
Search the resources/auto/ directory within this skill using pattern or string search tools to verify exact model names, endpoint paths, parameter schemas, and request payloads.
- Example search targets: model aliases (
grok-3, grok-2-vision), function calling schema (tools), search/web tools (grok-search), structured outputs (response_format), or vision capabilities.
Confirm Model & Endpoint Capabilities:
Before making API requests, verify that the selected Grok model supports the intended feature (e.g., multimodal vision inputs vs text-only, web search integration, function calling).
Handle API Credentials Safely:
Ensure XAI_API_KEY is loaded from environment variables or secure secret managers. Never hardcode credentials into source files.
Update Offline Resources when Outdated:
If a newly announced Grok feature or model identifier is missing from local resources, trigger a resource download:
skills --action download-resources --skill xai
Core Integration Patterns
- Model Selection: Verify current active model strings and pricing/rate-limit tiers in
resources/auto/.
- Streaming & Tool Calling: Use standard OpenAI-style
stream: true or tools arrays for function calling.
- Structured Outputs: Utilize JSON mode or JSON schema response formats when precise downstream parsing is required.
- Vision & Multimodal: Pass image URLs or base64 data to vision-supported Grok models following standard message content block arrays.
Completion Criteria
An xAI API integration task is complete when:
- Target model identifiers, endpoints, and parameters have been validated against vendored docs in
resources/auto/.
- Client initializations correctly target
https://api.x.ai/v1 and rely on environment-provided XAI_API_KEY.
- Error handling covers common API response codes (e.g. rate limits, token limit exceeded, invalid key).
- Code implementations have been verified against integration tests or mock executions.
1---2name: xai3description: Build applications with xAI and Grok APIs, SDKs, model selection, function calling, vision, live web search, and OpenAI-compatible client configurations. Use when integrating Grok models or xAI API endpoints, querying xAI capabilities, or configuring xAI API SDKs.4---56# xAI & Grok API Integration78Guidelines for discovering, configuring, and building software with xAI and Grok APIs, SDKs, and endpoints.910Because xAI models and SDK capabilities evolve rapidly, treat the vendored documentation in `resources/auto/` as the single source of truth for available endpoints, model identifiers, API parameters, and feature support.1112## OpenAI Compatibility & Client Setup1314xAI APIs provide OpenAI SDK compatibility. Most integration tasks use official OpenAI client libraries configured with xAI endpoints:1516- **Base URL:** `https://api.x.ai/v1`17- **Environment Variable:** `XAI_API_KEY`18- **SDK Setup Example (TypeScript / JavaScript):**1920 ```typescript21 import OpenAI from "openai"2223 const openai = new OpenAI({24 apiKey: process.env.XAI_API_KEY,25 baseURL: "https://api.x.ai/v1"26 })27 ```2829- **SDK Setup Example (Python):**3031 ```python32 from openai import OpenAI3334 client = OpenAI(35 api_key=os.environ.get("XAI_API_KEY"),36 base_url="https://api.x.ai/v1",37 )38 ```3940## Documentation & API Option Lookup Protocol4142When configuring xAI API integration for a task, follow this lookup protocol:43441. **Query Vendored Documentation:**45 Search the `resources/auto/` directory within this skill using pattern or string search tools to verify exact model names, endpoint paths, parameter schemas, and request payloads.46 - Example search targets: model aliases (`grok-3`, `grok-2-vision`), function calling schema (`tools`), search/web tools (`grok-search`), structured outputs (`response_format`), or vision capabilities.472. **Confirm Model & Endpoint Capabilities:**48 Before making API requests, verify that the selected Grok model supports the intended feature (e.g., multimodal vision inputs vs text-only, web search integration, function calling).493. **Handle API Credentials Safely:**50 Ensure `XAI_API_KEY` is loaded from environment variables or secure secret managers. Never hardcode credentials into source files.514. **Update Offline Resources when Outdated:**52 If a newly announced Grok feature or model identifier is missing from local resources, trigger a resource download:5354 ```bash55 skills --action download-resources --skill xai56 ```5758## Core Integration Patterns59601. **Model Selection:** Verify current active model strings and pricing/rate-limit tiers in `resources/auto/`.612. **Streaming & Tool Calling:** Use standard OpenAI-style `stream: true` or `tools` arrays for function calling.623. **Structured Outputs:** Utilize JSON mode or JSON schema response formats when precise downstream parsing is required.634. **Vision & Multimodal:** Pass image URLs or base64 data to vision-supported Grok models following standard message content block arrays.6465## Completion Criteria6667An xAI API integration task is complete when:68691. Target model identifiers, endpoints, and parameters have been validated against vendored docs in `resources/auto/`.702. Client initializations correctly target `https://api.x.ai/v1` and rely on environment-provided `XAI_API_KEY`.713. Error handling covers common API response codes (e.g. rate limits, token limit exceeded, invalid key).724. Code implementations have been verified against integration tests or mock executions.