API V8 Custom Endpoint

As I couldn’t find any info on how to get some custom endpoints up and running, I thought I’d share back here.
My install is in /opt/suitecrm so change that as needed.

  1. Make sure composer is installed - https://getcomposer.org/download/

cd /opt/suitecrm
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php --install-dir=bin --filename=composer
  1. Add a new autoload path to composer.json (I called mine ‘CustomApi’)

"autoload": {
  "psr-4": {
    "SuiteCRM\\": [
      "lib/",
      "include/",
      "tests/SuiteCRM/",
      "tests/unit/lib/SuiteCRM/"
    ],
    "SuiteCRM\\Custom\\": [
      "custom/lib"
    ],
    "SuiteCRM\\Modules\\": [
      "modules/"
    ],
    "CustomApi\\": [
      "custom/application/Ext/Api/V8/"
    ]
  },
  "classmap": [
    "Api/"
  ]
},
  1. Create some files using the new namespace that you created
    https://github.com/benperiton/suitecrm_custom_v8

  2. Dump autoload


cd /opt/suitecrm
bin/composer dump-autoload

Then you should be able to hit your endpoints

1 Like

Hi,

I was really interested to see your post. I’ve been going through hell trying to add a simple endpoint to return some data for one of our apps and I’ve been getting nowhere.

I tried what you suggested, but I’m getting the following error:

# Slim Application Error

The application could not run because of the following error:

## Details

**Type:**  Error

**Message:**  Class 'CustomApi\Controller\HapController' not found

**File:**  /Users/michael/Documents/WebDev/HYGH Projects/HYGH-Spot-CRM/custom/application/Ext/Api/V8/controllers.php

**Line:**  10

This is if I am using the normal

use Psr\Container\ContainerInterface as Container;

in my code.

If I use your

use Interop\Container\ContainerInterface as Container;

in the services.php and controller.php then I get this instead

# Slim Application Error

The application could not run because of the following error:

## Details

**Type:**  TypeError

**Message:**  Argument 1 passed to Api\Core\Loader\CustomLoader::{closure}() must be an instance of Interop\Container\ContainerInterface, instance of Slim\Container given, called in /Users/michael/Documents/WebDev/HYGH Projects/HYGH-Spot-CRM/vendor/pimple/pimple/src/Pimple/Container.php on line 118

**File:**  /Users/michael/Documents/WebDev/HYGH Projects/HYGH-Spot-CRM/custom/application/Ext/Api/V8/controllers.php

**Line:**  10

Any ideas would be really appreciated. There is virtually no documentation and I’m going around in circles.

Stupid me. It was failing due to the abandonment of interop and replacement by psr container of course.

I used the composer.json you showed in your example and that was causing me a problem.

Went back to the default and added in the deifnition for the Custom\ item, and everything is working.

Thanks for making that post else I’d still be running around in the dark not knowing what was going on. It would be nice if there were more tutorials and more documentation available for V8 as it seems to be very badly covered. I’ll put up my findings when I’m done so it will hopefully help someone in future.

Hi,

Would you be able to clarify what you mean by

Went back to the default and added in the deifnition for the Custom\ item, and everything is working.

Thanks.