Integrate xAI's Grok language models into your application using the AI SDK. Supports chat and completion APIs with the powerful Grok-3 model family.
Integrate xAI's Grok language models into your application using the AI SDK. This skill provides support for xAI's chat and completion APIs through a standardized interface.
This skill helps you integrate xAI's Grok models (including Grok-3-beta) into your Node.js or TypeScript application using the Vercel AI SDK. It provides a unified interface for language model operations including text generation, streaming, and structured outputs.
Follow these steps to integrate the xAI Grok provider:
Install both the AI SDK core and the xAI provider:
```bash
npm i ai @ai-sdk/xai
```
Obtain your xAI API key from the xAI platform and set it as an environment variable:
```bash
export XAI_API_KEY="your-api-key-here"
```
Or add it to your `.env` file:
```
XAI_API_KEY=your-api-key-here
```
Create a provider instance in your code:
```typescript
import { xai } from '@ai-sdk/xai';
```
Implement text generation with the Grok model:
```typescript
import { xai } from '@ai-sdk/xai';
import { generateText } from 'ai';
const { text } = await generateText({
model: xai('grok-3-beta'),
prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});
console.log(text);
```
**Streaming Responses:**
```typescript
import { streamText } from 'ai';
const result = await streamText({
model: xai('grok-3-beta'),
prompt: 'Explain quantum computing in simple terms.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
```
**Structured Output:**
```typescript
import { generateObject } from 'ai';
import { z } from 'zod';
const { object } = await generateObject({
model: xai('grok-3-beta'),
schema: z.object({
ingredients: z.array(z.string()),
steps: z.array(z.string()),
}),
prompt: 'Generate a recipe for chocolate chip cookies.',
});
```
For complete API reference and advanced features, visit:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/xai-grok-provider-integration/raw