Developer Resources
Grok 5 Imagine API Documentation
Integrate xAI's Grok 5 Imagine model into your applications. Support for text-to-image, text-to-video, and image-to-video generation with simple REST API.
1
Authentication
The Grok 5 Imagine API uses Bearer Token authentication. Pass your API Key in the request header.
Authentication
Authorization: Bearer sk-your-api-key-here
Keep your API key secure. Do not share it in client-side code.
Pricing
| Type | Cost | Description |
|---|---|---|
Text to Image (text2img) | 5 Credits | Generate images from text prompts using Grok 5 AI. |
Text to Video (text2video) | 24 Credits | Generate videos from text prompts using Grok 5 AI. |
Image to Video (img2video) | 24 Credits | Generate videos from images using Grok 5 AI. |
2
Create Task
POST
https://grok5ai.com/api/generateAsync Generation
Pricing
Credits vary by model: text2img costs 5 credits, text2video and img2video cost 24 credits each.
Initiate a generation task. The API returns a task_id immediately, which you use to poll for results.
Body Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| model | string | Required | Model to use: text2img, text2video, or img2video. |
| prompt | string | Conditional | The text description for generation. Max 5000 characters. Required for text2img and text2video. |
| image_urls | array | Conditional | Array of image URLs for img2video mode. Required if task_id is not provided. |
| task_id | string | Conditional | Task ID from a previous generation to use as input image. Required if image_urls is not provided. |
| callback_url | string | Optional | Webhook URL for task completion notification. |
Request Examples
Text to Image Example (cURL)
curl -X POST https://grok5ai.com/api/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text2img",
"prompt": "A serene mountain landscape at sunset"
}'Text to Video Example (cURL)
curl -X POST https://grok5ai.com/api/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "text2video",
"prompt": "A timelapse of clouds moving over mountains"
}'Image to Video Example (cURL)
curl -X POST https://grok5ai.com/api/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "img2video",
"image_urls": ["https://example.com/image.jpg"]
}'Success Response
{
"code": 200,
"data": {
"task_id": "grok5abc123def456img",
"status": "IN_PROGRESS"
}
}Error Response
{
"code": 400,
"message": "Bad Request: 'prompt' is required.",
"data": null
}3
Check Status
GET
https://grok5ai.com/api/statusPoll this endpoint to check the progress of your task. We recommend polling every 5-10 seconds.
Query Parameters
| Parameter | Type | Description |
|---|---|---|
| task_id | string | The task ID received from the generate endpoint. Required |
Status Values
PENDINGTask is queuedIN_PROGRESSProcessingSUCCESSCompletedFAILEDError occurredcURL
curl -X GET "https://grok5ai.com/api/status?task_id=grok5abc123def456img" \
-H "Authorization: Bearer YOUR_API_KEY"Response (In Progress)
{
"code": 200,
"data": {
"task_id": "grok5abc123def456img",
"status": "IN_PROGRESS",
"consumed_credits": 10,
"created_at": "2024-12-19T10:00:00Z"
}
}Response (Completed)
{
"code": 200,
"data": {
"task_id": "grok5abc123def456img",
"status": "SUCCESS",
"response": [
"https://cdn.example.com/image.png"
]
}
}Response (Failed)
{
"code": 200,
"message": "success",
"data": {
"task_id": "grok5abc123def456img",
"status": "FAILED",
"consumed_credits": 0,
"error_message": "Content policy violation detected",
"created_at": "2024-12-19T10:00:00Z",
"request": { ... },
"response": null
}
}Error Codes
| Code | Description |
|---|---|
| 200 | Success |
| 400 | Bad Request - Invalid parameters (missing prompt, invalid model, etc.) |
| 401 | Unauthorized - Missing or invalid API key |
| 404 | Not Found - Task ID does not exist |
| 500 | Internal Server Error - Please retry or contact support |