Blog | Upload a video with the api.video Python Client

Uploading video with api.video’s Python Client is easy. You can use the method described here to upload big files over 199 MiB or small files. The client will handle chunking the upload for you.

NOTE: If you’re wondering how this differs from the previous Python tutorials:

It differs in that it uses our new Python Client.

If you’d prefer to watch a video about how to upload video from your computer with the new client, you can check that out here:

The API reference documentation for the endpoints used in this tutorial can be reviewed here:

If you prefer written walkthroughs, read on. I’ll cover all the same information here.

Install the Python Client

Installing api.video’s Python Client is easy:

pip install api.video

I recommend using a virtual environment to install, but it’s not required.

How to Upload a Video from Your Computer

What you’ll be doing, is sending a request to the api.video server to create a video container. You’ll get back a video ID you can use to reference your container. Then you’ll prepare your video for upload and send a request to upload the video to the video ID you specify.

Be aware that you can only upload to a video ID once. You can change settings and details about your video later using the update feature of the api.video API.

You can get the code sample I’m using here:
https://github.com/apivideo/python-api-client/blob/master/examples/upload_video.py

When you are creating your video, you may want to include more settings and metadata than I show in the walkthrough. A complete list of choices are shown here:
https://github.com/apivideo/python-api-client/blob/master/docs/VideoCreationPayload.md

And here’s the code sample (tweaked a bit from the repository sample):

import

apivideo

from

apivideo

.

apis

import

VideosApi

from

apivideo

.

exceptions

import

ApiAuthException

api_key

=

"your api key here"

# Set up the authenticated client

client

=

apivideo

.

AuthenticatedApiClient

(

api_key

)

# if you rather like to use the sandbox environment:

# client = apivideo.AuthenticatedApiClient(api_key, production=False)

client

.

connect

(

)

videos_api

=

VideosApi

(

client

)

# Create the payload with video details

video_create_payload

=

{

"title"

:

"Client Video Test"

,

"description"

:

"Client test"

,

"public"

:

False

,

"tags"

:

[

"bunny"

,

"tree"

]

}

# Create the container for your video and print the response

response

=

videos_api

.

create

(

video_create_payload

)

print

(

"Video Container"

,

response

)

# Retrieve the video ID, you can upload once to a video ID

video_id

=

response

[

"video_id"

]

# Prepare the file you want to upload. Place the file in the same folder as your code.

file

=

open

(

"sample-mov-file.mov"

,

"rb"

)

# Upload your video. This handles videos of any size. The video must be in the same folder as your code.

# If you want to upload from a link online, you need to add the source parameter when you create a new video.

video_response

=

videos_api

.

upload

(

video_id

,

file

)

print

(

"Uploaded Video"

,

video_response

)

Code Sample Walkthrough

To use the api.video Python Client, you’ll want to start by making your imports. You need three. The first one you’ll need to import is apivideo. This will let you use authentication and the client. Next, you want to import VideosApi from apivideo.apis. It is best practice to import just the part of the client you’ll use, because it improves performance. VideosApi will let us use the create and upload features. Finally, you’ll import ApiAuthException for error handling.

import

apivideo

from

apivideo

.

apis

import

VideosApi

from

apivideo

.

exceptions

import

ApiAuthException

Next, we’ll add our API key and set up the Client. In the Python Client, authentication is handled for us, including refreshing the access token when needed. All we need to do is add our API key to the Client, then connect. After connecting, we can pass the Client to VideosApi, and we’re ready to upload our video!

api_key

=

"your api key here"

# Set up the authenticated client

client

=

apivideo

.

AuthenticatedApiClient

(

api_key

)

# if you rather like to use the sandbox environment:

# client = apivideo.AuthenticatedApiClient(api_key, production=False)

client

.

connect

(

)

videos_api

=

VideosApi

(

client

)

To upload our video, we first want to create our video container. The video container contains details about the video as well as any configuration settings like whether you want your video to be public or downloadable. As part of our request to create our container, we’ll want to send all those details and settings in a payload. For this walkthrough, I’m adding a title because that’s the only required setting for container creation. Then I’m including a description of what my video is, setting my video to be private (make public = false) and then I’m adding tags to search with – bunny and tree.

video_create_payload

=

{

"title"

:

"Client Video Test"

,

"description"

:

"Client test"

,

"public"

:

False

,

"tags"

:

[

"bunny"

,

"tree"

]

}

If you want to change the settings further, refer to the choices here: https://github.com/apivideo/python-api-client/blob/master/docs/VideoCreationPayload.md

Let’s create a request to make our video container and retrieve the video ID:

# Create the container for your video and print the response

response

=

videos_api

.

create

(

video_create_payload

)

print

(

"Video Container"

,

response

)

# Retrieve the video ID, you can upload once to a video ID

video_id

=

response

[

"video_id"

]

We grabbed the video ID, so next we prepare our file. To upload files with api.video, we open them in read-only binary format. Then we’ll add our file and video ID to our upload request and check out the response!

# Prepare the file you want to upload. Place the file in the same folder as your code.

file

=

open

(

"sample-mov-file.mov"

,

"rb"

)

# Upload your video. This handles videos of any size. The video must be in the same folder as your code.

# If you want to upload from a link online, you need to add the source parameter when you create a new video.

video_response

=

videos_api

.

upload

(

video_id

,

file

)

print

(

"Uploaded Video"

,

video_response

)

If everything works out, you’ll get a response showing you all the metadata and configuration details for your upload. You can navigate to your api.video dashboard to see the video you uploaded there. If you’re uploading a big video, give it a few minutes to be ready.

If you get stuck or have questions, you can write us on our community forum.

Erikka Innes

Erikka Innes

Developer Evangelist