Python Examples
Complete Python examples for integrating with the Adbot API. Uses the requests library.
Installation
pip install requests Configuration
import requests
# Configuration
API_BASE_URL = "https://api.adbot.fi"
API_KEY = "your_api_key_here"
# Headers for all requests
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
} List Templates
def list_templates():
"""Get all available templates."""
response = requests.get(
f"{API_BASE_URL}/api/templates",
headers=headers
)
response.raise_for_status()
return response.json()["data"]
# Usage
templates = list_templates()
for template in templates:
print(f"{template['id']}: {template['name']}") Create a Video
def create_material(template_id: str, name: str, data: dict, outputs: list):
"""Create a new material from a template."""
payload = {
"name": name,
"data": data,
"outputs": outputs
}
response = requests.post(
f"{API_BASE_URL}/api/templates/{template_id}/materials",
headers=headers,
json=payload
)
response.raise_for_status()
return response.json()["data"]
# Usage
material = create_material(
template_id="tpl_social_v1",
name="Summer Sale Video",
data={
"headline": "Summer Sale - 50% Off!",
"productImage": "https://example.com/product.png",
"ctaText": "Shop Now"
},
outputs=["1080x1920", "1080x1080"]
)
print(f"Created material: {material['id']}") Check Material Status
import time
def wait_for_material(material_id: str, timeout: int = 60):
"""Wait for material to be ready."""
start_time = time.time()
while time.time() - start_time < timeout:
response = requests.get(
f"{API_BASE_URL}/api/materials/{material_id}",
headers=headers
)
response.raise_for_status()
material = response.json()["data"]
if material["status"] == "ready":
return material
elif material["status"] == "failed":
raise Exception("Material generation failed")
time.sleep(2)
raise TimeoutError("Material generation timed out")
# Usage
material = wait_for_material("mat_abc123")
for video in material["videos"]:
print(f"{video['format']}: {video['url']}") Upload Image
def upload_image(file_path: str, name: str = None):
"""Upload an image file."""
with open(file_path, "rb") as f:
files = {"file": f}
data = {"name": name} if name else {}
response = requests.post(
f"{API_BASE_URL}/api/assets/upload",
headers={"Authorization": f"Bearer {API_KEY}"},
files=files,
data=data
)
response.raise_for_status()
return response.json()["data"]
# Usage
asset = upload_image("/path/to/product.png", "My Product")
print(f"Uploaded: {asset['url']}") Remove Background
def remove_background(asset_id: str):
"""Remove background from an image."""
response = requests.post(
f"{API_BASE_URL}/api/assets/remove-background",
headers=headers,
json={"assetId": asset_id}
)
response.raise_for_status()
return response.json()["data"]
# Usage
processed = remove_background("asset_abc123")
print(f"Processed image: {processed['url']}") Batch Video Generation
from concurrent.futures import ThreadPoolExecutor, as_completed
def batch_create_materials(template_id: str, items: list, outputs: list):
"""Create multiple materials in parallel."""
def create_one(item):
return create_material(
template_id=template_id,
name=item["name"],
data=item["data"],
outputs=outputs
)
materials = []
with ThreadPoolExecutor(max_workers=5) as executor:
futures = {executor.submit(create_one, item): item for item in items}
for future in as_completed(futures):
try:
material = future.result()
materials.append(material)
print(f"Created: {material['id']}")
except Exception as e:
print(f"Error: {e}")
return materials
# Usage
items = [
{"name": "Product A Video", "data": {"headline": "Product A - 30% Off", "productImage": "https://example.com/a.png"}},
{"name": "Product B Video", "data": {"headline": "Product B - 40% Off", "productImage": "https://example.com/b.png"}},
{"name": "Product C Video", "data": {"headline": "Product C - 50% Off", "productImage": "https://example.com/c.png"}},
]
materials = batch_create_materials("tpl_social_v1", items, ["1080x1920"]) Complete Workflow Example
import requests
import time
class AdbotClient:
def __init__(self, api_key: str):
self.base_url = "https://api.adbot.fi"
self.headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
def get_templates(self):
response = requests.get(f"{self.base_url}/api/templates", headers=self.headers)
response.raise_for_status()
return response.json()["data"]
def create_video(self, template_id: str, name: str, data: dict, outputs: list):
payload = {"name": name, "data": data, "outputs": outputs}
response = requests.post(
f"{self.base_url}/api/templates/{template_id}/materials",
headers=self.headers,
json=payload
)
response.raise_for_status()
return response.json()["data"]
def get_material(self, material_id: str):
response = requests.get(
f"{self.base_url}/api/materials/{material_id}",
headers=self.headers
)
response.raise_for_status()
return response.json()["data"]
def wait_for_video(self, material_id: str, timeout: int = 120):
start = time.time()
while time.time() - start < timeout:
material = self.get_material(material_id)
if material["status"] == "ready":
return material
elif material["status"] == "failed":
raise Exception("Video generation failed")
time.sleep(3)
raise TimeoutError("Video generation timed out")
# Usage
client = AdbotClient("your_api_key_here")
# Create a video
material = client.create_video(
template_id="tpl_social_v1",
name="My Campaign Video",
data={
"headline": "Amazing Product Launch!",
"productImage": "https://example.com/product.png",
"ctaText": "Learn More"
},
outputs=["1080x1920", "1080x1080"]
)
print(f"Video created: {material['id']}")
# Wait for completion
completed = client.wait_for_video(material["id"])
for video in completed["videos"]:
print(f"Download: {video['url']}")