When using custom web templates, especially for forms or authenticated actions, you must manually include the Request Verification Token to avoid CSRF (Cross-Site Request Forgery) issues. The default Studio templates handle this automatically, but custom templates require explicit inclusion.
To fix this, include the following Liquid tag inside your form:
You're encountering a common issue in Power Pages when using custom web templates: the Request Verification Token (__RequestVerificationToken
) is missing, which causes API calls to fail due to CSRF protection.
Here’s how to fix it and ensure the token is available on every page:
The default template automatically includes the necessary scripts and markup to inject the verification token into the page. Custom templates, however, require you to manually include this logic.
You can use the following approach to retrieve and use the token:
In your custom web template, add this snippet inside the <head>
or before your script:
Here’s a wrapper function using jQuery to include the token:
function safeAjax(ajaxOptions) {
var token = $('meta[name="csrf-token"]').attr('content');
if (!ajaxOptions.headers) {
ajaxOptions.headers = {};
}
ajaxOptions.headers['__RequestVerificationToken'] = token;
return $.ajax(ajaxOptions);
}
Then use it like this:
safeAjax({
type: "POST",
url: "/_api/accounts",
contentType: "application/json",
data: JSON.stringify({ name: "Test Account" }),
success: function (res) {
console.log("Success:", res);
},
error: function (err) {
console.error("Error:", err);
}
});
If you're calling external APIs or need more secure authentication, consider using OAuth 2.0 implicit grant flow.
🏷️ Tag me if you have any further questions or if the issue persists.
Fubar
62
Super User 2025 Season 2
Lucas001
48
Super User 2025 Season 2
KevinGador
44
Super User 2025 Season 2