Welcome to Common API
The Common API provides essential utility services for managing your kie.ai account and handling generated content. These APIs help you monitor credit usage and access generated files efficiently.
Authentication
All API requests require authentication using Bearer tokens. Please obtain your API key from the API Key Management Page .
Please keep your API key secure and never share it publicly. If you suspect your key has been compromised, reset it immediately.
API Base URL
Authorization : Bearer YOUR_API_KEY
Quick Start Guide
Step 1: Check Your Credit Balance
Monitor your account credits to ensure sufficient balance for continued service:
curl -X GET "https://api.kie.ai/api/v1/chat/credit" \
-H "Authorization: Bearer YOUR_API_KEY"
const response = await fetch ( 'https://api.kie.ai/api/v1/chat/credit' , {
method: 'GET' ,
headers: {
'Authorization' : 'Bearer YOUR_API_KEY'
}
});
const result = await response . json ();
console . log ( 'Current credits:' , result . data );
import requests
url = "https://api.kie.ai/api/v1/chat/credit"
headers = {
"Authorization" : "Bearer YOUR_API_KEY"
}
response = requests.get(url, headers = headers)
result = response.json()
print ( f "Current credits: { result[ 'data' ] } " )
Response:
{
"code" : 200 ,
"msg" : "success" ,
"data" : 100
}
Step 2: Get Download URL for Generated Files
Convert generated file URLs to temporary downloadable links:
curl -X POST "https://api.kie.ai/api/v1/common/download-url" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://tempfile.1f6cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbd98"
}'
const response = await fetch ( 'https://api.kie.ai/api/v1/common/download-url' , {
method: 'POST' ,
headers: {
'Authorization' : 'Bearer YOUR_API_KEY' ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({
url: 'https://tempfile.1f6cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbd98'
})
});
const result = await response . json ();
console . log ( 'Download URL:' , result . data );
import requests
url = "https://api.kie.ai/api/v1/common/download-url"
headers = {
"Authorization" : "Bearer YOUR_API_KEY" ,
"Content-Type" : "application/json"
}
payload = {
"url" : "https://tempfile.1f6cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbd98"
}
response = requests.post(url, json = payload, headers = headers)
result = response.json()
print ( f "Download URL: { result[ 'data' ] } " )
Response:
{
"code" : 200 ,
"msg" : "success" ,
"data" : "https://tempfile.1f6cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbd98"
}
Download URLs are valid for 20 minutes only . Make sure to download or cache the content within this timeframe.
API Overview
Get Account Credits
GET /api/v1/chat/credit Purpose : Monitor your account credit balanceFeatures :
Real-time credit balance retrieval
No parameters required
Instant response
Essential for usage monitoring
Use Cases :
Check credits before starting generation tasks
Monitor credit consumption patterns
Plan credit replenishment
Implement credit threshold alerts
Get Download URL
POST /api/v1/common/download-url Purpose : Generate temporary download links for generated filesFeatures :
Supports all kie.ai generated file types (images, videos, audio, etc.)
20-minute validity period
Secure authenticated access
Only works with kie.ai generated URLs
Use Cases :
Download generated content to local storage
Share temporary links with team members
Integrate with external systems
Build custom download workflows
Practical Examples
Credit Monitoring System
Implement an automated credit monitoring system:
class KieAIClient {
constructor ( apiKey ) {
this . apiKey = apiKey ;
this . baseUrl = 'https://api.kie.ai' ;
}
async getCredits () {
const response = await fetch ( ` ${ this . baseUrl } /api/v1/chat/credit` , {
method: 'GET' ,
headers: {
'Authorization' : `Bearer ${ this . apiKey } `
}
});
if ( ! response . ok ) {
throw new Error ( `Failed to get credits: ${ response . statusText } ` );
}
const result = await response . json ();
return result . data ;
}
async getDownloadUrl ( fileUrl ) {
const response = await fetch ( ` ${ this . baseUrl } /api/v1/common/download-url` , {
method: 'POST' ,
headers: {
'Authorization' : `Bearer ${ this . apiKey } ` ,
'Content-Type' : 'application/json'
},
body: JSON . stringify ({ url: fileUrl })
});
if ( ! response . ok ) {
throw new Error ( `Failed to get download URL: ${ response . statusText } ` );
}
const result = await response . json ();
return result . data ;
}
async downloadFile ( fileUrl , outputPath ) {
// Get download URL
const downloadUrl = await this . getDownloadUrl ( fileUrl );
// Download file
const response = await fetch ( downloadUrl );
const buffer = await response . arrayBuffer ();
// Save to file (Node.js)
const fs = require ( 'fs' );
fs . writeFileSync ( outputPath , Buffer . from ( buffer ));
console . log ( `File downloaded to: ${ outputPath } ` );
}
async checkCreditsAndWarn ( threshold = 10 ) {
const credits = await this . getCredits ();
if ( credits < threshold ) {
console . warn ( `⚠️ Low credits warning: ${ credits } credits remaining` );
return false ;
}
console . log ( `✓ Credits available: ${ credits } ` );
return true ;
}
}
// Usage example
const client = new KieAIClient ( 'YOUR_API_KEY' );
// Monitor credits before operation
async function runWithCreditCheck () {
const hasEnoughCredits = await client . checkCreditsAndWarn ( 20 );
if ( ! hasEnoughCredits ) {
console . error ( 'Insufficient credits. Please recharge your account.' );
return ;
}
// Proceed with operations
console . log ( 'Credits verified. Proceeding with operations...' );
}
// Download generated files
async function downloadGeneratedFiles ( fileUrls ) {
for ( let i = 0 ; i < fileUrls . length ; i ++ ) {
try {
await client . downloadFile (
fileUrls [ i ],
`./downloads/file- ${ i + 1 } .mp4`
);
console . log ( `✓ Downloaded file ${ i + 1 } / ${ fileUrls . length } ` );
} catch ( error ) {
console . error ( `✗ Failed to download file ${ i + 1 } :` , error . message );
}
}
}
// Periodic credit monitoring
async function monitorCredits ( intervalMinutes = 60 ) {
setInterval ( async () => {
try {
const credits = await client . getCredits ();
console . log ( `[ ${ new Date (). toISOString () } ] Current credits: ${ credits } ` );
if ( credits < 50 ) {
// Send alert (email, webhook, etc.)
console . warn ( 'ALERT: Credits below 50!' );
}
} catch ( error ) {
console . error ( 'Credit check failed:' , error . message );
}
}, intervalMinutes * 60 * 1000 );
}
import requests
import time
import os
from datetime import datetime
from typing import Optional
class KieAIClient :
def __init__ ( self , api_key : str ):
self .api_key = api_key
self .base_url = 'https://api.kie.ai'
self .headers = {
'Authorization' : f 'Bearer { api_key } '
}
def get_credits ( self ) -> int :
"""Get current account credits"""
response = requests.get(
f ' { self .base_url } /api/v1/chat/credit' ,
headers = self .headers
)
if not response.ok:
raise Exception ( f 'Failed to get credits: { response.text } ' )
result = response.json()
return result[ 'data' ]
def get_download_url ( self , file_url : str ) -> str :
"""Get temporary download URL for generated file"""
response = requests.post(
f ' { self .base_url } /api/v1/common/download-url' ,
headers = { ** self .headers, 'Content-Type' : 'application/json' },
json = { 'url' : file_url}
)
if not response.ok:
raise Exception ( f 'Failed to get download URL: { response.text } ' )
result = response.json()
return result[ 'data' ]
def download_file ( self , file_url : str , output_path : str ) -> None :
"""Download file from kie.ai URL"""
# Get download URL
download_url = self .get_download_url(file_url)
# Download file
response = requests.get(download_url)
if not response.ok:
raise Exception ( f 'Failed to download file: { response.text } ' )
# Save to file
os.makedirs(os.path.dirname(output_path), exist_ok = True )
with open (output_path, 'wb' ) as f:
f.write(response.content)
print ( f 'File downloaded to: { output_path } ' )
def check_credits_and_warn ( self , threshold : int = 10 ) -> bool :
"""Check credits and warn if below threshold"""
credits = self .get_credits()
if credits < threshold:
print ( f '⚠️ Low credits warning: { credits } credits remaining' )
return False
print ( f '✓ Credits available: { credits } ' )
return True
# Usage example
def main ():
client = KieAIClient( 'YOUR_API_KEY' )
# Monitor credits before operation
def run_with_credit_check ():
has_enough_credits = client.check_credits_and_warn( threshold = 20 )
if not has_enough_credits:
print ( 'Insufficient credits. Please recharge your account.' )
return
print ( 'Credits verified. Proceeding with operations...' )
# Download generated files
def download_generated_files ( file_urls : list ):
for i, file_url in enumerate (file_urls):
try :
client.download_file(
file_url,
f './downloads/file- { i + 1 } .mp4'
)
print ( f '✓ Downloaded file { i + 1 } / { len (file_urls) } ' )
except Exception as e:
print ( f '✗ Failed to download file { i + 1 } : { e } ' )
# Periodic credit monitoring
def monitor_credits ( interval_minutes : int = 60 ):
while True :
try :
credits = client.get_credits()
timestamp = datetime.now().isoformat()
print ( f '[ { timestamp } ] Current credits: { credits } ' )
if credits < 50 :
# Send alert (email, webhook, etc.)
print ( 'ALERT: Credits below 50!' )
except Exception as e:
print ( f 'Credit check failed: { e } ' )
time.sleep(interval_minutes * 60 )
# Example usage
print ( 'Checking credits...' )
run_with_credit_check()
print ( ' \n Downloading files...' )
file_urls = [
'https://tempfile.1f6cxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxbd98' ,
'https://tempfile.2f7dxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcd99'
]
download_generated_files(file_urls)
if __name__ == '__main__' :
main()
Error Handling
Common errors and handling methods:
// Check if API key is correct
if ( response . status === 401 ) {
console . error ( 'Invalid API key, please check Authorization header' );
// Retrieve or update API key
}
422 Validation Error (Download URL)
// Only kie.ai generated URLs are supported
if ( response . status === 422 ) {
const error = await response . json ();
console . error ( 'Invalid URL:' , error . msg );
// Ensure you're using a kie.ai generated file URL
// External URLs are not supported
}
// Credits depleted, need to recharge
if ( response . status === 402 ) {
console . error ( 'Insufficient credits. Please recharge your account.' );
// Redirect to credit purchase page
// Or send notification to admin
}
// Implement retry mechanism
async function apiCallWithRetry ( apiFunction , maxRetries = 3 ) {
for ( let i = 0 ; i < maxRetries ; i ++ ) {
try {
return await apiFunction ();
} catch ( error ) {
if ( i === maxRetries - 1 ) throw error ;
// Exponential backoff
const delay = Math . pow ( 2 , i ) * 1000 ;
await new Promise ( resolve => setTimeout ( resolve , delay ));
}
}
}
Best Practices
Monitor regularly : Check credits before starting large batch operations
Set up alerts : Implement automated alerts when credits fall below threshold
Budget planning : Track credit consumption patterns for better planning
Graceful degradation : Handle insufficient credit scenarios appropriately
Time-sensitive : Download URLs expire after 20 minutes
Cache appropriately : Save files immediately after getting download URLs
Batch downloads : Process multiple files efficiently within time limit
Error handling : Implement retry logic for failed downloads
API key protection : Never expose API keys in client-side code
HTTPS only : Always use HTTPS for API requests
Key rotation : Regularly rotate API keys for security
Access logging : Keep logs of API usage for auditing
Important Notes
Download URL Expiration : Temporary download URLs are valid for 20 minutes only . Make sure to:
Download files immediately after getting the URL
Implement error handling for expired URLs
Cache downloaded content for future use
Credit Balance : Service access will be restricted when credits are depleted. Always:
Monitor credit balance regularly
Set up low-credit alerts
Plan credit replenishment in advance
Implement graceful degradation when credits are low
Supported URLs : The download URL endpoint only supports files generated by kie.ai services. External file URLs will result in a 422 validation error.
Status Codes
Request processed successfully
Authentication credentials are missing or invalid
Account does not have enough credits to perform the operation
The requested resource or endpoint does not exist
Invalid request parameters (e.g., external URL not supported)
Request limit has been exceeded for this resource
System is currently undergoing maintenance
An unexpected error occurred while processing the request
The requested feature is currently disabled
Next Steps
Integration Examples
Support
Need help? Our technical support team is here to assist you.
Ready to start? Get your API key and begin using Common API services immediately!