Tutorials · 8 min read

Photo by <a href="https://www.pexels.com/photo/silver-imac-displaying-collage-photos-1779487/">Designecologist</a> from Pexels

Upload a video from your computer with the api.video API (Python)

When you upload videos from your computer, you must use a two step process. First you create a container for your video, then you upload your video into your container. This code sample shows you how!

Erikka Innes

February 10, 2021

If you have some videos on your computer that you need to upload to api.video, you're in luck - today's tutorial will show you how to do just that using Python. Your video must be under 200 MiB to use this method. The code sample is available on github in the api.video python-examples repository. Let's get started!

computer screen

Prerequisites

  • An api.video account
  • api.video API key
  • You'll need access to Python and you'll need to install the requests library if you don't already have it added

What You'll Be Doing

This is a quick summary of what you're going to do to upload your video.

  1. You'll authenticate with api.video using your API key and retrieve a token that lasts one hour that you can use to access all of api.video's other endpoints.
  2. You'll create a video container for your video and retrieve the videoId.
  3. You'll use the videoId to upload your video from your computer to api.video. You can only upload a video to the videoId once. If you decide you wanted to upload a different video, you'll have to go through the create container and upload video steps again. You don't need to repeat the part where you get a new token unless your token expires (so if you take an hour long break and come back, you'll need to redo that step then).

Code Sample

## How to upload a video under 200 MiB using the api.video API

import requests

## Set up variables for endpoints
auth_url = "https://ws.api.video/auth/api-key"
create_url = "https://ws.api.video/videos"

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

payload = {
    "apiKey": "your API key here"
}

response = requests.request("POST", auth_url, json=payload, headers=headers)
response = response.json()
token = response.get("access_token")

auth_string = "Bearer " + token


## Set up headers for authentication
headers_bearer = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": auth_string
}

## Create a video container
payload2 = {
    "title": "Demo Vid from my Computer",
    "description": "Video upload of Big Buck Bunny to demo how to do an upload from a folder on your computer."
}

response = requests.request("POST", create_url, json=payload2, headers=headers_bearer)
response = response.json()
videoId = response["videoId"]

## Create endpoint to upload video to
upload_url = upload_url + "/" + videoId + "/source"

## Create upload video headers 
headers_upload = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": auth_string
}

file = {"file": open("local/path/to/myvideo.flv", "rb")}
response = requests.request("POST", upload_url, files=file, headers=headers_upload)
json_response = response.json()
print(json_response)

Code Walkthrough

You'll start by adding the requests library, so you can easily make HTTP requests.

How people added libraries before computers did it for you.


import requests

Next, let's assign a variable to each endpoint we'll use, they'll be a little easier to work with. We're going to assign two of them here, and we'll use the second one later to build the third one:

  • Authentication endpoint - This is where we send our API key to exchange it for a token. It's auth_url in the code sample.
  • Videos endpoint - Specifically we'll use the feature on this endpoint that's for creating a video. It's create_url in the code sample.
  • Video Upload endpoint - It looks a lot like the other endpoint. It's create_url + "/videoId" + "/source". We'll construct this after we get a videoId to work with.
auth_url = "https://ws.api.video/auth/api-key"
create_url = "https://ws.api.video/videos"

Authentication - Get Your Token

We'll prepare our headers and payload for authentication, then send a POST request to api.video. With the api.video API you start authentication by sending a request using your API key to retrieve a token. You can use your sandbox or your production key, the backend will figure out which you are using and handle it accordingly. If you choose to use your sandbox credentials for this walkthrough, your video will be uploaded with the watermark 'FOR DEVELOPMENT PURPOSES ONLY.':

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

payload = {
    "apiKey": "your API key here"
}
response = requests.request("POST", auth_url, json=payload, headers=headers)

Now we need to get the token from our response, so we can authenticate with the rest of api.video's endpoints when we want to do something. To get the token, we'll convert the response to JSON, then retrieve the token out of the JSON dictionary.

response = response.json()
token = response.get("access_token")

Set up a Video Container

Using the token, we'll get the headers and payload ready for our next request, which will be to ask api.video to create a video container for us.

## Set up headers for authentication
headers_bearer = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": auth_string
}

## Create a video container
payload2 = {
    "title": "Demo Vid from my Computer",
    "description": "Video upload of Big Buck Bunny to demo how to do an upload from a folder on your computer."
}

api.video endpoints use bearer authentication. Add the authorization header for bearer authentication to your list of headers, and then for the payload we keep it simple, just a title and description. But what about the path to your file? Don't worry we're just creating a container right now. When you upload from your computer, you create a container first, and do your upload second.

Send Your Request and Grab the VideoId

For the next part of our walkthrough, we'll send our request, convert the response to JSON, and retrieve the videoId. We need the videoId so we can tell api.video what container we want to upload our video to.

response = requests.request("POST", create_url, json=payload2, headers=headers_bearer)
response = response.json()
videoId = response["videoId"]

Create the Endpoint for Uploading Your Video

Set up the endpoint you'll upload your video to, which is composed of https://ws.api.video/videos + / + videoId + /source. You also need to add the headers. For Python, you don't want to use the "Content-Type": multipart/form-data header you see listed if you try the API explorer. Because you are doing an upload with only one part, it won't work. Omit this in your headers. Set things up like this:

headers_upload = {
    "Accept": "application/vnd.api.video+json",
    "Authorization": auth_string
}

For the payload, you're sending a video file, so you'll have to open the file, attach it to a dictionary, and for readability, we assign the dictionary to a variable. We use "rb" because we want to open the file as read-only binary format. Check it out:

file = {"file": open("sample-flv-file.flv", "rb")}

We can assign our variable in our request to upload our video, and then process and print the request response:

response = requests.request("POST", upload_url, files=file, headers=headers_upload)
json_response = response.json()
print(json_response)

The response provides a lot of detail about your video:

{
	'videoId': 'video ID here', 
	'title': 'Demo Vid from my Computer', 
	'description': 'Video Upload From My Computer', 
	'public': True, 
	'panoramic': False, 
	'mp4Support': True, 
	'publishedAt': '2021-02-05T04:25:02+00:00', 
	'createdAt': '2021-02-05T04:25:02+00:00', 
	'updatedAt': '2021-02-05T04:25:02+00:00', 
	'tags': [], 
	'metadata': [], 
	'source': {
		'type': 'upload', 
		'uri': '/videos/video_ID_here/source'
	 }, 
	'assets': {
		'iframe': '<iframe src="https://embed.api.video/vod/video_ID_here" width="100%" height="100%" frameborder="0" scrolling="no" allowfullscreen="true"></iframe>', 
		'player': 'https://embed.api.video/vod/video_ID_here', 
		'hls': 'https://cdn.api.video/vod/video_ID_here/hls/manifest.m3u8',
		'thumbnail': 'https://cdn.api.video/vod/video_ID_here/thumbnail.jpg'
 }
}
  • videoId - the unique identifier for your new video. Before it was just referencing a container, now it's referencing a container that holds your video!
  • title - the title you provided for your video when you made the container
  • description - a description of what the video contains
  • public - by default, your video is public
  • panoramic - by default, your video isn't listed as being a 360° (panoramic is the term api.video uses for 360° or immersive video)
  • mp4Support - whether you have an mp4 version of your video available or not. By default you do. If you turn this off, your video will not be downloadable from a player and there won't be a link to an mp4 version of your file.
  • publishedAt - the time your video was published, in ISO-8601 format.
  • createdAt - the time your video was created, in ISO-8601 format.
  • updatedAt - the time your video was updated in ISO-8601 format.
  • tags - you can add individual key words to categorize your videos and make them easier to search if you have a lot of them.
  • metadata - you can add a dictionary of key value pairs that categorize your videos for searches.
  • source - this lists the video type, which is upload, and the link you add to the api endpoint to get to your video.
  • assets - this is a list of links you can use to set up different services with your video. You can embed an iframe using the iframe code, use the player link to add the file to a player, get the manifest file if you need the source for live streaming, and api.video selects a thumbnail for you automatically.

You can update all of this information should you choose to. That's it for uploading a video from your computer using Python!

If you have any questions or suggestions, please share them on our community forum and if you still haven't created your api.video account, you can do that here. Happy building!

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.