Shutdown Proxmox using APi

I know that this is a post for shuttting down the PVE host via the Rest API, but I tried adapting the solution of user @gfngfn256 to shutdown a VM on the node, following the Proxmox documentation I just had to make a few modifications. This was the result:
Bash:
'curl "https://$IP:$PORT/api2/extjs/nodes/{node}/stopall" -H "Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID" -H "Content-Type: application/x-www-form-urlencoded" --data "vms=2201" --insecure'

But it returns the message: Permission check failed.
I have already verified that I'm using the correct Token and Token Secret by running the @gfngfn256 solution successfully and I also verified that VM.PowerMgmt Permission is correctly linked to the API Token.

I would appreciate any ideas on what the problem could be.

Thanks!
I will post an excerpt from my HA configuration.yaml which works with API for me:

YAML:
shell_command:
  start_lxc_<CTID>: 'curl -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -X POST https://<IP>:8006/api2/json/nodes/<NodeName>/lxc/<CTID>/status/start --insecure'
  stop_lxc_<CTID>: 'curl -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -X POST https://<IP>:8006/api2/json/nodes/<NodeName>/lxc/<CTID>/status/stop --insecure'
  start_vm_<VMID>: 'curl -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -X POST https://<IP>:8006/api2/json/nodes/<NodeName>/qemu/<VMID>/status/start --insecure'
  shutdown_vm_<VMID>: 'curl -H "Authorization: PVEAPIToken=<UserName>!<TokenID>=<TokenSecret>" -X POST https://<IP>:8006/api2/json/nodes/<NodeName>/qemu/<VMID>/status/shutdown --insecure'

Please note that I recently stopped using the API method to access Proxmox from within HA, and rather use SSH (by setting up a pair of SSH keys between HA and Proxmox).
My current configuration.yaml looks as follows:

YAML:
shell_command:
  start_lxc_<CTID>: "ssh -i /config/id_rsa -o StrictHostKeyChecking=no root@<IP> -t 'pct start <CTID>'"
  stop_lxc_<CTID>: "ssh -i /config/id_rsa -o StrictHostKeyChecking=no root@<IP> -t 'pct stop <CTID>'"
  start_vm_<VMID>: "ssh -i /config/id_rsa -o StrictHostKeyChecking=no root@<IP> -t 'qm start <VMID>'"
  shutdown_vm_<VMID>: "ssh -i /config/id_rsa -o StrictHostKeyChecking=no root@<IP> -t 'qm shutdown <VMID>'"

Its more robust & versatile. YMMV.

Remember which ever version you use, ALL THE QUOTES, DOUBLE-QUOTES, COMMAS ETC. MUST BE EXACTLY AS MINE TO WORK
 
  • Like
Reactions: neoplayer
yes, because you token doesn't have the right permission.. you need 'VM.PowerMgmt' on '/vms/XXX'
I knew it was a permission problem, but by logic if I give to the token the permission VM.PowerMgmt in the nodes directory/domain, it should encompass all that's inside it, including the VMs in the nodes. But I made it work by giving permission to the $USER for every single VM I want it to be capable of shutting down with the API Token. First I tried giving the permissions to the VMs to the Token, but it still returned the error "Permission check failed".
 
I will post an excerpt from my HA configuration.yaml which works with API for me:
Thanks for the answer. I use ssh too in my Proxmox Cluster at home, but the enviroment that I need the configuration for is a really very busy environment in terms of traffic, so I opted for using the Rust API as is more efficient for single short commands.
 
I knew it was a permission problem, but by logic if I give to the token the permission VM.PowerMgmt in the nodes directory/domain, it should encompass all that's inside it, including the VMs in the nodes. But I made it work by giving permission to the $USER for every single VM I want it to be capable of shutting down with the API Token. First I tried giving the permissions to the VMs to the Token, but it still returned the error "Permission check failed".

please refer to the API viewer, it tells you which privileges are required for which action.. guessing how ACLs work is not a good approach ;)
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!