Getting Started
This guide will walk you through setting up and making your first image generation request.
Prerequisites
Before you begin, you'll need:
- An active BestImage.ai account
- Your API key from the dashboard
- Basic knowledge of HTTP/REST APIs
Step 1: Get Your API Key
- Log in to your BestImage.ai account
- Navigate to Profile > API Key
- Copy your API key and keep it secure
Warning: Never expose your API key in client-side code or public repositories
Step 2: Image Generation
Here's how to generate images using our API:
Code Examples
javascript
// Install: npm install axiosimport axios from 'axios';
const apiKey = 'YOUR_API_KEY';const baseURL = 'https://api.bestimage.ai';
// Create image generation taskasync function generateImage() { try { const response = await axios.post('/api/v1/image/task', { model_name: 'flux-context-max', prompt: 'A beautiful sunset over mountains', width: "16", height: "9" }, { baseURL, headers: { 'Authorization': `Bearer ${apiKey}`, 'Content-Type': 'application/json' } });
const { task_id } = response.data.data; console.log('Task created:', task_id); // Poll for results return await pollResult(task_id); } catch (error) { console.error('Error:', error.response?.data || error.message); }}
// Poll for task completionasync function pollResult(taskId) { while (true) { try { const response = await axios.get('/api/v1/image/' + taskId, { baseURL, headers: { 'Authorization': `Bearer ${apiKey}` } });
const { task_status, task_result } = response.data.data; if (task_status === 'succeed') { console.log('Images generated:', task_result.images); return task_result.images; } else if (task_status === 'failed') { throw new Error('Task failed'); } // Wait 2 seconds before next poll await new Promise(resolve => setTimeout(resolve, 2000)); } catch (error) { console.error('Polling error:', error); break; } }}
generateImage();Understanding the Response
When you create a task, you'll get a response like this:
json
{ "code": 0, "message": "success", "data": { "task_id": "12345", "response_url": "https://api.bestimage.ai/api/v1/image/12345", "status": "processing" }}Use the task_id to poll for results until the task completes:
json
{ "code": 0, "message": "success", "data": { "task_id": "12345", "task_status": "succeed", "task_result": { "images": [ { "url": "https://storage.bestimage.ai/generated/image1.jpg", "thumbnail_url": "https://storage.bestimage.ai/generated/thumb1.jpg", "resolution": "1024x576", "credits": 1 } ] } }}Key Features
- Text-to-Image: Generate images from text descriptions
- Image-to-Image: Transform existing images (add
image_url_listparameter) - Multiple Aspect Ratios: 21:9, 16:9, 4:3, 3:2, 1:1, 2:3, 3:4, 9:16, 9:21
What's Next?
- Check out the complete API Reference
- Learn about available models in the overview