Sora2 Calling the `/v1/videos` API (Python Example)
Calling the Sora2 /v1/videos API (Python Example)
Section titled “Calling the Sora2 /v1/videos API (Python Example)”Overview of this page
Sora2 Official /v1/videos API (Asynchronous Task)
Section titled “Sora2 Official /v1/videos API (Asynchronous Task)”- Official documentation: https://platform.openai.com/docs/api-reference/videos/create
Python Example
Section titled “Python Example”import requests
# --- 1. Enter the API Key you obtained from 4All API ---API_KEY = "sk-JKzyxxxxxxxxxxxxxxxxxxxxxxxxxx"
# --- 2. Enter your [creative prompt] here ---# vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvmy_prompt = "A lone protagonist standing on a skyscraper rooftop at sunset, wide shot, cinematic, 2.39:1, 24fps, anamorphic lens, film grain, teal & orange color grading, dramatic rim light, slow dolly in, slight handheld grit" # <--- Creative content, do not include "15s" or "vertical"# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# --- 3. Set your [technical parameters] here ---video_seconds = "15" # Video duration (s), corresponding to your "15s"
# Video size/orientation, corresponding to "vertical", "horizontal", "HD"# "1080x1920" = vertical, HD (HD Vertical)# "1920x1080" = horizontal, HD (HD Horizontal)# "" (empty string) = let the API decide automatically (default)video_size = "" # <--- Set here to "vertical, HD"
# Your API endpointAPI_URL = "https://sg.4All API.com/v1/videos"
# Prepare request headersheaders = { 'Authorization': f'Bearer {API_KEY}'}
# Prepare the form data to send# We will fill in the technical parameters above hereform_data = { 'model': (None, 'sora-2'), 'prompt': (None, my_prompt), # Use the creative prompt 'seconds': (None, video_seconds), # Use '15' 'input_reference': (None, ''), 'size': (None, video_size) # Use '1080x1920'}
print(f"Sending request to: {API_URL}")print(f"Using prompt: {my_prompt}")print(f"Duration set to: {video_seconds}s")print(f"Size set to: {video_size}")
try: # Send POST request response = requests.post(API_URL, headers=headers, files=form_data)
# Check HTTP status code response.raise_for_status()
# Print the successful response content print("Request successful, response content:") print(response.text)
except requests.exceptions.HTTPError as http_err: # Catch HTTP errors print(f"HTTP error: {http_err}") print(f"Response content: {response.text}")except requests.exceptions.RequestException as err: # Catch other request errors print(f"Request error occurred: {err}")