Skip to content

Latest commit

 

History

History
238 lines (192 loc) · 5.85 KB

File metadata and controls

238 lines (192 loc) · 5.85 KB
title description
Authentication
Learn how to authenticate your ModelsLab API requests. Get your API key, configure headers, and securely integrate ModelsLab AI APIs into your application.

Getting Your API Key

Sign up at [modelslab.com](https://modelslab.com) if you haven't already. Navigate to your [API Keys Dashboard](https://modelslab.com/dashboard/api-keys). Click **Create New Key** and copy your API key immediately. Save your key in a secure location. You won't be able to see it again. Never share your API key publicly or commit it to version control. Treat it like a password.

Using Your API Key

Include your API key in every request using the key parameter in the request body:

{
  "key": "your_api_key_here",
  "prompt": "A beautiful sunset over mountains",
  ...
}

Code Examples

import requests

API_KEY = "your_api_key_here"  # Use environment variables in production

response = requests.post(
    "https://modelslab.com/api/v6/images/text2img",
    json={
        "key": API_KEY,
        "prompt": "A beautiful sunset over mountains",
        "model_id": "flux",
        "width": 512,
        "height": 512
    }
)

data = response.json()
print(data)
const API_KEY = "your_api_key_here"; // Use environment variables in production

const response = await fetch("https://modelslab.com/api/v6/images/text2img", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    key: API_KEY,
    prompt: "A beautiful sunset over mountains",
    model_id: "flux",
    width: 512,
    height: 512
  })
});

const data = await response.json();
console.log(data);
curl -X POST "https://modelslab.com/api/v6/images/text2img" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "your_api_key_here",
    "prompt": "A beautiful sunset over mountains",
    "model_id": "flux",
    "width": 512,
    "height": 512
  }'
<?php
$apiKey = "your_api_key_here";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://modelslab.com/api/v6/images/text2img");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "key" => $apiKey,
    "prompt" => "A beautiful sunset over mountains",
    "model_id" => "flux",
    "width" => 512,
    "height" => 512
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);
print_r($data);
?>
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    apiKey := "your_api_key_here"

    payload := map[string]interface{}{
        "key":      apiKey,
        "prompt":   "A beautiful sunset over mountains",
        "model_id": "flux",
        "width":    512,
        "height":   512,
    }

    jsonData, _ := json.Marshal(payload)

    resp, err := http.Post(
        "https://modelslab.com/api/v6/images/text2img",
        "application/json",
        bytes.NewBuffer(jsonData),
    )
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()

    var result map[string]interface{}
    json.NewDecoder(resp.Body).Decode(&result)
    fmt.Println(result)
}

Environment Variables

Always use environment variables to store your API key in production applications.
import os

API_KEY = os.environ.get("MODELSLAB_API_KEY")
const API_KEY = process.env.MODELSLAB_API_KEY;
export MODELSLAB_API_KEY="your_api_key_here"

Security Best Practices

Never hardcode API keys in your source code. Use environment variables or secret management tools. Create different API keys for development, staging, and production environments. Periodically rotate your API keys, especially if you suspect they may have been compromised. Regularly check your [dashboard](https://modelslab.com/dashboard) for unusual API activity.

Authentication Errors

Status Code Error Message Solution
401 Invalid API key Verify your API key is correct and hasn't been revoked
401 API key required Include the key parameter in your request body
402 Insufficient credits Add credits to your account or upgrade your plan
403 Feature not available This feature requires a higher subscription tier
For detailed error handling, see our [Error Codes](/error-codes) documentation.

API Key Management

You can manage your API keys from the dashboard:

  • Create new keys for different applications
  • Revoke compromised or unused keys
  • View usage statistics per key
  • Set rate limits (Enterprise plans)

Next Steps

Make your first API call in 5 minutes Understand API rate limits for your plan Handle errors gracefully in your application Use our official SDKs for faster development