Access csrf_token in custom app's portal page

Is there a way for me to be able to access the csrf_token in the JavaScript file for my custom app’s portal page? I’m trying to send a post request to an frappe api method but it doesn’t allow me to, and responds with a csrf_token error saying that the toke isn’t valid. Haven’t been able to find a solution after researching for a day/

To check if this value exists in your case, you can expose it in your portal as a debugging step (twice to cross check it).

In www/{page}.html

<pre>{{ frappe.sessions.get_csrf_token() }}</pre>

In www/{page}.js

console.log(frappe.csrf_token)
// OR
frappe.ready(()=> {
    console.log(frappe.csrf_token)
})

In the case of the first method, I get this error:

'function object' has no attribute 'get_csrf_token'

and it breaks the whole page.

When I tried the second, it says that frappe is undefined.

This could be be a change based on the Frappe version you’re using. Internal APIs like this are sometimes changed. Search “csrf_token” in whatever version/branch you’re on and see what the correct function signature is.

I’m using version 15. The correct function still seems to be get_csrf_token(). I just can’t seem to access anything related to frappe.

Is your app installed? Can/does your HTML page render anything when it is routed to?

Yes my app is installed and yes it renders the page when routed to.