Monday, June 27, 2016

SUGAR CRM 7: Creating a Custom API

1. Add your php class under custom/modules/<module>/clients/base/api for specific module extensions or clients/base/api for custom api calls

2. Override registerApiRest() and add your API customization.
public function registerApiRest() 
{   
    return array(       
           'MyGet' => array(           
              'reqType' => 'GET',
              'path' => array('Accounts', 'GetExample'),
              'pathVars' => array('', ''),           
              'method' => 'MyGet',           
              'shortHelp' => 'An Example of a GET endpoint',
              'longHelp' => '',        )
    );
}
 
3. Add your MyGet method
public function MyGet($api, $args){
    return "Hello World!";}
 
4. Final Code
<?php
if(!defined('sugarEntry') ||
!sugarEntry) die('Not A Valid Entry Point');

class MyEndpointsApi extends SugarApi
{
   public function registerApiRest()
   {
     return array(            
       'MyGet' => array(                
         'reqType' => 'GET',                
         'path' => array('Accounts', 'GetExample'),                
         'pathVars' => array('', ''),                
         'method' => 'MyGet',                
         'shortHelp' => 'An Example of a GET endpoint',                
         'longHelp' => '',
       )
     );
   }
 
   public function MyGet($api, $args)
   {return "Hello World!";}

5. Test your API call (I suggest POSTMAN for Chrome)
    

No comments:

Post a Comment