Using the osTicket API

One of the [I feel] less documented features of osTicket is the ticket API.  This API has been built into osTicket for quite some time and allows you to write your own html forms and push the data to osTicket to open tickets.  This means that you could write a contact block for your web page and have it submit to osTicket directly, or if you have another piece of software that can run a script you can write your own custom script to open tickets from that software.  Example a network monitoring system (like NOCOL/SNIPS) could open a ticket when a device stops responding.

Before you can start using the API you will need to do some configuring in osTicket.  Log into your osTicket installation with your admin account. and go to Admin panel -> Manage -> API Key.

api key start

On the right click on "Add New API Key".

osticket add api key

There are several things that you need to enter here.  First is the IP address of the source where the request will originate from.  This can be the server IP address assigned to your server that hosts the software. Next you will want to check "Can Create Tickets (XML/JSON/EMAIL)".  Then under "Admin Notes" put a note so that in the future you can go in and see what this API key was made for.  Something like: "API Key for automating X software opening tickets." where X is the name of the software your using.  Feel free to customize this to be what will relay to you the reason for this API key.  Next click "Add Key".

api key is setup

Take note of the "THISISAFAKEAPIKEYHERE!" (which of course will be a real API key for your installation).  You will need this for your script.

Next download the osTicket API example. The only thing in this archive is a file called: ost-api-example.php.   This is an example of how to use the API using PHP.  Obviously you will need to edit this file to suit your implementation of osTicket.  The two really important parts are on lines 18 and 19. 

'url'=>'http://your.domain.tld/api/tickets.json', // URL to site.tld/api/tickets.json 'key'=>'PUTyourAPIkeyHERE' // API Key goes here Edit line 18 to have your URL.
Edit line 19 by changing "PUTyourAPIkeyHERE" to your API key (aka the "THISISAFAKEAPIKEYHERE!" from earlier).

If you have any required custom fields you will want to add them to the $data = array (lines 39 through line 49).  There are examples of both a list and text field included (Agency and Site).

Next try to run the script.  You can do this by putting the script on a web server and browsing to it. [note: the webserver will need to have the IP address that you put in the API key!]