How to login into matomo on-premise programmatically?

var myHeaders = new Headers();
myHeaders.append("Cookie", "MATOMO_SESSID=SESSID");

var formdata = new FormData();
formdata.append("form_login", "username");
formdata.append("form_nonce", "mv6rl8be95qpdjqprgm5ucq7ap");
formdata.append("form_redirect", "https://example.onpremise.com/index.php?module=CoreHome&action=&period=day&date=yesterday");
formdata.append("form_password", "Password");
formdata.append("form_rememberme", "1");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: formdata,
  redirect: 'follow'
};

fetch("https://example.onpremise.com/index.php?module=Login", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

I tried the matomo login API through Postman and got 403. This was the error message I got:

Error : The form security failed because of a token mismatch. Please reload the form and check that your cookies are on.

My matomo version is 4.13.3. Is there a way to accomplish login through API?