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...

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

# Multiple Restrictions

A token may have more than one limitation. If any condition evaluates as true, the file will be unavailable for download.

{# Limit to 1 year or 100 downloads (whichever comes first) #}
{% set token = craft.digitalDownload.createToken(asset, {
    'expires': 'P1Y',
    'maxDownloads': 100
}) %}

# createToken(asset, options = [])

  • asset - The downloadable Asset.
  • options - A key-value set of options.
Option Default Description
expires 'P14D' Time until asset key expires. Accepts any valid PHP interval specification.
maxDownloads 0 Maximum number of downloads allowed. (0 = unlimited)
requireUser null If 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 specific user (by user ID)
[492, 467]                // Multiple specific users

'myGroup'                 // Anyone in this user group
['mygroup', 'otherGroup'] // Multiple user groups

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