Skip to main content

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 minutesWhat you’ll need:

Step 1: Get Your API Key

1

Log in to ModelsLab

Go to modelslab.com and log in to your account.
2

Navigate to API Keys

3

Create a New Key

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]}")
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

ParameterDescriptionDefault
keyYour API key (required)-
promptText description of the image (required)-
model_idAI model to use (e.g., “flux”, “sdxl”)“flux”
widthImage width in pixels (256-1024)512
heightImage height in pixels (256-1024)512
samplesNumber of images to generate (1-4)1
num_inference_stepsQuality/detail level (20-50)30
guidance_scaleHow 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 to find the perfect fit

Next Steps

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

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 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 or contact [email protected].