How to Create Single-Use and Multiple-Use Links
This page shows how to create the two kinds of token link that a transformation can put in an email. A multiple-use link opens its form every time it is clicked, until it expires. A single-use link is deleted once its action is done. To put either link on a button in an email, see How to Add a Button with a Single-Use Link to an Email.
Both kinds use the same token. A token works until its expiry date or until a transformation deletes it. A single-use link is a token that your workflow deletes after the action.
Choosing a Link Kind
| Kind | Use it for | Example |
|---|---|---|
| Multiple use | A link a person may open more than once. The form shows information or a record they can come back to. | A reminder that lists the assets a custodian holds. The link stays valid for 14 days. |
| Single use | An approval, or any link whose form changes a record. | An Approve button for a purchase request. |
Creating the Token
The command is the same for both kinds. In the transformation that sends the email:
Set APIToken = CommandProcessor "GetAPITokenForRole", _
"Purchase Approval", _
"PurchaseApprovalAccountWrite", _
"10000", "7", _
"currentkey", "%RequestID%", "answer", "approve"
Set DBName = Sql select db_name()
Set URL = "%weburl%/xpanel.aspx?xtoken=%APIToken%&database=%DBName%"
| Argument | Example | Meaning |
|---|---|---|
| Form | Purchase Approval |
The form the link opens |
| Role | PurchaseApprovalAccountWrite |
The account the visitor works as. 12 to 40 letters and digits, ending in Read or Write |
| Key expiry days | 10000 |
Used once, when xAssets creates the role's account |
| Token expiry days | 7 |
Whole days. The link stops working after this |
| Name and value pairs | currentkey, %RequestID%, answer, approve |
Values passed to the form. currentkey is the record to open |
%APIToken% holds the token's identifier. It is the only part of the token that appears in the link. The form, the record and the values are stored in the token on the server. The person who clicks the link cannot change them.
The visitor works as the role's user, in the APIKEYWRITE user group for a Write role or APIKEYREAD for a Read role. Use a Read role for a multiple-use link whose form only displays data.
Multiple-Use Links
- Create the token as above. Set the expiry to the number of days the link is needed.
- Send the link. Nothing needs to be kept.
- In the landing form's script, read the record with
QueryString "currentkey"and the other values withQuerystringOptional:
Set Client.KeyID = QueryString "currentkey"
Set Client.Answer = QuerystringOptional "answer", ""
Each click logs the visitor on again and opens the same form for the same record. The form shows the record as it is at the time of the click.
A reminder that creates a new token leaves the earlier links working. Each link stops at its own expiry date. The batch service deletes expired tokens.
Single-Use Links
A single-use link needs three things: the token's value kept with the record, a check before the action, and a delete after it.
1. Keep the token's value with the record
When you create the token, save %APIToken% in a field of the record the link is for. Use a field that only the workflow and administrators can read. Anyone who reads the value can build the link.
If a reminder creates a new token for the same record, delete the old one first. Read the stored value and pass it to DeleteAPIToken, then save the new value.
2. Check the record before the action
In the landing form's script, read the record and stop if the action is already done:
Set Client.KeyID = QueryString "currentkey"
Set Status = SQL "select <status field> from <table> where <id field> = %Client.KeyID%"
If %Status% = 'Approved' Then
Throw "This request has already been approved."
End If
Put the action on a button in the form. Do not run it when the form opens. Some mail systems open every link in an email to check it, and an action that runs on opening would run then.
3. Delete the token after the action
At the end of the transformation that the button runs, read the stored value and delete the token by that value:
Set StoredToken = SQL "select <token field> from <table> where <id field> = %RequestID%"
DeleteAPIToken "%StoredToken%"
Always pass the value. DeleteAPIToken with a blank value deletes the token of the logon request only. The form's script and its button run on later requests. A blank DeleteAPIToken there leaves the token in place.
To check, click the link a second time. It must show the logon error below.
Second Click Behavior
| Kind | Second click |
|---|---|
| Multiple use, before expiry | The form opens again for the same record |
| Single use, token deleted | The logon fails. A message box titled Logon Error shows the reason, and no form opens |
| Either kind, after expiry | The same logon error |
Related Articles
- How to Add a Button with a Single-Use Link to an Email -- the email template and the button
- API Tokens -- how tokens are stored, created and deleted
- The AMSX Programming Language -- the commands used on this page