How to generate Stream URLs

With Dailymotion for Enterprise, you have the freedom to deliver your content the way you want with the multiple solutions we offer!

To keep your content safely hosted at Dailymotion and integrate it with third-party players, you can generate secured stream URLs usable with controlled access on the client side.

This guide will walk you through setting up a service to generate stream URLs and provide them to your end-users.


Overview

  1. Create a Private API Key
  2. Set up a backend service to generate secured stream URLs
  3. Deliver content to your end-users via their unique stream URL

Step 1: Create a Private API Key

In order to authenticate against your Dailymotion account and retrieve an access token, you need to generate a Private API Key, which can be done easily from your Dailymotion Studio:

  1. Log in to your Dailymotion Studio
  2. Go to Organization Settings > API Keys > Create API Key > Private API Key
  3. Under Permissions > Manage videos > toggle on Generate Stream URLs
  4. Click Create
  5. Copy and store your API key and secret in a secure location

    For more details, check our full API Key guide.
If you don’t have access to Private API keys, or if Generate Stream URLs is not available as one of the permissions:

please reach out to your Account Manager or our Support team to turn on the needed features on your account. You may need to upgrade your account to access them.

Step 2: Set up a service to generate stream URLs

To ensure optimal security, we recommend setting up a backend service to handle generating stream URLs for each of your clients: to make sure your content is shared in a secured way and cannot be accessed publicly or leaked, each generated stream URL is tied to the requesting client IP and only valid for a limited time.

Suggested high-level workflow

Sample service on GitHub

To get you started if you don’t have such service yet, we have put together a GitHub repository with basic building blocks to help you set up a sample service.

Please note that this repository is meant to be a starting point: you will likely need to customize it to ensure that it meets all your security requirements and your use cases before deploying it to production.

Sample stream URL service on GitHub

Sample code snippet to generate stream URL

If you already have a backend service of your own, the following code sample can be added within your server architecture to authenticate and generate secured stream URLs:

Python
PHP
import requests
import os

# Please adapt CLIENT_ID and CLIENT_SECRET depending on your server architecture
CLIENT_ID = os.getenv("DAILYMOTION_CLIENT_ID")
CLIENT_SECRET = os.getenv("DAILYMOTION_CLIENT_SECRET")
 
DAILYMOTION_API_BASE_URL = "https://partner.api.dailymotion.com"
 
DAILYMOTION_API_OAUTH_TOKEN_URL = f"{DAILYMOTION_API_BASE_URL}/oauth/v1/token"
DAILYMOTION_API_VIDEO_URL = f"{DAILYMOTION_API_BASE_URL}/rest/video"
 
 
def fetch_dailymotion_api_access_token() -> str:
    response = requests.post(
        url=DAILYMOTION_API_OAUTH_TOKEN_URL,
        data={
            "scope": "read_video_streams",
            "grant_type": "client_credentials",
            "client_id": CLIENT_ID,
            "client_secret": CLIENT_SECRET,
        },
        timeout=2,
    )
 
    return response.json()["access_token"]
 
 
def fetch_stream_urls(
    video_id: str,
    video_formats: str,
    client_ip: str,
):
    response = requests.get(
        url=f"{DAILYMOTION_API_VIDEO_URL}/{video_id}",
        params={
            "client_ip": client_ip,
            "fields": video_formats,
        },
        headers={
            "Authorization": f"Bearer {fetch_dailymotion_api_access_token()}",
        },
        timeout=2,
    )
 
    return response.json()
<?php
 
# Please adapt CLIENT_ID and CLIENT_SECRET depending on your server architecture
define("CLIENT_ID", getenv("DAILYMOTION_CLIENT_ID"));
define("CLIENT_SECRET", getenv("DAILYMOTION_CLIENT_SECRET"));
 
const DAILYMOTION_API_BASE_URL = "https://partner.api.dailymotion.com";
 
const DAILYMOTION_API_OAUTH_TOKEN_URL = DAILYMOTION_API_BASE_URL . "/oauth/v1/token";
const DAILYMOTION_API_VIDEO_URL = DAILYMOTION_API_BASE_URL . "/rest/video";
 
function fetchDailymotionApiAccessToken(): string {
    $ch = curl_init(DAILYMOTION_API_OAUTH_TOKEN_URL);
 
    curl_setopt_array(
        $ch,
        [
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => [
                "scope"=>"read_video_streams",
                "grant_type"=> "client_credentials",
                "client_id" => CLIENT_ID,
                "client_secret" => CLIENT_SECRET,
            ],
            CURLOPT_TIMEOUT => 2,
            CURLOPT_RETURNTRANSFER => true,
        ]
    );
 
    $response = curl_exec($ch);
 
    curl_close($ch);
 
    return json_decode($response, true)["access_token"];
}
 
function fetchStreamUrls(string $videoId, string $videoFormats, string $clientIp): array {
    $ch = curl_init(
        DAILYMOTION_API_VIDEO_URL . "/{$videoId}"
        . "?client_ip={$clientIp}"
        . "&fields={$videoFormats}"
    );
 
    curl_setopt_array(
        $ch,
        [
            CURLOPT_HTTPHEADER => ["authorization: Bearer " . fetchDailymotionApiAccessToken()],
            CURLOPT_TIMEOUT => 2,
            CURLOPT_RETURNTRANSFER => true,
        ]
    );
 
    $response = curl_exec($ch);
 
    curl_close($ch);
 
    return json_decode($response, true);
}

Once the API request is finished, the server returns a JSON response containing the stream URL of the requested video.

Step 3: Deliver content to your end-users using their unique stream URL

Now that you retrieved the stream URL for the intended video, you can integrate it in your third-party player solution to provide your end-users with the requested content.

Example: see below how a stream URL can be added in an HTML5 player.

# Integrate the retrieved stream URL in a player
 
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="referrer" content="no-referrer">
  <title>Demo</title>
</head>
<body>
  <video-js id="demo" controls></video-js>  
  <link href="https://vjs.zencdn.net/7.21.1/video-js.css" rel="stylesheet" />
  <script src="https://vjs.zencdn.net/7.21.1/video.min.js"></script>
  <script type="text/javascript">
    const apiUrl = '/stream-urls'
    const videoPlayer = videojs('demo')
 
    fetch(`${apiUrl}?video_id=<your_video_xid_here>&video_formats=stream_h264_url`)
      .then((res) => res.json())
      .then((data) => {
        videoPlayer.src(data.stream_h264_url)
      })
  </script>
</body>
</html>

Resources

List of supported formats

You can generate stream URLs in any of our supported video formats by passing the desired API parameter. 

Supported manifest
ManifestQualityAPI parameterResolutionW x H
HLS (m3u8)Dynamically adjustedstream_hls_urlDynamically adjustedDynamically adjusted
Live HLS (m3u8)Dynamically adjustedstream_live_hls_urlDynamically adjustedDynamically adjusted
Supported formats
FormatQualityAPI parameterResolutionW x H
H264 (mp4)stream_h264_url380p512 x 384
H264 (mp4)hqstream_h264_hq_url480p720 x 480
H264 (mp4)hdstream_h264_hd_url720p1280 x 720
H264 (mp4)hd1080stream_h264_hd1080_url1080p1920 x 1080
H264 (mp4)qhdstream_h264_qhd_url1440p2560 x 1440
H264 (mp4)uhdstream_h264_uhd_url2160p3840 x 2160

Postman Collection

You can try the API call to generate stream URLs in a more graphical user interface via our ready-to-use Postman Collection. It includes the API request filled with the endpoint and parameters to authenticate and generate a stream URL.

All you need to do is filling the variable fields with your credentials and parameters values!


Download files from Dailymotion

If you need to download a file from a stream URL, follow these steps:

  1. Generate a H264 stream URL in the desired format (following steps 1 & 2 above)
  2. Append the retrieved stream URL with download=1 parameter to enable file download.
  3. Download the file using the link
# BASH example
wget https://www.dailymotion.com/cdn/H264-512x384/video/x730nnd.mp4?sec=4dXW1UzVoHWu7YyRlEXvXn_WQDTROD8sPgKTcnUems38Mmhktq1YlZF4jT4GshGm&download=1