Digital Download

Documentation  •  Changelog


Creating a Token

Manual vs. Automatic

Tokens can be created manually (see below) or automatically, when rendering a link on the fly.

In cases where a link is automatically created, a new token will be created each time the link is rendered. If you need to maintain the same token for repeated use, then you will want to create the token manually.

Here's how to manually generate a token for your downloadable file...

in Twig...

{% set token = craft.digitalDownload.createToken(file, options) %}

in PHP (Craft 2)...

$token = craft()->digitalDownload->createToken($file, $options);

in PHP (Craft 3)...

use doublesecretagency\digitaldownload\DigitalDownload;

$token = DigitalDownload::$plugin->digitalDownload->createToken($file, $options);

createToken(file, options = [])

  • file - An AssetFileModel of the downloadable file.
  • options - A key-value set of options.
expires'P14D'Time until asset key expires. Accepts any valid PHP interval specification.
maxDownloads0Maximum number of downloads allowed. (0 = unlimited)
requireUsernullIf downloads are restricted to specific users and/or user groups.
headers{ }Associative array of optional HTTP headers to send during download.

The requireUser option can take on a variety of forms:

null                      // Anyone can download (default)

'*'                       // Any logged-in user can download

492                       // Only one user, with this user ID

'myGroup'                 // Anyone in this user group

[492, 467]                // Multiple users

['mygroup', 'otherGroup'] // Multiple user groups

['mygroup', 467]          // Mix & match users and groups