Answers.Body: <p>I figured this out:</p>
  <pre><code>{% set token = craft.app.tokens.createToken([&quot;preview/preview&quot;, {&quot;elementType&quot;: &quot;craft\\elements\\Entry&quot;, &quot;sourceId&quot;: 123, &quot;siteId&quot;: 1}]) %}
  &lt;a href=&quot;{{ element.url }}?token={{ token }}&quot;&gt;Preview&lt;/a&gt;
  </code></pre>
  <p>Make sure <code>sourceId</code> and <code>siteId</code> are set correctly.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-05-17T20:15:59.310Z
Answers.LastActivityDate: 2021-05-17T20:15:59.310Z
Answers.UserDisplayName: David Jones
Body: <p>I have a template intended for administrators (separate from the Craft control panel) where I'd like to generate tokenized preview links for unpublished posts (the same URL you get when you click on the &quot;View&quot; button from an entry page within the control panel). These links are intended for administrators to share with third parties so they can preview a page before it's published. Here's the Twig I have so far (which isn't working):</p>
  <pre><code>&lt;a href=&quot;{{ element.url }}?token={{ craft.app.tokens.createToken() }}&quot;&gt;Preview&lt;/a&gt;
  </code></pre>
  <p>What am I missing?</p>
  
Tags: <token><preview>
Title: How to generate a preview URL in Twig
ThreadText: # How to generate a preview URL in Twig
  
  (Asked by: username_0 on 2021-05-14)
  
  I have a template intended for administrators (separate from the Craft control panel) where I'd like to generate tokenized preview links for unpublished posts (the same URL you get when you click on the "View" button from an entry page within the control panel). These links are intended for administrators to share with third parties so they can preview a page before it's published. Here's the Twig I have so far (which isn't working):
  
  ```
  <a href="{{ element.url }}?token={{ craft.app.tokens.createToken() }}">Preview</a>
  
  ```
  
  What am I missing?
  
  **Tags:** token, preview
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2021-05-17. Score: 3 | Accepted answer
  
  I figured this out:
  
  ```
  {% set token = craft.app.tokens.createToken(["preview/preview", {"elementType": "craft\\elements\\Entry", "sourceId": 123, "siteId": 1}]) %}
  <a href="{{ element.url }}?token={{ token }}">Preview</a>
  
  ```
  
  Make sure `sourceId` and `siteId` are set correctly.
  
  
  ---
  
  