API struggles and also no ticket


Nov 29, 2019
We are running on SpamExperts at this moment, but because we are already using Proxmox VE and SpamExperts is not software we like, we are looking into Proxmox Mail Gateway.

To get the domains in the PMG, we would like to use the API, but the documentation isn`t really straight forward (some examples would be helpful). I made a simple script for adding a domain, but the response is HTTP request failed! HTTP/1.0 401 No ticket.

    $url = 'https://1xx.xx.xxx.251:8006/api2/json/config/domains/';
    $cn_match = 'pmg.domain.tld';

    $data = array("domain" => "exampledomain.com", "comment" => "Imported");

    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-Type: application/json\r\n",
            'method'  => 'PUT',
            'content' => http_build_query($data)
        , 'ssl' => array(
            'verify_peer' => false,
            'cafile' => "./cacert.pem",
            'ciphers' => 'HIGH:TLSv1.2:TLSv1.1:TLSv1.0:!SSLv3:!SSLv2',
            'CN_match' => $cn_match,
            'disable_compression' => true,

    $context  = stream_context_create($options);
    $response = file_get_contents($url, false, $context);

It would be great if someone can help me out with a working example, so I can work on that to get things running.
  • Like
Reactions: Stoiko Ivanov

you need to create an authentication ticket by passing valid credentials, and afterwards you need to pass the ticket with all your later requests.


Sorry, I do first this (the rest of the script looks the same):

    $url = 'https://1xx.xx.xxx.251:8006/api2/json/access/ticket/';
    $cn_match = 'pmg.domain.tld';

    $data = array("username" => "nathand", "password" => "xxxxxx", "realm" => "pmg");

And after that the second one (add domain) is running. But the authentication one also gives the 401 no ticket, so I'm still missing something.

But the authentication one also gives the 401 no ticket, so I'm still missing something.

you need to get the authentication working first in any case.

do you send a POST request to api2/json/access/ticket with the parameters username, password? (realm isn't needed. you can use username@realm for the username parameter). once successful, it should return ticket and CSRFPreventionToken which you then pass to your next requests.


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!