How Create Custom API V8 in Suite CRM 8

261 views Asked by At

How to Create Custom API V8 in Suite CRM 8

Trying to create a custom API in SuiteCRM 8 using V8, here I am posting my solution in answer hope this will help the developer community.

after investing a few days finally able to figure out the solution.

Regards Sanjeev

1

There are 1 answers

0
Sanjeev Mallik On

Creating a custom API in SuiteCRM 8 involves a few steps. Here's a high-level overview of the process:

Define the API endpoint:

  • Decide on the functionality you want to expose through your custom API.
  • Determine the URL endpoint for your API, such as custom/application/Ext/Api/V8/.

Create a new Custom API controller:

  • In the SuiteCRM 8 source code, navigate to the public/legacy/custom/application/Ext/Api/V8/Controller directory.
  • Create a new PHP file for your API endpoint, e.g., DatesController.php.

namespace CustomApi\Controller;
use Api\V8\Controller\BaseController;
use Exception;
use Slim\Http\Request;
use Slim\Http\Response;
if (!defined('sugarEntry') || !sugarEntry) {  die('Not A Valid Entry Point');}


class DatesController extends BaseController
{
    public function getDates(Request $request, Response $response)
    {
        return "Sucess: getDates";
    }

    public function updateDates(Request $request, Response $response)
    {
        return "Sucess: updateDates";
    }
}

Register the API route:

  • In the SuiteCRM 8 source code, navigate to the public/legacy/custom/application/Ext/Api/V8/Config directory.
  • Create a new PHP file, e.g., routes.php.
  • Define the route for your custom API endpoint, specifying the URL, HTTP method, and controller class.

$app->get('/testApi', function() {
    return 'Sucess: Response from Custom Test API';
});

$app->post('/dates/{ProductId}', 'CustomApi\Controller\DatesController:updateDates');
$app->get('/dates', 'CustomApi\Controller\DatesController:getDates');

**Extend Controller **

In the SuiteCRM 8 source code, navigate to the public/legacy/custom/application/Ext/Api/V8/ and create a new file controllers.php

<?php
require 'custom/application/Ext/Api/V8/Controller/DatesController.php';
use Api\V8\Controller;
use Psr\Container\ContainerInterface as Container;
use Api\V8\Controller\DatesController;


return [
        'CustomApi\Controller\DatesController' => function (Container $container) {
            return new CustomApi\Controller\DatesController();
    }
    ];




Test your custom API:

Start your local development server. Make API requests to the endpoint you defined, e.g., http://localhost/api/v8/custom/dates. Verify that the API is functioning as expected and returning the desired responses.

Make it work add into composer file inside

autoload": {
"CustomApi\\": [
        "custom/application/Ext/Api/V8/"
      ]
}