> ## Documentation Index
> Fetch the complete documentation index at: https://docs.modelslab.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 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

<Steps>
  <Step title="Create an Account">
    Sign up at [modelslab.com](https://modelslab.com) if you haven't already.
  </Step>

  <Step title="Access Dashboard">
    Navigate to your [API Keys Dashboard](https://modelslab.com/dashboard/api-keys).
  </Step>

  <Step title="Generate Key">
    Click **Create New Key** and copy your API key immediately.
  </Step>

  <Step title="Store Securely">
    Save your key in a secure location. You won't be able to see it again.
  </Step>
</Steps>

<Warning>
  Never share your API key publicly or commit it to version control. Treat it like a password.
</Warning>

## Using Your API Key

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

```json theme={null}
{
  "key": "your_api_key_here",
  "prompt": "A beautiful sunset over mountains",
  ...
}
```

## Code Examples

<CodeGroup>
  ```python Python theme={null}
  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)
  ```

  ```javascript JavaScript theme={null}
  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);
  ```

  ```bash cURL theme={null}
  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 PHP theme={null}
  <?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);
  ?>
  ```

  ```go Go theme={null}
  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)
  }
  ```
</CodeGroup>

## Environment Variables

<Tip>
  Always use environment variables to store your API key in production applications.
</Tip>

<CodeGroup>
  ```python Python theme={null}
  import os

  API_KEY = os.environ.get("MODELSLAB_API_KEY")
  ```

  ```javascript JavaScript (Node.js) theme={null}
  const API_KEY = process.env.MODELSLAB_API_KEY;
  ```

  ```bash Shell theme={null}
  export MODELSLAB_API_KEY="your_api_key_here"
  ```
</CodeGroup>

## Security Best Practices

<CardGroup cols={2}>
  <Card title="Use Environment Variables" icon="lock">
    Never hardcode API keys in your source code. Use environment variables or secret management tools.
  </Card>

  <Card title="Separate Keys" icon="key">
    Create different API keys for development, staging, and production environments.
  </Card>

  <Card title="Rotate Regularly" icon="rotate">
    Periodically rotate your API keys, especially if you suspect they may have been compromised.
  </Card>

  <Card title="Monitor Usage" icon="chart-line">
    Regularly check your [dashboard](https://modelslab.com/dashboard) for unusual API activity.
  </Card>
</CardGroup>

## 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       |

<Note>
  For detailed error handling, see our [Error Codes](/error-codes) documentation.
</Note>

## API Key Management

You can manage your API keys from the [dashboard](https://modelslab.com/dashboard/api-keys):

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

## Next Steps

<CardGroup cols={2}>
  <Card title="Quickstart Guide" icon="rocket" href="/quickstart">
    Make your first API call in 5 minutes
  </Card>

  <Card title="Rate Limits" icon="gauge" href="/rate-limits">
    Understand API rate limits for your plan
  </Card>

  <Card title="Error Codes" icon="triangle-exclamation" href="/error-codes">
    Handle errors gracefully in your application
  </Card>

  <Card title="SDKs" icon="code" href="/sdk/python">
    Use our official SDKs for faster development
  </Card>
</CardGroup>
