api.video
SearchCtrl K

Features

Developers

Upload a video to api.video using a public URL

February 8, 2021 - Erikka Innes in Video upload, Authenticate, Python

If Python is your programming language of choice, and you want to quickly upload a video to api.video using a public URL, you've come to the right place. The code sample is available on github in the api.video python-examples repository. Let's get started!


Prerequisites

If you would prefer to watch a video that explains the same information as this tutorial, you can watch this:

You can also review the API reference documentation for uploading videos here: Upload a video

Code Sample

import requests

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

# Set up header for first authentication request
headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

payload = {
    "apiKey": "sandbox or production 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 bearer authentication
headers2 = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": auth_string
}

# Upload the video
payload = {
    "title": "My Demo Video",
    "source": "https://www.learningcontainer.com/mp4-sample-video-files-download/#Sample_AVI_File"
}
response = requests.request("POST", create_url, json=payload, headers=headers2)

# Check out the response
print(response.json())

Code Walkthrough

This section walks through the code to give you an idea of what you'll be doing.

Set up Authentication with Your API Key

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

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 use two endpoints today:

  • 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.
auth_url = "https://ws.api.video/auth/api-key"
create_url = "https://ws.api.video/videos"

Create the payload for your headers and the payload for your payload. These are for your authentication request. The api.video API requires that you authenticate with your API key to obtain a token. Your token is good for one hour and can be used to authenticate with all other endpoints.

NOTE: You can use your sandbox or your production key. The backend for api.video will figure out which you selected and route your request appropriately. If you didn't pay for a full account, all of your videos will be watermarked 'FOR DEVELOPMENT PURPOSES ONLY'.

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

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

Send a request with your header and payload information, convert the response to json so you can retrieve information from it, and then grab access_token.

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

Now you have your token, you need to set new headers that are for Bearer authentication. Create a string with your token for the new Authorization header, and a new header payload.

auth_string = "Bearer " + token

headers2 = {
    "Accept": "application/json",
    "Content-Type": "application/json",
    "Authorization": auth_string
}

Send Your Video Upload Request

And the part you've been waiting for, the code for video upload! By default, your video is public and includes mp4 support, which is a link to a 1080p mp4 of your file. If you want to change these settings so your video is private or mp4 support is turned off (this will make it so no one can download your video from a player) you can check our docs for details about all the parameters your payload can include. For now, we'll keep the payload simple:

payload = {
    "title": "My Demo Video",
    "source": "https://www.learningcontainer.com/mp4-sample-video-files-download/#Sample_AVI_File"
}
response = requests.request("POST", create_url, json=payload, headers=headers2)
print(response.json())

You can use this payload to see the sample run. For your own upload, you may want to use a different title and a different source link.

Review Video Details in the Response

When you print the response here, you get back a lot of interesting information about your video:

{'videoId': 'vi8PXlHuWN25wPOVtGfBSQkJ', 
'title': 'My Demo Video', 
'description': '', 
'public': True, 
'panoramic': False, 
'mp4Support': True, 
'publishedAt': '2021-02-04T01:50:23+00:00', 
'createdAt': '2021-02-04T01:50:23+00:00', 
'updatedAt': '2021-02-04T01:50:23+00:00', 
'tags': [], 
'metadata': [], 
'source': {
	'type': 'upload', 
	'uri': '/videos/vi8PXlHuWN25wPOVtGfBSQkJ/source'
	}, 
'assets': {
	'iframe': '<iframe src="https://embed.api.video/vod/vi7EVlHuWN05wPOVtGfBSQkJ" width="100%" height="100%" frameborder="0" scrolling="no" allowfullscreen="true"></iframe>', 
	'player': 'https://embed.api.video/vod/vi8PXlHuWN25wPOVtGfBSQkJ', 
	'hls':'https://cdn.api.video/vod/vi8PXlHuWN25wPOVtGfBSQkJ/hls/manifest.m3u8',   'thumbnail': 'https://cdn.api.video/vod/vi8PXlHuWN25wPOVtGfBSQkJ/thumbnail.jpg'
  }
}
  • videoId - a unique ID that identifies your uploaded video
  • title - the title of your video
  • description - if you put a description of your video it appears here
  • public - whether your video is public or not
  • panoramic - whether your video is 360° or not
  • mp4Support - whether your video has an mp4 version of it available or not (turn this off if you don't want people to be able to download your video from a player)
  • publishedAt - when you first published your uploaded video
  • createdAt - when your uploaded video was created
  • updatedAt - when you last updated your video
  • tags - if you added individual tags to categorize your video with, they appear here
  • metadata - if you added key value pairs to categorize your videos with, they appear here
  • source - details about your video source
  • assets - different ways you can upload or work with your new video, there's a link you can use with a player, the source file for the hls versio of your video, and a thumbnail that's selected for you (you can change it if you like, but that's a different tutorial)

And that's how you do a simple video upload to api.video!

You May Also Like...

Erikka Innes

Developer Evangelist

Create your free account

Start building with video now