Upload a video to api.video using a public URL

Upload a video to api.video using a public URL

Video upload with the api.video API. Move a video from a public URL to api.video using Python.

Erikka Innes

February 7, 2021

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

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

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:

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'
  }
}

And that's how you do a simple video upload to api.video! Let us know if you have any questions in our Community! Happy building!