Getting Your API Key
Generate Key
Click Create New Key and copy your API key immediately.
Store Securely
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
Python
JavaScript
cURL
PHP
Go
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.
Python
JavaScript (Node.js)
Shell
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
Use Environment Variables Never hardcode API keys in your source code. Use environment variables or secret management tools.
Separate Keys Create different API keys for development, staging, and production environments.
Rotate Regularly Periodically rotate your API keys, especially if you suspect they may have been compromised.
Monitor Usage Regularly check your 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 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
Quickstart Guide Make your first API call in 5 minutes
Rate Limits Understand API rate limits for your plan
Error Codes Handle errors gracefully in your application
SDKs Use our official SDKs for faster development