Skip to content

Latest commit

 

History

History
244 lines (199 loc) · 6.66 KB

File metadata and controls

244 lines (199 loc) · 6.66 KB
title description
Quickstart
Get started with ModelsLab API in under 5 minutes. Generate your first AI image with a simple API call using cURL, Python, or JavaScript.

Overview

This guide will help you make your first API call to ModelsLab and generate an AI image. By the end, you'll have a working integration.

**Time to complete**: 5 minutes

What you'll need:

Step 1: Get Your API Key

Go to [modelslab.com](https://modelslab.com) and log in to your account. Open your [API Keys Dashboard](https://modelslab.com/dashboard/api-keys). Click **Create New Key** and copy the generated key. Save your API key securely. You won't be able to see it again after leaving the page.

Step 2: Make Your First API Call

Choose your preferred language and run the code:

import requests

response = requests.post(
    "https://modelslab.com/api/v6/images/text2img",
    json={
        "key": "YOUR_API_KEY",
        "prompt": "A majestic lion in a savanna at sunset, photorealistic, 8k",
        "model_id": "flux",
        "width": 512,
        "height": 512,
        "samples": 1,
        "num_inference_steps": 30,
        "guidance_scale": 7.5
    }
)

data = response.json()
print(data)

# If successful, the image URL will be in data["output"]
if data.get("status") == "success":
    print(f"Image URL: {data['output'][0]}")
const response = await fetch("https://modelslab.com/api/v6/images/text2img", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    key: "YOUR_API_KEY",
    prompt: "A majestic lion in a savanna at sunset, photorealistic, 8k",
    model_id: "flux",
    width: 512,
    height: 512,
    samples: 1,
    num_inference_steps: 30,
    guidance_scale: 7.5
  })
});

const data = await response.json();
console.log(data);

// If successful, the image URL will be in data.output
if (data.status === "success") {
  console.log(`Image URL: ${data.output[0]}`);
}
curl -X POST "https://modelslab.com/api/v6/images/text2img" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "YOUR_API_KEY",
    "prompt": "A majestic lion in a savanna at sunset, photorealistic, 8k",
    "model_id": "flux",
    "width": 512,
    "height": 512,
    "samples": 1,
    "num_inference_steps": 30,
    "guidance_scale": 7.5
  }'
Replace `YOUR_API_KEY` with your actual API key from Step 1.

Step 3: Understanding the Response

Success Response

When your image is generated successfully:

{
  "status": "success",
  "generationTime": 2.45,
  "id": "abc123-def456",
  "output": [
    "https://pub-3626123a908346a7a8be8d9295f44e26.r2.dev/generations/abc123.png"
  ],
  "meta": {
    "prompt": "A majestic lion in a savanna at sunset, photorealistic, 8k",
    "model_id": "flux",
    "width": 512,
    "height": 512,
    "seed": 12345
  }
}

Open the URL in output to see your generated image!

Processing Response (Async)

For complex generations, you may receive a processing status:

{
  "status": "processing",
  "id": "abc123-def456",
  "eta": 15,
  "message": "Your request is being processed"
}

If this happens, use the fetch endpoint to check the status:

# Poll for results
fetch_response = requests.post(
    f"https://modelslab.com/api/v6/images/fetch/{data['id']}",
    json={"key": "YOUR_API_KEY"}
)
print(fetch_response.json())

Key Parameters Explained

Parameter Description Default
key Your API key (required) -
prompt Text description of the image (required) -
model_id AI model to use (e.g., "flux", "sdxl") "flux"
width Image width in pixels (256-1024) 512
height Image height in pixels (256-1024) 512
samples Number of images to generate (1-4) 1
num_inference_steps Quality/detail level (20-50) 30
guidance_scale How closely to follow prompt (1-20) 7.5

Pro Tips for Better Results

Be specific about what you want: - ❌ "a dog" - ✅ "A golden retriever puppy playing in autumn leaves, soft natural lighting, shallow depth of field, professional photography" Add style modifiers to guide the output: - "photorealistic", "8k", "detailed" - "oil painting", "watercolor", "digital art" - "cinematic lighting", "studio photography" - **Lower (3-7)**: More creative, varied results - **Higher (8-15)**: Closer to your prompt, more literal - **flux**: Best for photorealistic images - **sdxl**: Great for artistic and stylized images - Browse [all models](https://modelslab.com/models) to find the perfect fit

Next Steps

Now that you've made your first API call, explore more:

Explore all image generation options Create AI-generated videos Generate speech and clone voices Edit and enhance images with AI

Troubleshooting

- Double-check your API key is copied correctly - Ensure you're including it in the `key` field of the request body - Verify your key hasn't been revoked in the dashboard You've hit your queue limit. Wait for current requests to complete or upgrade your plan. See [Rate Limits](/rate-limits) for details. Some generations take longer. Use the async pattern with the fetch endpoint to handle long-running requests. Need help? Join our [Discord community](https://discord.com/invite/modelslab-1033301189254729748) or contact [support@modelslab.com](mailto:support@modelslab.com).