Documentation Index Fetch the complete documentation index at: https://docs.toapis.com/llms.txt
Use this file to discover all available pages before exploring further.
The docs Playground does not support file uploads : Please use the cURL, Python, or JavaScript code examples below for testing.
Important Change : For better performance and cost control, we no longer support passing base64 image data directly in generation APIs. Please use this endpoint to upload images first, then use the returned URL in generation requests.
Why Upload First?
Performance - Base64 encoding inflates data by 33%, uploading first significantly reduces request payload size
Reusability - Upload once, use the URL multiple times without re-transmitting
Workflow
Authorizations
Use Bearer Token for authentication Get your API Key: Visit API Key Management Authorization: Bearer YOUR_API_KEY
Body
Image file Supported formats:
JPEG (.jpg, .jpeg)
PNG (.png)
WebP (.webp)
GIF (.gif)
Limits:
Upload purpose (optional) Default: generation
Response
Whether the request succeeded
Upload record ID for tracking
Public URL of the uploaded image, can be used directly in generation APIs
MIME type of the image, e.g., image/jpeg
curl --request POST \
--url https://toapis.com/v1/uploads/images \
--header 'Authorization: Bearer <token>' \
--form 'file=@/path/to/your/image.jpg'
200 Success
400 Bad Request
400 File Too Large
{
"success" : true ,
"message" : "" ,
"data" : {
"id" : "upload_abc12345" ,
"url" : "https://files.toapis.com/uploads/123/1737568800_abc12345.jpg" ,
"mime_type" : "image/jpeg" ,
"size" : 89234
}
}
Complete Example: Image-to-Image Workflow
Here’s a complete image-to-image workflow example:
import requests
import time
import os
API_KEY = os.getenv(
"TOAPIS_API_KEY" , "your-ToAPIs-key"
)
BASE_URL = "https://toapis.com"
def _raise_api_error ( resp : requests.Response, payload : dict ) -> None :
if resp.ok:
return
msg = payload.get( "message" ) or payload.get( "error" )
if isinstance (msg, dict ):
msg = msg.get( "message" ) or str (msg)
raise RuntimeError ( f "HTTP { resp.status_code } : { msg or payload } " )
def _require_api_key () -> None :
if not API_KEY :
raise RuntimeError ( "缺少 TOAPIS_API_KEY 环境变量" )
def upload_image ( file_path : str ) -> str :
_require_api_key()
with open (file_path, "rb" ) as f:
resp = requests.post(
f " { BASE_URL } /v1/uploads/images" ,
headers = { "Authorization" : f "Bearer { API_KEY } " },
files = { "file" : f},
)
body = resp.json()
_raise_api_error(resp, body)
if not body.get( "success" ):
raise RuntimeError (body.get( "message" ) or str (body))
return body[ "data" ][ "url" ]
def create_generation ( image_url : str , prompt : str ) -> str :
_require_api_key()
resp = requests.post(
f " { BASE_URL } /v1/images/generations" ,
headers = {
"Authorization" : f "Bearer { API_KEY } " ,
"Content-Type" : "application/json" ,
},
json = {
"model" : "gemini-3-pro-image-preview" ,
"prompt" : prompt,
"image_urls" : [image_url],
"size" : "16:9" ,
},
)
body = resp.json()
_raise_api_error(resp, body)
task_id = body.get( "id" ) or body.get( "task_id" )
if not task_id:
raise RuntimeError ( f "创建任务响应缺少 id: { body } " )
return task_id
def wait_for_result ( task_id : str ) -> str :
_require_api_key()
while True :
resp = requests.get(
f " { BASE_URL } /v1/images/generations/ { task_id } " ,
headers = { "Authorization" : f "Bearer { API_KEY } " },
)
result = resp.json()
_raise_api_error(resp, result)
status = result.get( "status" )
if status == "completed" :
r = result.get( "result" ) or {}
items = r.get( "data" ) or []
if not items or not items[ 0 ].get( "url" ):
raise RuntimeError ( f "completed 但无 URL: { result } " )
return items[ 0 ][ "url" ]
if status == "failed" :
err = result.get( "error" ) or {}
raise RuntimeError (
f "生成失败: { err.get( 'message' ) or result.get( 'fail_reason' ) or result } "
)
time.sleep( 2 )
if __name__ == "__main__" :
image_url = upload_image( "reference.jpg" )
print ( f "✅ 图片已上传: { image_url } " )
task_id = create_generation(image_url, "将这张照片转换为赛博朋克风格" )
print ( f "✅ 任务已创建: { task_id } " )
result_url = wait_for_result(task_id)
print ( f "✅ 生成完成: { result_url } " )