Skip to main content

Uploading videos to Bunny Stream

The Bunny Stream documentation is a little vague, so here are instructions for using the API in PHP.

Create Video

Before actually uploading a video, you need to create a video using the video_createvideo endpoint. In PHP the code to do so looks like this:

$client = new \GuzzleHttp\Client();

// libraryId: the ID for the Bunny Stream Library to create the video in
// title: the title for the video
// thumbnailTime, integer, what point in the video in milliseconds to use as a thumbnail
$response = $client->request('POST', 'https://video.bunnycdn.com/library/[libraryId]/videos', [
	'body' => '{"title":"[your video title]","thumbnailTime":[time for thumbnail generation, in millis]',
	'headers' => [
		'AccessKey' => '<your Bunny Stream API access key - your normal Bunny API key will not work>',
		'accept' => 'application/json',
		'content-type' => 'application/*+json',
	],
]);

$result = $response->getBody();
// Bunny Stream API returns result as a JSON string
$result = json_decode($result);
// The new video ID, if the request was successful
$guid = $result->guid;

Upload video

If the video was created successfully, you can now upload it using the video_uploadvideo endpoint:

$yourVideoLocalFilePath = "path/to/yourvideo.mp4";
$videoGuid = "[the GUID you got after creating the video in the previous step]";

// Open video file for reading in binary form
$videoFileBody = fopen($yourVideoLocalFilePath, "rb");
// Get the filesize of the video so we can read it correctly
$fileSize = filesize($yourVideoLocalFilePath);
// Read the video file into a string
$videoFileBodyString = fread($videoFileBody, $fileSize);
$response = $client->request('PUT', 'https://video.bunnycdn.com/library/[libraryId]/videos/' . $videoGuid, [
	'body' => $videoFileBodyString,
	'headers' => [
		'AccessKey' => '<your Bunny Stream API key>',
		'accept' => 'application/json',
	],
]);