Skip to content
Stream
Visit Stream on GitHub
Set theme to dark (⇧+D)

Upload video file

Basic Uploads (for small videos)

For files smaller than 200MB you can use simple form based uploads. This is an easy way to upload but does not support resumable uploading.

Make an HTTP request with content-type header set to multipart/form-data and include the media as an input with the name set to file.

cURL example

curl -X POST \-H "Authorization: Bearer $TOKEN" \-F file=@/Users/kyle/Desktop/video.mp4 \https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream

Resumable uploads with tus (for large files)

What is tus?

tus is a protocol based on HTTP for resumable file uploads. Resumable means that an upload can be interrupted at any moment and can be resumed without re-uploading the previous data again. An interruption may happen willingly, if the user wants to pause, or by accident in case of an network issue or server outage.

tus protocol is the recommended method for uploading large files to Cloudflare Stream from a computer. Popular programming languages have tus client implementations.

Specifying upload options

The tus protocol allows you to add optional parameters in the Upload-Metadata header.

Supported options in "Upload-Metadata"

  • requiresignedurls

    • If this key is present, the video playback for this video will be required to use signed urls after upload.
  • allowedorigins

  • thumbnailtimestamppct

    • Specify the default thumbnail timestamp percentage. Note that percentage is a floating point value between 0.0 and 1.0.
  • watermark

    • The watermark profile UID.

Command-line example

You will also need to download a tus client. This tutorial will use the tus python client, available through pip, pythons's package manager.

pip install -U tus.py
tus-upload --chunk-size 5242880 --header Authorization "Bearer $TOKEN" $PATH_TO_VIDEO https://api.cloudflare.com/client/v4/accounts/$ACCOUNT/stream

In the beginning of the response from tus, you’ll see the endpoint for getting information about your newly uploaded video.

INFO Creating file endpointINFO Created: https://api.cloudflare.com/client/v4/accounts/d467d4f0fcbcd9791b613bc3a9599cdc/stream/dd5d531a12de0c724bd1275a3b2bc9c6...

Golang Example

To get started, import a tus client. You can use the go-tus by eventials to upload from your Go applications.

package main
import (    "net/http"    "os"
    tus "github.com/eventials/go-tus")
func main() {    accountID := "ACCOUNT ID"
    f, err := os.Open("videofile.mp4")
    if err != nil {        panic(err)    }
    defer f.Close()
    headers := make(http.Header)    headers.Add("Authorization", "Bearer $TOKEN")
    config := &tus.Config{        ChunkSize:           5 * 1024 * 1024, // Cloudflare Stream requires a minimum chunk size of 5MB.        Resume:              false,        OverridePatchMethod: false,        Store:               nil,        Header:              headers,        HttpClient:          nil,    }
    client, _ := tus.NewClient("https://api.cloudflare.com/client/v4/accounts/"+ accountID +"/stream", config)
    upload, _ := tus.NewUploadFromFile(f)
    uploader, _ := client.CreateUpload(upload)
    uploader.Upload()}

You can also get the progress of the upload if you're running the upload in a goroutine.

// returns the progress percentage.upload.Progress()
// returns whether or not the upload is complete.upload.Finished()

Please see go-tus on GitHub for functionality such as resuming uploads and getting more details about the progress of the upload.

Node.js Example

Install tus-js-client

npm install tus-js-client

Set up an index.js and configure:

  • API endpoint with your Cloudflare Account ID
  • Request headers to include a API token
var fs = require("fs");var tus = require("tus-js-client");
// specify location of file you'd like to upload belowvar path = __dirname + "/test.mp4";var file = fs.createReadStream(path);var size = fs.statSync(path).size;
var options = {  endpoint: "https://api.cloudflare.com/client/v4/accounts/{ACCOUNT ID}/stream",  headers: {    'Authorization': 'Bearer $TOKEN',  },  chunkSize: 5 * 1024 * 1024, // Cloudflare Stream requires a minimum chunk size of 5MB.  resume: true,  metadata: {    filename: "test.mp4",    filetype: "video/mp4",    defaulttimestamppct: 0.5,    watermark: "$WATERMARKUID"  },  uploadSize: size,  onError: function (error) {    throw error;  },  onProgress: function (bytesUploaded, bytesTotal) {    var percentage = (bytesUploaded / bytesTotal * 100).toFixed(2);    console.log(bytesUploaded, bytesTotal, percentage + "%");  },  onSuccess: function () {    console.log("Upload finished:", upload.url);    var index = upload.url.lastIndexOf("/") + 1;    var mediaId = upload.url.substr(index)    console.log("Media id:", mediaId);  }};
var upload = new tus.Upload(file, options);upload.start();