Login

Python SDK (Sync & Async)

The FastPix Python SDK offers an efficient way to integrate with FastPix APIs. It provides both synchronous and asynchronous functionalities, allowing you to choose the most suitable approach based on your application's requirements.


Synchronous mode

The SDK can be used in a synchronous manner, where each API call completes before the next one is made. This is ideal for simple, one-step-at-a-time tasks where blocking behavior is acceptable. It's easy to use and is perfect for small applications, debugging, or testing.


When to use synchronous mode:

  • For simple tasks that don't require concurrent operations.
  • When you need to perform one API call and get the result immediately, such as uploading a single video and receiving a playback link.
  • Ideal for smaller applications or developers testing the integration.

Example use case: You want to upload one video of your event’s highlights and immediately get a playback link to share on social media.


Asynchronous mode

The SDK also supports asynchronous operations, leveraging Python's asyncio to allow non-blocking API calls. This makes it perfect for high-performance applications that require parallel API calls, such as handling multiple uploads or streams concurrently.


When to use asynchronous mode:

  • For high-performance, large-scale tasks like bulk video uploads or processing multiple videos concurrently.
  • When you need to integrate with other background services or applications in a non-blocking manner.
  • Ideal for businesses with high-volume video processing needs, like uploading dozens of session recordings from a conference.

Example use case: You need to upload and process multiple videos at the same time, saving time by executing operations in parallel.




Let’s talk features

The FastPix Python SDK provides the following key features:


  1. Media APIs:
  • Upload media: Easily upload media files from URLs or devices to the FastPix platform.
  • Manage media: Perform various media operations such as listing, fetching, updating, and deleting media assets.
  • Playback IDs: Create and manage playback IDs to access your media.

  1. Live streaming APIs:
  • Create & manage live streams: Stream live events by creating, listing, updating, and deleting live streams.
  • Control stream access: Generate playback IDs to control who can access your live streams.
  • Simulcast: Stream content to multiple platforms simultaneously with ease.

For more detailed information, refer to the FastPix API Reference.


Prerequisites

Before you start using the FastPix Python SDK, make sure you have the following:

  1. To authenticate with our APIs, you'll need an Access Token and a Secret Key. You can generate these credentials by following the steps in the Authentication with Access Tokens guide.\
  2. Python (version >= 3.7): This SDK is compatible with Python version 3.7 or higher on the server side.

Step 1: Installation

To install the SDK, use pip with the GitHub repository URL to easily download and install the required libraries:

Via pip (you may need to run pip with root permission)

pip install git+https://github.com/FastPix/python-server-sdk 

Below you will find examples of configuring FastPix Python SDK into your project.


Step 2: Importing the SDK

from fastpix import Client as client 

Client = client(username="your-access-token-id", password="your-secret-key") 

PLEASE NOTE

For Async SDK users: When using the AsyncClient, all SDK methods must be prefixed with the await keyword. Import should also be fromAsyncClient .


Here’s an example:

# Sync Usage 

from fastpix import Client as client 

Client = client(username="your-access-token-id", password="your-secret-key") 

# Define the parameters for fetching media assets in a separate variable. 
media_request_params = { 
    "limit": 10, # Number of media assets to fetch in one request. 
    "offset": 1, # Starting position for the list of media assets (useful for pagination). 
    "orderBy": "desc" # Sort order for the media assets ("desc" for descending, "asc" for ascending). 
} 

try: 
    media = Client.media.get_all_media(params=media_request_params) 
    print("Media:", media) 

except Exception as e: 
    print(f"Error fetching media: {str(e)}")

# Async Usage  
# Note for Async SDK Users: When using the AsyncClient, all API methods must be prefixed with the await keyword. 

from fastpix import AsyncClient as client 

import asyncio 

async def main(): 
    # Initialize the AsyncClient with username and password. 

    Client = client(username="your-access-token-id", password="your-secret-key") 

    # Define the parameters for fetching media assets in a separate variable. 

media_request_params = { 
    "limit": 10, # Number of media assets to fetch in one request. 
    "offset": 1, # Starting position for the list of media assets (useful for pagination). 
    "orderBy": "desc" # Sort order for the media assets ("desc" for descending, "asc" for ascending). 
} 

try: 
    # Fetch media assets asynchronously using the await keyword. 
    media = await Client.media.get_all_media(params=media_request_params) 
    print("Media:", media) 

except Exception as e: 
    print(f"Error fetching media: {str(e)}") 

# Run the async function 

asyncio.run(main()) 


1. Perform media operations (Video on demand)


1.1. Media uploads

Upload media from URL

Use the client.media.create_pull_video() method to upload media directly from a URL. For detailed configuration and to add other features, refer to the Create media from URL API documentation.


# Define the request payload for uploading media from a URL. 

mediaFromUrlRequest = { 
    "inputs": [{ 
    "type": "video", # Specifies the type of media being uploaded (e.g., "video"). 
    "url": "https://static.fastpix.io/sample.mp4" # URL of the media file to be uploaded. 
    }], 
    "metadata": { 
        "video_title": "Big_Buck_Bunny" # Metadata to associate with the media, such as its title. 
    }, 
    "accessPolicy": "public", # Access policy for the media ("public" or "private"). 
    "maxResolution": "1080p" 
} 
 
media_response = client.media.create_pull_video(mediaFromUrlRequest) 
print("media_response", media_response)

Upload media from local device

Use the client.media.get_presigned_url() method to fetch the signedUrl and to make use of it to upload media directly from a local device via PUT HTTP Method. For detailed configuration options, refer to the Upload media from device API documentation.


# Define the request payload for uploading media from a device. 

mediaFromDeviceRequest = { 
    "corsOrigin": "*", # Specifies the allowed origin for CORS (Cross-Origin Resource Sharing). "*" allows all origins. 
    "pushMediaSettings": { 
    "accessPolicy": "private", # Sets the access policy for the uploaded media (e.g., "private" or "public"). 
    "optimizeAudio": True, # Enables audio optimization for the uploaded media. 
    "maxResolution": "1080p", # Specifies the maximum resolution allowed for the uploaded media. 
    }, 
} 
 
mediaFromDeviceResponse = client.media.get_presigned_url(mediaFromDeviceRequest) 
print("Upload Response:", mediaFromDeviceResponse) 


1.2. Media management

Get list of all media

Use client.media.get_all() to fetch a list of all media assets. You can customize the query by modifying parameters such as limit, offset and orderBy. For more details on listing media assets, refer to the Get list of all media API documentation.


# Define the parameters for fetching media assets in a separate variable. 

media_asset_request_params = { 
    "limit": 10, # Number of media assets to fetch in one request. 
    "offset": 1, # Starting position for the list of media assets (useful for pagination). 
    "orderBy": "desc", # Sort order for the media assets ("desc" for descending, "asc" for ascending). 
} 

# Assuming `client` is already initialized with the proper credentials 

all_media_assets = client.media.get_all(media_asset_request_params) 

# Print the fetched media assets 
print("All Media Assets:", all_media_assets) 

Get media asset by ID

Use the client.media.get_by_id() method to retrieve a media asset by its ID. Simply provide the mediaId of the asset to fetch its details. For more information, refer to the Get a media by ID API documentation.


mediaId = "mediaId" # Unique identifier for the media asset to be retrieved 

get_media_asset = client.media.get_by_id(mediaId) 

# Print the retrieved media asset by ID 
print("Retrieved media asset by ID:", get_media_asset) 

Update media asset

Use the client.media.update() method to modify the metadata or other properties of a specific media asset. Provide the mediaId of the asset and the updated metadata. For more information, refer to the Update a media by ID API documentation.


mediaId = "mediaId" # Unique identifier for the media asset to be retrieved 

# Define the payload with the updates to be applied to the media asset. 

updatePayload = { 
    "metadata": { 
        "key": "value", # Replace "key" and "value" with actual metadata keys and values 
    }, 
} 

update_media_asset = client.media.update(mediaId, updatePayload) 

# Print the updated media asset details 
print("Updated Media Asset:", update_media_asset)

Delete media asset

Use the client.media.delete() method to remove a media asset by its ID. Simply provide the mediaId of the asset you wish to delete. For further details, refer to the Delete a media by ID API documentation.


mediaId = "mediaId" # Unique identifier for the media asset to be retrieved 

# Assuming `client` is already initialized with the necessary credentials 
delete_media_asset = client.media.delete(mediaId) 
 
# Print the response indicating the media asset has been deleted 
print("Deleted Media Asset:", delete_media_asset) 

Get media asset info

Use the client.media.get_media_info() method to retrieve detailed information about a specific media asset by providing its mediaId. For more information, refer to the Get info of media inputs API documentation.


mediaId = "mediaId" # Unique identifier for the media asset to be retrieved 

getMediaInfo = client.media.get_media_info(mediaId) 
print("Media Asset Info:", getMediaInfo) 


1.3. Manage media playback

Generate media playback ID

Use the client.media_playback_ids.create() method to create a playback ID for a specific media asset. Provide the mediaId and configure options like the accessPolicy. For detailed configuration options, refer to the Create a playback ID API documentation.


# Define the mediaId and accessPolicy dynamically 
 
mediaId = "media-id" # Unique identifier for the media asset. 
playbackOptions = { 
    "accessPolicy": "public", # Can be 'public' or 'private'. 
} 
playbackIdResponse = client.media_playback_ids.create(mediaId, playbackOptions) 
print("Playback ID Creation Response:", playbackIdResponse) 

Delete media playback ID

Use the client.media_playback_ids.delete() method to remove a playback ID for a specific media asset. You must provide both the mediaId and the playbackId to delete it. For detailed configuration options, refer to the Delete a playback ID API documentation.


# Define the mediaId and playbackId dynamically 

mediaId = "media-id"; # The ID of the media asset for which you want to delete the playback ID. 
playbackIds = ["id1", "id2"]; # The playback ID that you want to delete. 
deletePlaybackResponse = client.media_playback_ids.delete(mediaId, playbackIds) 
print("Playback ID Deletion Response:", deletePlaybackResponse) 


2. Live stream operations


2.1 Start live stream

Create a new stream:

Use the client.livestreams.create() method to start a live stream with your desired configurations. For detailed configuration options, refer to the Create a new stream API documentation.


liveStreamRequest = { 
    "playbackSettings": { 
    "accessPolicy": "public", # Defines the access level of the live stream (public or private) 
    }, 
    "inputMediaSettings": { 
    "maxResolution": "1080p", # Set the maximum resolution of the live stream 
    "reconnectWindow": 1800, # Set the duration for reconnecting the stream in seconds 
    "mediaPolicy": "private", # Define media policy (private or public) 
    "metadata": { 
        "liveStream": "fp_livestream", # Custom metadata for the live stream 
    }, 
    "enableDvrMode": True, # Enable DVR mode to allow viewers to rewind the live stream 
    }, 
} 
 
# Initiating the live stream
generateLiveStream = client.livestreams.create(liveStreamRequest) 
print("Live Stream initiated successfully:", generateLiveStream) 


2.2 Live stream management

Get list of all live streams:

Use the client.livestreams.list() method to retrieve a list of all live streams. Customize the query by adjusting parameters such as limit, offset, and orderBy. For detailed configuration options, refer to the Get all live streams API documentation.


getAllLiveStreamPagination = { 
    "limit": 10, # Limit the number of live streams retrieved. 
    "offset": 1, # Skip a specified number of streams for pagination. 
    "orderBy": "asc", # Order the results based on the specified criteria ("asc" or "desc"). 
} 

getAllLiveStreams = client.livestreams.list(getAllLiveStreamPagination) 
print("All Live Streams:", getAllLiveStreams) 

Get live stream by Id:

Use the client.livestreams.get() method to fetch a specific live stream by its ID. Provide the streamId of the stream you want to retrieve. For more details, refer to the Get stream by ID API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
getLiveStreamById = client.livestreams.get(streamId) 
print("Live Stream Details:", getLiveStreamById) 

Update live stream:

Use the client.livestreams.update() method to modify the configuration of a live stream. Provide the streamId and specify the fields you wish to update. For more details, refer to the Update a stream API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Provide the stream ID for the live stream to update 

updateLiveStreamRequest = { 
    "metadata": { 
    "livestream_name": "Game_streaming", 
    }, 
    "reconnectWindow": 100, 
} 

updateLiveStream = client.livestreams.update(streamId,updateLiveStreamRequest) 
print("Updated Live Stream:", updateLiveStream) 

Delete live stream:

Use the client.livestreams.delete() method to remove a live stream. Provide the streamId of the stream you want to delete. For more details, refer to the Delete a stream API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Provide the stream ID for the live stream to update 
deleteLiveStream = client.livestreams.delete("a09f3e958c16ed00e85bfe798abd9845") # Provide the stream ID of the live stream to delete 
print("Deleted Live Stream:", deleteLiveStream) 


2.3. Manage live stream playback:

Generate live stream playback ID:

Use the client.livestream_playback_ids.create() method to create a playback ID for a live stream. Replace streamId with the actual ID of the stream and specify the desired accessPolicy. For more details, refer to the Create a playback ID API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
body = { "accessPolicy": "public" } 
generateLiveStreamPlaybackId = client.livestream_playback_ids.create(streamId, body) 
print("Generated Live Stream Playback ID:", generateLiveStreamPlaybackId) 

Delete live stream playback ID:

Use the client.livestream_playback_ids.delete() method to remove a specific playback ID for a live stream. Provide both the streamId of the stream and the playbackId you wish to delete. For more details, refer to the Delete a playback ID API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
playbackId = "632029b4-7c53-4dcf-a4d3-1884c29e90f8" # Replace with actual playback ID 
deleteLiveStreamPlaybackId = client.livestream_playback_ids.delete(streamId, playbackId) 
print("Deleted Live Stream Playback ID:", deleteLiveStreamPlaybackId) 

Get live stream playback policy:

Use the client.livestream_playback_ids.get() method to retrieve the playback policy for a specific live stream playback ID. Provide the streamId and playbackId to fetch the policy details. For more information, refer to the Get stream's playback ID API documentation.


media_type = "livestream"
stream_id = "1c5e8abcc2080cba74f5d0ac91c7833e"  # Replace with the actual stream ID
playback_id = "95ce872d-0b58-44f3-be72-8ed8b97ee2c9"  # Replace with the actual playback ID
 
get_livestream_playback_policy = client.playback_ids.get(media_type, stream_id, playback_id)
 
print("Live Stream Playback Policy:", get_livestream_playback_policy )


2.4. Simulcast live stream:

Initiate live stream simulcast:

Use the client.livestreams.create_simulcast() method to create a new simulcast for a live stream. Provide the streamId along with the simulcast payload, including the URL and stream key. For more details, refer to the Create a simulcast API documentation.


simulcastPayload = { 
    "url": "rtmps://live.fastpix.io:443/live", 
    "streamKey": "46c3457fa8a579b2d4da64125a2b6e83ka09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream key 
} 

streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
generateSimulcast = client.livestreams.create_simulcast(streamId, simulcastPayload) 
print("Generate Simulcast:", generateSimulcast) 

Get live stream simulcast:

Use the client.livestreams.get_simulcast() method to retrieve details of a specific simulcast for a live stream. Provide the streamId and simulcastId of the simulcast you wish to fetch. For more details, refer to the Get a specific simulcast of a stream API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
simulcastId = "7269209ff0299319b6321c9a6e7850ff" # Replace with actual simulcast ID 
getLiveSimulcast = client.livestreams.get_simulcast(streamId, simulcastId) 
print("Live Stream Simulcast Details:", getLiveSimulcast) 

Update live stream simulcast:

Use the client.livestreams.update_simulcast() method to modify the configuration of a simulcast stream. Provide the streamId, simulcastId, and the fields you wish to update. For more details, refer to the Update a specific simulcast of a stream API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
simulcastId = "7269209ff0299319b6321c9a6e7850ff" # Replace with actual simulcast ID 

updatePayload = { 
    "isEnabled": False, # Disable the simulcast stream (set to True to enable) 
    "metadata": { 
        "simulcast2": "media" # Update the metadata as needed 
    } 
} 

updateLiveSimulcast = client.livestreams.update_simulcast(streamId, simulcastId, updatePayload) 
print("Updated Live Stream Simulcast:", updateLiveSimulcast) 

Delete live stream simulcast:

Use the client.livestreams.delete_simulcast() method to remove a specific simulcast from a live stream. Provide the streamId and simulcastId of the simulcast you wish to delete. For more details, refer to the Delete a simulcast API documentation.


streamId = "a09f3e958c16ed00e85bfe798abd9845" # Replace with actual stream ID 
simulcastId = "7269209ff0299319b6321c9a6e7850ff" # Replace with actual simulcast ID 
deleteLiveSimulcast = client.livestreams.delete_simulcast(streamId, simulcastId) 
print("Deleted Live Stream Simulcast:", deleteLiveSimulcast) 


Contributions

We are accepting open contributions to the repository. Feel free to fork the project, make your changes, and submit a pull request for review.