Hi, how can we help you today?

Getting Started With API Example

To help you get started quickly, here are some common patterns for basic API operations using Python:

Generating an Image from Text


import requests
import base64
import json

# Authentication setup
api_key = "your_api_key"
api_secret = "your_api_secret"
credentials = f"{api_key}:{api_secret}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
headers = {
    "Authorization": f"Basic {encoded_credentials}",
    "Content-Type": "application/json"
}

# Request payload
payload = {
    "prompt": "A serene landscape with mountains and a lake at sunset",
    "modelId": "flux.1-dev",  # Using Flux model
    "width": 1024,
    "height": 1024,
    "numberOfImages": 1,
    "samplingSteps": 28,
    "guidanceScale": 3.5
}

# Make the request
response = requests.post(
    "https://api.cloud.scenario.com/v1/generate/txt2img",
    headers=headers,
    json=payload
)

# Parse the response
data = response.json()
job_id = data.get("jobId")
print(f"Image generation started with job ID: {job_id}")

# You would then poll the job status to get the results

Checking Job Status and Retrieving Results


def check_job_status(job_id):
    status_response = requests.get(
        f"https://api.cloud.scenario.com/v1/jobs/{job_id}",
        headers=headers
    )
    return status_response.json()
    
# Poll until job is complete
import time
while True:
    job_data = check_job_status(job_id)
    status = job_data.get("status")
    
    if status == "success":
        # Job is complete, access the results
        outputs = job_data.get("outputs", [])
        for output in outputs:
            imageIds = output.get("assetIds")
            print(f"Generated image IDs: {imageIds}")
        break
    elif status == "failure":
        print("Job failed:", job_data.get("error"))
        break
    else:
        print(f"Job status: {status}, progress: {job_data.get('progress', 0)}%")
        time.sleep(2)  # Wait before checking again




Was this helpful?