Answers.Body: <p>The <code>['template' =&gt; 'template/path']</code> syntax in the doc block is incorrect (will get that updated).</p>
  <p>If you want to route to a template and you want to do this from Twig, it'd be something like:</p>
  <p>generate.twig</p>
  <pre><code>{% set token = craft.app.tokens.createToken(['templates/render', {template: 'claim'}]) %}
  &lt;a href=&quot;claim?token={{ token }}&quot;&gt;Claim&lt;/a&gt;
  </code></pre>
  <p>claim.twig</p>
  <pre><code>You did it.
  </code></pre>
  <p>Then request to claim with a valid token will get the usageCount/expiry logic applied to the token.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-11T07:43:24.713Z
Answers.Text: HI Brad, thats amazing thanks so much for spotting this. Could I be a bit of a pain and ask for a follow up please. Could you provide me with the code that would allow me to set a max use of this token to 1 use. Also (sorry) - is there a way to get a token by the querystring? I am trying to get the token by what is provided in the query string - see if it is valid (has not expired or hit its max use) and if it has then delete it (if it is valid then show some data) - I've been looking at the docs, but have spent alot of time on this so any help would be really great - thanks again :)
Answers.UserDisplayName: Wally
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-20T20:08:04.420Z
Answers.Text: could I please ask you to update your answer to include one more bit of formatting: How do you add a "$usageLimit" to the token creation? I thought it would look like this:  {% set token = craft.app.tokens.createToken(['templates/render', {template: "/downloads/_one-pager"}], 1) %} ? Thanks
Answers.UserDisplayName: Wally
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-10T23:08:46.593Z
Answers.LastActivityDate: 2022-10-11T16:54:56.423Z
Answers.LastEditDate: 2022-10-11T16:54:56.423Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a workflow were a user will be given a link, with a token attached to it, upon clicking that link, the token will be read from the query string, if it is valid the user will be presented a download link and the token will be deleted.</p>
  <p>If the token is not valid (or has been deleted) - then the User will be shown an error page.</p>
  <p>Process I am using to achieve this (and the issues):</p>
  <ol>
  <li>create a token. <em><strong>(this issue is blocking so needs to be resolved first)</strong></em></li>
  </ol>
  <p><a href="https://docs.craftcms.com/api/v3/craft-services-tokens.html#public-methods" rel="nofollow noreferrer">https://docs.craftcms.com/api/v3/craft-services-tokens.html#public-methods</a></p>
  <p>From the docs it says that you can set a route as the first argument when creating a token. But when I use the token in a URL it gives me an error - Steps to reproduce below:</p>
  <pre><code>// Route to a template
  Craft::$app-&gt;tokens-&gt;createToken(['template' =&gt; 'template/path']);
  </code></pre>
  <p>I am writing this in TWIG:</p>
  <pre><code>  {# Create token #}
    {% set token = craft.app.tokens.createToken(&quot;https://&lt;website&gt;.ddev.site/DEV/downloadToken&quot;) %}
  </code></pre>
  <p>I then attach the Token to a url to present to the user as part of the URL querystring:</p>
  <pre><code>&lt;a href=&quot;https://&lt;website&gt;.ddev.site?token={{ token }}&quot; target=&quot;_blank&quot;&gt;URL LINK&lt;/a&gt;
  </code></pre>
  <p>I would suspect that when I visit this link, the token routes me to the route I set in the token variable? But I always get a 404 error: Caused by: <strong>Invalid Route – yii\base\InvalidRouteException</strong></p>
  <p><a href="https://i.sstatic.net/Thm91.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Thm91.png" alt="enter image description here" /></a></p>
  <p>So, could someone please give me an example of a valid route? and retrieving it.</p>
  <p>If there are any questions then please let me know :)</p>
  <p>Also: note - This is a support question for <a href="https://craftcms.stackexchange.com/questions/39909/using-tokens-to-authenticate-a-one-time-download-link">Using tokens to authenticate a one-time download link?</a></p>
  
Tags: <yii><api><token>
Title: How to generate a token, retrieve it and delete it
ThreadText: # How to generate a token, retrieve it and delete it
  
  (Asked by: username_0 on 2022-10-06)
  
  I have a workflow were a user will be given a link, with a token attached to it, upon clicking that link, the token will be read from the query string, if it is valid the user will be presented a download link and the token will be deleted.
  
  If the token is not valid (or has been deleted) - then the User will be shown an error page.
  
  Process I am using to achieve this (and the issues):
  
  1. create a token. ***(this issue is blocking so needs to be resolved first)***
  
  <https://docs.craftcms.com/api/v3/craft-services-tokens.html#public-methods>
  
  From the docs it says that you can set a route as the first argument when creating a token. But when I use the token in a URL it gives me an error - Steps to reproduce below:
  
  ```
  // Route to a template
  Craft::$app->tokens->createToken(['template' => 'template/path']);
  
  ```
  
  I am writing this in TWIG:
  
  ```
    {# Create token #}
    {% set token = craft.app.tokens.createToken("https://<website>.ddev.site/DEV/downloadToken") %}
  
  ```
  
  I then attach the Token to a url to present to the user as part of the URL querystring:
  
  ```
  <a href="https://<website>.ddev.site?token={{ token }}" target="_blank">URL LINK</a>
  
  ```
  
  I would suspect that when I visit this link, the token routes me to the route I set in the token variable? But I always get a 404 error: Caused by: **Invalid Route – yii\base\InvalidRouteException**
  
  [![enter image description here](https://i.sstatic.net/Thm91.png)](https://i.sstatic.net/Thm91.png)
  
  So, could someone please give me an example of a valid route? and retrieving it.
  
  If there are any questions then please let me know :)
  
  Also: note - This is a support question for [Using tokens to authenticate a one-time download link?](https://craftcms.stackexchange.com/questions/39909/using-tokens-to-authenticate-a-one-time-download-link)
  
  **Tags:** yii, api, token
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-10-10. Score: 1 | Accepted answer
  
  The `['template' => 'template/path']` syntax in the doc block is incorrect (will get that updated).
  
  If you want to route to a template and you want to do this from Twig, it'd be something like:
  
  generate.twig
  
  ```
  {% set token = craft.app.tokens.createToken(['templates/render', {template: 'claim'}]) %}
  <a href="claim?token={{ token }}">Claim</a>
  
  ```
  
  claim.twig
  
  ```
  You did it.
  
  ```
  
  Then request to claim with a valid token will get the usageCount/expiry logic applied to the token.
  
  > **Comments:**
  > **username_0**: HI username_1, thats amazing thanks so much for spotting this. Could I be a bit of a pain and ask for a follow up please. Could you provide me with the code that would allow me to set a max use of this token to 1 use. Also (sorry) - is there a way to get a token by the querystring? I am trying to get the token by what is provided in the query string - see if it is valid (has not expired or hit its max use) and if it has then delete it (if it is valid then show some data) - I've been looking at the docs, but have spent alot of time on this so any help would be really great - thanks again :)  
  > **username_0**: could I please ask you to update your answer to include one more bit of formatting: How do you add a "$usageLimit" to the token creation? I thought it would look like this:  {% set token = craft.app.tokens.createToken(['templates/render', {template: "/downloads/_one-pager"}], 1) %} ? Thanks  
  
  ---
  
  