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) %}

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