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

CC BY-SA 4.0
2021-05-17T20:15:59.310Z
2021-05-17T20:15:59.310Z
David Jones
<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>

<token><preview>
How to generate a preview URL in Twig
# 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):
Preview

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

Make sure sourceId and siteId are set correctly.


---

