Dailymotion offered an Application Programming Interface (API) that allowed developers to interact with and access various features of the Dailymotion platform programmatically.
This API enabled developers to integrate Dailymotion’s video content, user accounts, and other functionalities into their applications, websites, or services.
30-60 Minutes to read
Dailymotion Player SDK
The JavaScript SDK api.dmcdn.net/all.js
is available on GitHub and must be loaded from https://api.dmcdn.net/all.js
. This SDK on Dailymotion CDN provide access to the features of the Dailymotion Data API.
Dailymotion does not longer support using this JS SDK for Player integration and should only be used for interaction with the Data API. The Player can be embedded with any of our embed methods that can be found here. |
Loading the SDK
You can load the SDK using a <script>
element and by calling DM.init()
method. Below is an example of initializing the SDK with all the common options turned on:
<script src="https://api.dmcdn.net/all.js"></script>
<script>
DM.init({
apiKey: 'YOUR API KEY',
status: true, // check login status
cookie: true // enable cookies to allow the server to access the session
});
</script>
Loading the SDK asynchronously
The most efficient way to load the SDK in your site is to load it asynchronously so it does not block loading other elements of your page. This is particularly important to ensure fast page loads for users and SEO robots/spiders. Below outlines an example:
<script>
window.dmAsyncInit = function()
{
DM.init({ apiKey: 'your app id', status: true, cookie: true });
};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = 'https://api.dmcdn.net/all.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(e, s);
}());
</script>
In the example above, the function assigned to window.dmAsyncInit is run as soon as the Dailymotion JavaScript SDK is loaded. Any code you want to run after the SDK is loaded should be placed within this function and after the call to DM.init(). For example, this is where you would test the logged in status of the user or subscribe to any Dailymotion events your application is interested in.
Authentication
The Dailymotion JavaScript SDK allows you to log your users with their Dailymotion account in order to act on their behalf: access content, publish or edit videos, etc. For this purpose, you can use any of:
Method | Method description |
---|---|
Login and/or request extended permissions. | |
Logout (only if the user is connected with your site). | |
Get current login status from dailymotion.com. | |
Synchronous accessor for the current session. |
In addition, you can subscribe to the following events using DM.Event.subscribe()
:
-
auth.statusChange
-
auth.sessionChange
-
auth.login
-
auth.logout
Graph API
You can make Graph API requests using DM.api()
method:
var handleAPIResponse = function (response) {
alert('Name is ' + response.screenname);
};
DM.api('/user/rs', {
fields: 'screenname'
}, handleAPIResponse);
Dailymotion Player API
Find all Player API methods below.
DM.play()
function() {
this.api('play');
}
DM.togglePlay()
function() {
this.api('toggle-play');
}
DM.pause()
function() {
this.api('pause');
}
DM.seek()
function(time) {
this.api('seek', time);
}
DM.load()
function(id, settings) {
this.api('load', id, settings);
}
DM.setMuted()
function(muted) {
this.api('muted', muted);
}
DM.toggleMuted()
function() {
this.api('toggle-muted')
}
DM.setVolume()
function(volume) {
this.api('volume', volume);
}
DM.setQuality()
function(quality) {
this.api('quality', quality);
}
DM.setSubtitle()
function(subtitle) {
this.api('subtitle', subtitle);
}
DM.setFullscreen()
function(fullscreen) {
this.api('fullscreen', fullscreen);
}
DM.setControls()
function (visible) {
this.api('controls', visible);
}
DM.toggleControls()
function () {
this.api('toggle-controls');
}
DM.setProp()
function() {
this.api.apply(
this, ['set-prop'].concat([].slice.call(arguments)));
}
}
DM.setAdsConfig()
function (config) {
this.api("set-ads-config", config);
}
DM.setCustConfig()
function (config) {
this.api("set-ads-config", config);
}
DM.watchOnSite()
function(muted) {
this.api('watch-on-site');
}
DM.setLoop()
function (loop) {
this.api('loop', loop);
}
DM.api()
The JavaScript SDK allows you to build rich applications that can make API calls against the Dailymotion servers directly from the user’s browser. This can improve performance in many scenarios, as compared to making all calls from your server. It can also help reduce, or eliminate the need to proxy the requests through your own servers, freeing them to do other things.
DM.api(path, method, params, callback)
Parameters
Except path
, all arguments to this method are optional.
Name | Type | Description |
---|---|---|
|
| The resource path. |
|
| The HTTP method (default “get”). |
|
| The parameters for the query. |
|
| The callback function to handle the response. |
Examples
If you have an authenticated user, get their User Object:
var handleAPIResponse = function (response) {
alert('Your name is ' + user.screenname);
};
DM.api('/me', {
fields: 'screenname'
}, handleAPIResponse);
Get the 3 most recent posted video from the authenticated user:
var handleAPIResponse = function(response) {
alert('Got ' + response.list.length + ' videos' + (response.has_more ? ' and has more' : ''));
};
DM.api('/me/videos', {
limit: 3
}, handleAPIResponse);
Search for videos with “javascript tutorial” query:
var handleAPIResponse = function(response) {
alert(response.list[0].title);
};
DM.api('/videos', {
search: 'javascript tutorial',
fields: 'title'
}, handleAPIResponse);
If you have an authenticated user with write permission scope and you want to like a video for them:
var videoId = 'xk2jd2';
var handleAPIResponse = function(response) {
if (!response || response.error)
{
alert('Error occured');
}
else
{
alert('Liked successfuly');
}
};
DM.api('/me/favorites/' + videoId, 'post', handleAPIResponse);
DM.getLoginStatus()
Find out the current status from the server, and get a session if the user is connected.
The user’s status or the question of who is the current user is the first thing you will typically start with. For the answer, we ask dailymotion.com. Dailymotion will answer this question in one of two ways:
-
Someone you don’t know.
-
Someone you know and have interacted with. Here’s a session for them.
Here is how you find out:
DM.getLoginStatus(function(response)
{
if (response.session)
{
// logged in and connected user, someone you know
}
else
{
// no user session available, someone you dont know
}
});
The example above will result in the callback being invoked once on load based on the session from www.dailymotion.com. JavaScript applications are typically written with heavy use of events, and the SDK encourages this by exposing various events. These are fired by the various interactions with authentication flows, such as DM.login().
Events
Event name | Event description |
---|---|
| This event is fired when your application first notices the user (in other words, gets a session when it didn’t already have a valid one). |
| This event is fired when your application notices that there is no longer a valid user (in other words, it had a session but can no longer validate the current user). |
| This event is fired for any auth related change as they all affect the session: login, logout, session refresh. Sessions are refreshed over time as long as the user is active with your application. |
| Typically you will want to use the auth.sessionChange event. But in rare cases, you want to distinguish between these three states: Connected Logged into Dailymotion but not connected with your application * Not logged into Dailymotion at all. |
The DM.Event.subscribe and DM.Event.unsubscribe functions are used to subscribe to these events. For example:
DM.Event.subscribe('auth.login', function(response)
{
// do something with response
});
The response object returned to all these events is the same as the response from DM.getLoginStatus, DM.login or DM.logout. This response object contains:
Property | Property description |
---|---|
| The status of the user. One of |
| The session object. |
Parameters
Name | Type | Description |
---|---|---|
| Function | The callback function to handle the response. |
DM.getSession()
Synchronous accessor for the current Session. The synchronous nature of this method is what sets it apart from the other login methods. It is similar in nature to DM.getLoginStatus(), but it just returns the session. Many parts of your application already assume the user is connected with your application. In such cases, you may want to avoid the overhead of making asynchronous calls.
Note: You should never use this method at page load time. Generally, it is safer to use DM.getLoginStatus() if you are unsure.
Returns: The current session if available, null otherwise.
DM.login()
Once you have determined the user’s status, you may need to prompt the user to login. It is best to delay this action to reduce user friction when they first arrive at your site. You can then prompt and show them the “Connect with Dailymotion” button bound to an event handler which does the following:
DM.login(function(response)
{
if (response.session)
{
// user successfully logged in
}
else
{
// user cancelled login
}
});
You should only call this on a user event as it opens a popup. Most browsers block popups, unless they were initiated from a user event, such as a click on a button or a link.
Depending on your application’s needs, you may need additional permissions from the user. A large number of calls do not require any additional permissions, so you should first make sure you need a permission. This is a good idea because this step potentially adds friction to the user’s process. Another point to remember is that this call can be made even after the user has first connected. So you may want to delay asking for permissions until as late as possible:
DM.login(function(response)
{
if (response.session)
{
if (response.session.scope)
{
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
}
else
{
// user is logged in, but did not grant any permissions
}
}
else
{
// user is not logged in
}
}, {scope: 'read write'});
Name | Type | Description |
---|---|---|
|
| The callback function to handle the response |
|
| Options to modify login behavior (optional). scope: Space separated list of permissions. |
DM.logout()
Logout the user in the background.
Just like logging in is tied to dailymotion.com, so is logging out – and this call logs the user out of both Dailymotion and your site. This is a simple call:
DM.logout(function(response)
{
// user is now logged out
});
You can only log out a user that is connected to your site. |
Parameters
Name | Type | Description |
---|---|---|
|
| The callback function to handle the response. |
DM.Event.subscribe()
Subscribe to a given event name, invoking your callback function whenever the event is fired.
For example, suppose you want to get notified whenever the session changes:
DM.Event.subscribe('auth.sessionChange', function(response)
{
// do something with response.session
});
Parameters
Name | Type | Description |
---|---|---|
|
| Name of the event. |
|
| The handler function. |
DM.Event.unsubscribe()
Removes subscribers, inverse of DM.Event.subscribe.
Removing a subscriber is basically the same as adding one. You need to pass the same event name and function to unsubscribe that you passed into subscribe. If we use a similar example to DM.Event.subscribe, we get:
var onSessionChange = function(response)
{
// do something with response.session
};
DM.Event.subscribe('auth.sessionChange', onSessionChange);
// sometime later in your code you dont want to get notified anymore
DM.Event.unsubscribe('auth.sessionChange', onSessionChange);
Parameters
Name | Type | Description |
---|---|---|
|
| Name of the event. |
|
| The handler function. |
Events
All events supported by the API.
apiready
if (this.apiReady) return /* dispatch only once */;
start
this.ended = false;
loadedmetadata
this.error = null;
timeupdate
this.error = null;
ad_timeupdate
this.currentTime = parseFloat(event.time);
progress
this.bufferedTime = parseFloat(event.time);
durationchange
this.duration = parseFloat(event.duration);
seeking
this.seeking = true;
this.currentTime = parseFloat(event.time);
seeked
this.seeking = false;
this.currentTime = parseFloat(event.time);
fullscreenchange
this.fullscreen = DM.parseBool(event.fullscreen);
controlschange
this.controls = DM.parseBool(event.controls);
volumechange
this.volume = parseFloat(event.volume);
this.muted = DM.parseBool(event.muted);
ad_start
this.adData = event.adData;
video_start
ad_play
playing
ad_start
this.paused = false;
end
// no break, also set paused
this.ended = true;
ad_end
this.adData = {};
ad_pause
video_end
pause
this.paused = true;
error
this.error = {
code: event.code,
title: event.title,
message: event.message
};
rebuffer
this.rebuffering = DM.parseBool(event.rebuffering);
qualitiesavailable
this.qualities = event.qualities;
qualitychange
this.quality = event.quality;
subtitlesavailable
this.subtitles = event.subtitles;
subtitlechange
this.subtitle = event.subtitle;
videochange
this.video = {
videoId: event.videoId,
title: event.title,
duration: parseFloat(event.duration)
};
ad_companions
this.companionAds = event.companionAds;
Error codes
Find a summary of error codes used by the API.
HTTP Error codes
HTTP Error | Corresponding DAILYMOTION Error |
---|---|
| Bad Request The API call requires authentication but it was not presented or was wholly invalid, or the API call was invalid (invalid_parameter, missing_required_parameter). |
| Unauthorized A valid access token should be provided. This error may come from an expired access token. |
| Forbidden The request is understood, but it has been refused or access is not allowed. An accompanying error message will explain why. This code is used when requests are being denied due to spam activity, or the request requires higher privileges than provided by the access token. |
| Not Found The requested object was not found. This error can also be thrown when you request non active users, censored videos, etc. |
| Method Not Allowed Invalid HTTP Method + method_not_allowed error type. |
| Not Implemented The specified method does not exist (invalid_method). |
| Internal Server Error This API error covers any other type of problem, for example a temporary problem with the Dailymotion servers, and should turn up only very infrequently. Check the associated message for more information. |
DM Error types
Here’s a list of error types you may encounter in errors returned by the API.
Type | Description |
---|---|
| Thrown when the user doesn’t have the permission to access the data. For example missing a required scope to access certain fields. |
| The requested object has been deleted |
| The API endpoint or object connection is invalid. |
| Your request contains invalid parameters. For example you set an invalid data type for a field. |
| The API call is correct, but the method is not allowed. For example replace a video URL before encoding process is over. |
| You forgot a required parameter in your API call. |
| The requested object was not found. |
| The data you tried to set using the API could not be saved, this is generally a temporary error that will resolve itself over time. |
DM Video access errors
When requesting access to a video, the API may return a message explaining why the access can’t be granted inside the specific access_error field. Here is a list of the different access error codes you may encounter and their descriptions.
Error Code | Description |
---|---|
| No video has been specified, you need to specify one. |
| Content has been deleted. |
| Live content is not available, for example it may not have started yet. |
| Copyrighted content, access forbidden. |
| Content rejected (this video may have been removed due to a breach of the terms of use, a copyright claim or an infringement upon third party rights. |
| Publishing in progress. |
| Video geo-restricted by its owner. |
| Explicit content. |
| Explicit content (offsite embed). |
| Private content. |
| An encoding error occured. |
| Encoding in progress. |
| This video has no preset (no video stream). |
| This video has not been made available on your device by its owner. |
| Kids host error. |
| Content not available on this website, it can only be watched on Dailymotion. |
| This content has been uploaded by an inactive channel and its access is limited. |