sugarcrm v10 REST API Creating custom Endpoints


Example

One of the features in SugarCRM 7.x is being able to easily add and extend custom endpoints to accomplish what you require.

In this example, we'll create a couple of custom endpoints to return some data about the request.

This custom file is being placed in custom/clients/base/api/DescriptionAPI.php.

<?php

if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

//You need to inherit the SugarApi Class to
class DescriptionApi extends SugarApi
{
    public function registerApiRest()
    {
        return array(

            //Define a key for the array
            'DescribeRequest' => array(

                //Array of the acceptable kinds of requests for this method
                'reqType' => array('GET','POST','PUT','DELETE'),

                //If true, anyone can access. If false, only authenticated users.
                'noLoginRequired' => true,

                //Here is the path to access the endpoint, in this case: Describe/Request
                'path' => array('Describe', 'Request'),

                //Specify an empty string for the path variables
                'pathVars' => array('', ''),

                //method to call
                'method' => 'DescribeMyRequest',

                //A small description, displayed in rest/v10/help page
                'shortHelp' => 'Describes your Request Method',

                //Further help, displayed when drilling down into the help page
                'longHelp' => 'custom/clients/base/api/help/DescribeRequestHelp.html',
            ),
            //Here's another entry with some more in depth information
            'DescribeIncludingArgs' => array(
                'reqType' => array('GET','POST','PUT','DELETE'),
                'noLoginRequired' => true,

                //This time, we'll include a third element with a ?
                //So now the path is Describe/Request/{dataFromURL}
                'path' => array('Describe', 'Request', '?'),

                //Here, we specify the key for accessing that data within the function
                'pathVars' => array('', '','dataFromURL'),
                
                'method' => 'DescribeMyRequestIncludingArguments',
                'shortHelp' => 'Describes the request you sent, including method, URL parameters, and request body',
                'longHelp' => 'custom/clients/base/api/help/DescribeIncludingArgsHelp.html',
            ),
        );

    }

    /**
     * Your custom logic goes in here.
     */
    public function DescribeMyRequest($api, $args)
    {
        //Find out the request method sent
        $requestType = $_SERVER['REQUEST_METHOD'];

        return "You sent a $requestType request.";
    }

    /**
     * Here is the second function
     */
    public function DescribeMyRequestIncludingArguments($api, $args)
    {
        //Find out the request method sent
        $requestType = $_SERVER['REQUEST_METHOD'];

        //Get the data included in the URL parameter
        $data = $args['dataFromURL'];

        //Read from the request body
        $body = file_get_contents('php://input');

        return "You sent a $requestType request including the header argument: `$data` and the body: `$body`";
    }

}

After adding this file, you'll need to perform a Repair and Rebuild in order for Sugar to register your endpoint correctly.

After, if you navigate to rest/v10/Describe/Request in your browser, you should see:

"You sent a GET request."

Now, if you use a REST client to send a POST request, with some data, for example: POST rest/v10/Describe/Request/Stuff with the body {"key":"value"} you should receive:

"You sent a POST request including the header argument: Stuff and the body: {\"key\":\"value\"}"