Skip to content

Python SDK for Sirv REST API

By , Head of Sirv StudioUpdated

Open this page in a new chat

Short answer

Sirv's official Python SDK wraps the Sirv REST API, so you can upload, download, search and manage files, metadata, spins and account statistics from your code. Install it with pip install sirv-rest-api, then connect with the client ID and secret from your Sirv account's API settings. The SDK gets and refreshes the access token for you.

After reading this you will know

  • How to install the SDK and connect to your Sirv account
  • Which file, account, statistics and spin methods it offers
  • How to handle errors and limits

The official Python SDK provides a Pythonic interface for interacting with the Sirv REST API.

It handles authentication automatically with token refresh and provides generator-based iteration for paginated results.

Resources:

Install the SDK using pip:

pip install sirv-rest-api

For async support:

pip install sirv-rest-api[async]
  • Python 3.8 or higher
  • requests library
  • requests-toolbelt for uploads
from sirv import SirvClient
# Initialize the client
client = SirvClient(
client_id="your-client-id",
client_secret="your-client-secret"
)
# Connect to obtain token
client.connect()
# Get account information
account = client.get_account_info()
print(f"Account: {account['alias']}")
# List files in a directory
contents = client.read_folder_contents("/")
for file in contents["contents"]:
print(file["filename"])

The SDK handles authentication automatically. When you call connect(), it obtains an access token using your client credentials. The token is automatically refreshed before it expires.

client = SirvClient(
client_id="your-client-id",
client_secret="your-client-secret",
auto_refresh_token=True # Default: automatically refresh before expiry
)
# Connect with default expiry
client.connect()
# Or connect with custom expiry (1 hour)
client.connect(expires_in=3600)
# Check if connected
if client.is_connected():
print("Token is valid")
# Get current access token for external use
token = client.get_access_token()
OptionTypeDefaultDescription
client_idstrrequiredYour API client ID
client_secretstrrequiredYour API client secret
base_urlstrhttps://api.sirv.comAPI endpoint
auto_refresh_tokenboolTrueAuto-refresh token before expiry
token_refresh_bufferint60Seconds before expiry to refresh token
timeoutint30Request timeout in seconds
max_retriesint3Retry failed requests
client = SirvClient(
client_id="your-client-id",
client_secret="your-client-secret",
base_url="https://api.sirv.com",
auto_refresh_token=True,
token_refresh_buffer=60,
timeout=30,
max_retries=3
)

Use the context manager for automatic connection and cleanup:

with SirvClient(client_id="client-id", client_secret="client-secret") as client:
account = client.get_account_info()
print(f"Account: {account['alias']}")
# Client automatically disconnected

Complete file management including upload, download, copy, rename, delete, and metadata operations.

CategoryMethods
File Operationsread_folder_contents(), get_file_info(), upload_file(), download_file(), download_file_to(), copy_file(), rename_file(), delete_file(), batch_delete(), create_folder(), fetch_url()
Metadataget_file_meta(), set_file_meta(), get_approval_flag(), set_approval_flag(), get_file_title(), set_file_title(), get_file_description(), set_file_description(), get_file_tags(), add_file_tags(), remove_file_tags(), set_product_meta()
Folder Optionsget_folder_options(), set_folder_options()
Search & Iterationsearch_files(), iterate_search_results(), scroll_search(), iterate_folder_contents()
# List folder contents
contents = client.read_folder_contents("/images")
# Get file information
info = client.get_file_info("/images/photo.jpg")
# Upload a file from bytes
with open("/local/path/photo.jpg", "rb") as f:
data = f.read()
client.upload_file("/images/photo.jpg", data)
# Upload a file from path
client.upload_file("/images/photo.jpg", "/local/path/photo.jpg")
# Upload a file object
with open("/local/path/photo.jpg", "rb") as f:
client.upload_file("/images/photo.jpg", f)
# Download a file to bytes
data = client.download_file("/images/photo.jpg")
# Download a file to local path
client.download_file_to("/images/photo.jpg", "/local/downloaded.jpg")
# Copy a file
client.copy_file("/images/photo.jpg", "/backup/photo.jpg")
# Rename/move a file
client.rename_file("/images/old-name.jpg", "/images/new-name.jpg")
# Delete a file
client.delete_file("/images/photo.jpg")
# Batch delete files
client.batch_delete(["/images/photo1.jpg", "/images/photo2.jpg"])
# Create a folder
client.create_folder("/images/new-folder")
# Fetch file from URL
client.fetch_url("https://example.com/image.jpg", "/images/fetched.jpg")
# Fetch with wait for completion
client.fetch_url("https://example.com/image.jpg", "/images/fetched.jpg", wait=True)
# Search files
results = client.search_files({
"query": "filename:*.jpg",
"from": "/images",
"size": 100
})
# Iterate through search results (auto-pagination)
for file in client.iterate_search_results({
"query": "extension:.jpg",
"from": "/images"
}):
print(file["filename"])
# Manual pagination with scroll
results = client.search_files({"query": "*.jpg", "size": 100})
while results.get("_scroll_id"):
results = client.scroll_search(results["_scroll_id"])
for file in results["hits"]:
print(file["filename"])
# Iterate through folder contents (auto-pagination)
for item in client.iterate_folder_contents("/images"):
print(item["filename"])
# Get and set file metadata
meta = client.get_file_meta("/images/photo.jpg")
client.set_file_meta("/images/photo.jpg", {
"title": "My Photo",
"description": "A beautiful sunset"
})
# Get and set title
title = client.get_file_title("/images/photo.jpg")
client.set_file_title("/images/photo.jpg", "New Title")
# Get and set description
description = client.get_file_description("/images/photo.jpg")
client.set_file_description("/images/photo.jpg", "New Description")
# Manage tags
tags = client.get_file_tags("/images/photo.jpg")
client.add_file_tags("/images/photo.jpg", ["nature", "sunset", "beach"])
client.remove_file_tags("/images/photo.jpg", ["beach"])
# Set product metadata
client.set_product_meta("/images/product.jpg", {
"id": "SKU123",
"name": "Product Name",
"brand": "Brand Name"
})
# Approval flag
approved = client.get_approval_flag("/images/photo.jpg")
client.set_approval_flag("/images/photo.jpg", True)
# Get folder options
options = client.get_folder_options("/images")
# Set folder options
client.set_folder_options("/images", {
"scanSpins": True
})

Manage your Sirv account settings and retrieve account information.

MethodDescription
get_account_info()Get account information including CDN URL and account name
get_account_limits()Get API request limits and usage
get_storage_info()Get storage usage information
get_account_users()List all account users
get_billing_plan()Get billing plan details
# Get account information
account = client.get_account_info()
print(f"CDN URL: {account['cdnURL']}")
# Get API limits
limits = client.get_account_limits()
print(f"API calls: {limits['used']}/{limits['limit']}")
# Get storage information
storage = client.get_storage_info()
print(f"Storage used: {storage['used']} bytes")
# Get billing plan
plan = client.get_billing_plan()
print(f"Plan: {plan['name']}")
# List account users
users = client.get_account_users()

Retrieve usage statistics for your account.

MethodDescription
get_http_stats()Get HTTP transfer statistics
get_spin_views_stats()Get spin viewer statistics (max 5 days)
get_storage_stats()Get storage statistics
from datetime import date
# HTTP transfer statistics
http_stats = client.get_http_stats(
from_date=date(2024, 1, 1),
to_date=date(2024, 1, 31)
)
# Spin views statistics (max 5 days)
spin_stats = client.get_spin_views_stats(
from_date=date(2024, 1, 1),
to_date=date(2024, 1, 5),
alias="account-alias"
)
# Storage statistics
storage_stats = client.get_storage_stats(
from_date=date(2024, 1, 1),
to_date=date(2024, 1, 31)
)

Convert between spin and video formats, manage points of interest, and export to marketplaces.

# Convert spin to video
video = client.spin2video({
"filename": "/spins/product.spin"
})
# Convert video to spin
spin = client.video2spin({
"filename": "/videos/product.mp4"
})
# Export spin for marketplaces
client.export_spin_to_amazon(filename="/spins/product.spin")
client.export_spin_to_walmart(filename="/spins/product.spin")
client.export_spin_to_home_depot(filename="/spins/product.spin")
client.export_spin_to_lowes(filename="/spins/product.spin")
client.export_spin_to_grainger(filename="/spins/product.spin")
# Get points of interest
pois = client.get_points_of_interest("/spins/product.spin")
# Set point of interest
client.set_point_of_interest("/spins/product.spin", {
"name": "Feature Highlight",
"frame": 10,
"x": 0.5,
"y": 0.3
})
# Delete point of interest
client.delete_point_of_interest("/spins/product.spin", "poi-id")

Generate protected access tokens for secure file access.

jwt = client.generate_jwt({
"filename": "/images/private-photo.jpg",
"expiresIn": 3600 # 1 hour
})
print(f"Protected URL: {jwt['url']}")

The SDK provides a SirvApiError class for API-specific exceptions.

from sirv import SirvClient, SirvApiError
try:
client.upload_file("/images/photo.jpg", data)
except SirvApiError as e:
print(f"Status: {e.status_code}")
print(f"Error code: {e.error_code}")
print(f"Message: {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")
PropertyDescription
status_codeHTTP status code
error_codeSirv-specific error code
messageError message

The SDK includes full type hints with TypedDict definitions for all response types:

from sirv.types import AccountInfo, FileInfo, SearchResult, PointOfInterest
# IDE will provide autocomplete and type checking
account: AccountInfo = client.get_account_info()
print(account["cdnURL"]) # Type-safe access
  • AccountInfo, StorageInfo, BillingPlan
  • FileInfo, FolderContents, SearchResult
  • FileMeta, ProductMeta, PointOfInterest
  • HttpStats, SpinViewsStats, StorageStats
  • And 20+ additional types
Back to top