Skip to main content
New Idea

This is how to add Approval URL to API Bespoke Approval Requests

Related products:Freshservice
  • January 7, 2025
  • 0 replies
  • 52 views

Jason.Evans2
Contributor
Forum|alt.badge.img+1

This is a quick how add Approval URL to API Bespoke Approval Requests

Like me, if you want to make a bespoke approval request via the API it says on the page https://api.freshservice.com/#create_ticket_approval

Note: "email_content" is an optional parameter for sending approval. If not provided, the default approval notification will be sent to approvers.

Now it does not say much about how to add the “{{approval.portal_url}}” placeholder into your emails or approval requests via the API, which means when you send an email with a non standard “email_content” JSON it will not put this in automatically. 

However, a way has been found and I am posting it here so others do not get stuck scratching their heads for a day like me. 

I did this in PowerShell so convert to what ever language you are using. 

Convert this to how you wish. When hyperlinking, ensure you put an extra curly brackets on the placeholder for the ticket.

$apiKey = "api_key:X"
$headers = @{
"Content-Type" = "application/json"
}

$emailbodynote = @"
<p>Dear {{ticket.requester.name}}</p>
<p><b>Ticket: </b><a href=`"$({{{ticket.url}}})`">{{ticket.url}}</a></p>
<p> This is an approval </p>
<ul style="list-style-type:square;">
<li>Please click <span style="color: green;"><b>Approve</b></span> if you would like this call queue to be renewed.</li>
<li>Please click <span style="color: red;"><b>Reject</b></span> if this call queue is no longer required.</li>
</ul>
<p>Please use the link below to Approve/Reject the request</p>
<p><b>Approval Link:</b> | <a href=`"$({{{approval.portal_url}}})`">{{approval.portal_url}}</a></p>
<p>Thanks Your Support Deptartment</p>
"@

# Approval Type #
# $To_be_approved_by_Everyone = 1
# $To_be_approved_by_Anyone = 2
# $To_be_approved_by_Majority = 3
$To_be_approved_by_First_Responder = 4

$ownerIdOwner = 000000000001

$body = @{
approver_id = $ownerIdOwner
approval_type = $To_be_approved_by_First_Responder
email_content = $emailbodynote
} | ConvertTo-Json

$test_ticket = 000001

$myDomainURI = "https://domain.freshservice.com/api/v2/"

$ApprovalURL = ( "{0}/tickets/{1}/approvals" -f $myDomainURI, $test_ticket )

do {
try {
$response = Invoke-RestMethod -Uri $uri -Method Post -Headers $headers -Body $body -Authentication Basic -Credential (New-Object System.Management.Automation.PSCredential("", (ConvertTo-SecureString $apiKey -AsPlainText -Force)))
$success = $true
} catch {
if ($_.Exception.Response.StatusCode.Value__ -eq 429) {
Write-Host "Received 429 Too Many Requests. Waiting for 60 seconds before retrying..."
Start-Sleep -Seconds 60
$success = $false
} else {
throw $_
}
}
} while (-not $success)

I hope this helps anyone trying to do this.