Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

osTicket v1.10 (stable) and Maintenance Release v1.9.15 are now available! Go get it now

Attachments in API

Hi has anyone managed to send attachments using the API ? I've used ntozier's example code. But trying to attach a txt file, the newly created ticket's thread shows a "File Import Error", "Unable to import attachment - file.txt" in the ticket. And trying to attach an image (which is what I really want to do) shows "Invalid file type (ext) for".

This is my image attachement code:

$config = array(

$path= 'myimage.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/'.$type.';base64,' . base64_encode($data);

$data = array(
    'name'      =>      'John Doe',  // from name aka User/Client Name
    'email'     =>      '',  // from email aka User/Client Email
    'phone'     =>        '1234567890',  // phone number aka User/Client Phone Number
    'subject'   =>      'Test API message',  // test subject, aka Issue Summary
    'message'   =>      'This is a test of the osTicket API',  // test ticket body, aka Issue Details.
    'ip'        =>      $_SERVER['REMOTE_ADDR'], // Should be IP address of the machine thats trying to open the ticket.  
    'attachments' => array('myimage.png' => $base64)

#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($code != 201)
    die('Unable to create ticket: '.$result);

$ticket_id = (int) $result;

echo $ticket_id;
This works as far as creating a new ticket and returning the Id but like I said, the attachment fails.


  • I remember having the same issue, but not how I fixed it.
    But my brain is telling me that it has something to do with the file permissions of the uploaded file and PHP giving the file the permissions of the of the folder that its places in.  (I could be misremembering that.)
  • I am also having this same exact issue. Can anyone shed some light on this?
  • Thanks for trying to help. The file is converted into a base64 string though and then isn't it ment to be inserted into the ost_file, ost_file_chunk tables..? Not sure how I can work with permissions if this is the case.
  • Hi zgjonbalaj,bananas,ntozier  ,
                                             I made the the whole API work using the Create API of the OSTicket and also I have changed the functions for some of the files which I want to described everything in the attached file.I made the attachment for the pics also working.
    The attached file will give you all the whole functionality for working on API.
    If you all have any issue let me know 

  • @anurupborah2001 Thanks for your help i appreciate all your efforts.
  • Hello, thanks for the API file update.

    I tried to use it with osTicket v1.10.1 and it does not work. There are a lot of codes that have changed and does not match the description in your file.

    Is there anyone who have managed to insert attachments via the API to osticket?
Sign In or Register to comment.