A Next.js application that generates images using the xAI API (Grok-2-image model). This application allows you to:
- Generate images from text prompts
- Create multiple images at once (1-10)
- Choose between URL and Base64 JSON response formats
- View and download generated images
- See the revised prompt used by the AI
You can see a live demo of this application here (deploy your own version to get a demo link).
Deploy your own version of this application to Vercel with one click:
- 🖼️ Generate images from text prompts
- 🔄 Create multiple images at once
- 📥 Download generated images
- 🔍 Preview images in full-screen modal
- 📚 View and manage generation history
- 🔒 Secure API key handling (server-side)
- 📱 Responsive design for all devices
- 🌓 Light and dark mode support
- Node.js 18.x or later
- An xAI API key (get it from x.ai)
-
Clone the repository:
git clone https://github.com/yourusername/xai-image-generator.git cd xai-image-generator -
Install dependencies:
# Choose one of the following package managers: npm install # or yarn install # or pnpm install
-
Copy the example environment file and add your xAI API key:
cp .env.local.example .env.local
Then edit
.env.localand add your xAI API key. -
Run the development server:
# Choose one of the following commands: npm run dev # or yarn dev # or pnpm dev
-
Open http://localhost:3000 in your browser to see the application.
- The user enters a prompt and selects options (number of images, response format).
- The client sends a request to the Next.js API route.
- The API route securely forwards the request to the xAI API using the server-side API key.
- The generated images are returned to the client and displayed.
- Users can download the generated images.
The application uses the xAI API for image generation. The endpoint is:
POST https://api.x.ai/v1/images/generations| Parameter | Type | Description |
|---|---|---|
model |
string |
Required: Must be "grok-2-image" (the xAI image generation model). |
prompt |
string |
The text prompt for image generation. |
n |
number |
Number of images to generate (range: 1–10). |
response_format |
string |
Format of the response: "url" (image URLs) or "b64_json" (base64 data). |
This project is licensed under the MIT License - see the LICENSE file for details.
- Next.js - The React framework
- Tailwind CSS - For styling
- shadcn/ui - UI components
- xAI - For the image generation API