Making API 3.0 Calls

From a Browser

Posting an API call to Intacct can be accomplished from a very simple set of HTML loaded into a web browser.

<HTML>
  <form method="post" action="https://api.intacct.com/ia/xml/xmlgw.phtml">
    <textarea rows="20" cols="80" name="xmlrequest">
    </textarea><br/>
    <input type="submit"/>
  </form>
</HTML>

The key to the browser-based call is the form element containing the XML to post needs to be named "xmlrequest".

From a Trigger

You can add a custom Trigger to custom objects on selected events, with optional conditions.  To invoke an Intacct Web Services API call from a trigger, choose the "HTTP Post" trigger type with the appropriate URL for the environment.  In production, this URL is "https://api.intacct.com/ia/xml/xmlgw.phtml".  In the preview environment, the URL is "https://partner.intacct.com/ia/xml/xmlgw.phtml".  To use a trigger, you must first create a document template for the trigger to process.  Look at this sample document to see how to use merge fields to create a dynamic API call from a trigger: Create an Invoice from a Trigger

Using a Programming Language like PHP

All modern languages provide basic facilities for posting API calls over an HTTP(S) connection.  The following sample PHP code demonstrates how to make arbitrary API calls:

<?php
class api_post { 
public static function create($xml) { 
  $createXml = "<create>" . $xml . "</create>"; 
  return api_post::post($createXml); 
} 
public static function xml2_1_method($xml) { 
  return api_post::post($xml); 
} 
private static function post($xml) { 
  global $sessionId; 
  $templateHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
     <request> <control> <senderid>intacct_dev</senderid> 
     <password>****</password> <controlid>foobar</controlid> 
     <uniqueid>false</uniqueid> <dtdversion>3.0</dtdversion> 
     </control> <operation> <authentication> 
     <sessionid>{1%}</sessionid> </authentication> 
     <content> <function controlid=\"foobar\">"; 
   $templateFoot = "</function> </content> </operation> </request>";         
   $xml = $templateHead . $xml . $templateFoot;         
   $xml = str_replace("{1%}", $sessionId, $xml);         // post!         
   $ch = curl_init();        
   curl_setopt( $ch, CURLOPT_URL, "https://api.intacct.com/ia/xml/xmlgw.phtml" );         
   curl_setopt( $ch, CURLOPT_HEADER, 0 );         
   curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );         
   curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );         
   curl_setopt( $ch, CURLOPT_MUTE, 1 );         
   curl_setopt( $ch, CURLOPT_TIMEOUT, 300 ); //Seconds until timeout
   curl_setopt( $ch, CURLOPT_POST, 1 );                 
   $body = "xmlrequest=" . urlencode( $xml );         
   curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );                 
   $response = curl_exec( $ch );                
   curl_close( $ch );                 
   return $response;     
  } 
} 
?>

Using Python

For Python version 3.x. The following example shows posting a call to get vendors:

mysenderid = 'PutYourSenderIdHere'
mysenderpwd = 'PutYourSenderPasswordHere'
mycompany = 'PutYourCompanyIdHere'
myuser = 'PutYourUserIdHere'
mypwd = 'PutYourUserPasswordHere'
myxml = '<?xml version="1.0" encoding="iso-8859-1"?><request><control><senderid>'
myxml += mysenderid
myxml += '</senderid><password>'
myxml += mysenderpwd
myxml += '</password><controlid>ControlIdHere</controlid><uniqueid>false</uniqueid>'
myxml += '<dtdversion>3.0</dtdversion></control><operation><authentication><login><userid>'
myxml += myuser
myxml += '</userid><companyid>'
myxml += mycompany
myxml += '</companyid><password>'
myxml += mypwd
myxml += '</password></login></authentication>'
myxml += '<content><function controlid="C1"><read><object>vendor</object>'
myxml += '<keys/><fields>*</fields><returnFormat>xml</returnFormat></read>'
myxml += '</function></content></operation></request>'
import http.client, urllib.parse
params = urllib.parse.urlencode({'xmlrequest': myxml})
headers = {"Content-type": "application/x-www-form-urlencoded",
           "Accept": "text/plain"}
conn = http.client.HTTPSConnection("api.intacct.com")
conn.request("POST", "/ia/xml/xmlgw.phtml", params, headers)
response = conn.getresponse()
print(response.status, response.reason)
data = response.read()
data
conn.close()

The following was graciously supplied by one of our members:

import urllib2

auth_info = {
    'senderid':'xyz',
    'password':'xyz',
    'userid':'xyz',
    'companyid':'xyz',
    'userpw':'xyz'
    }
headers = {'Content-type': 'x-intacct-xml-request'}

API_URL = 'https://api.intacct.com/ia/xml/xmlgw.phtml'

req_shell = """<?xml version="1.0" encoding="iso-8859-1"?><request><control>
<senderid>{senderid}</senderid><password>{password}</password><controlid>foobar</controlid>
<uniqueid>false</uniqueid><dtdversion>3.0</dtdversion></control><operation>
<authentication><login><userid>{userid}</userid><companyid>{companyid}</companyid>
<password>{userpw}</password></login></authentication><content>
<function controlid="foobar">{payload}</function></content></operation></request>"""

payload = '<read><object>vendor</object><keys/><fields>*</fields><returnFormat>xml</returnFormat></read>'

req = urllib2.Request(API_URL, req_shell.format(payload=payload, **auth_info), headers)
resp = urllib2.urlopen(req).read()

Using PHP5

For PHP 5.x. The following example shows posting a call to get vendors:

<?php

$mysenderid = 'PutYourSenderIdHere';
$mysenderpwd = 'PutYourSenderPasswordHere';
$mycompany = 'PutYourCompanyIdHere';
$myuser = 'PutYourUserIdHere';
$mypwd = 'PutYourUserPasswordHere';

$xmlPostStr = '<?xml version="1.0" encoding="iso-8859-1"?><request><control><senderid>';
$xmlPostStr.= $mysenderid;
$xmlPostStr.= '</senderid><password>';
$xmlPostStr.= $mysenderpwd;
$xmlPostStr.= '</password><controlid>ControlIdHere</controlid><uniqueid>false</uniqueid><dtdversion>3.0</dtdversion></control><operation><authentication><login><userid>';
$xmlPostStr.= $myuser;
$xmlPostStr.= '</userid><companyid>';
$xmlPostStr.= $mycompany;
$xmlPostStr.= '</companyid><password>';
$xmlPostStr.= $mypwd;
$xmlPostStr.= '</password></login></authentication>';
$xmlPostStr.= '<content><function controlid="C1"><read><object>vendor</object><keys/><fields>*</fields><returnFormat>xml</returnFormat></read></function></content></operation></request>';

  	// INITIATE CURL
  	$ch = curl_init();
  	curl_setopt( $ch, CURLOPT_URL, 'https://api.intacct.com/ia/xml/xmlgw.phtml');
  	curl_setopt( $ch, CURLOPT_HEADER, 0 );
  	curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
  	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
  	curl_setopt( $ch, CURLOPT_TIMEOUT, 300); //Seconds until timeout
  	curl_setopt( $ch, CURLOPT_POST, 1 );
  	
  	// BUILD THE POST BODY
  	$body = "xmlrequest=" . urlencode( $xmlPostStr );
  	curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
  	
  	// POST AND GET RESPONSE
  	$response = curl_exec( $ch );
	  	
  	curl_close( $ch );
	
  	//PRINT OUT THE RESPONSE
  	echo $response;
  	
?>