Hello everyone, I’m glad to join this community. I need your help to get information on how to create custom EndPoints for the Dolibarr REST API. I tried creating one, but I’m facing some issues and I’m not even sure if I’m on the right track. Let me share that I’m currently working on version 20 of Dolibarr in a local environment, and I’m also learning how to develop modules.
The response from the aforementioned attempt is as follows:
Response Body
API not found (failed to include API file)
Response Code
501
Response Headers
{
"access-control-allow-headers": "Content-Type, Authorization, api_key, DOLAPIKEY",
"access-control-allow-methods": "GET, POST, PUT, DELETE",
"access-control-allow-origin": "*",
"connection": "close",
"content-length": "42",
"content-type": "text/html; charset=UTF-8",
"date": "Thu, 13 Mar 2025 01:54:04 GMT",
"server": "Apache/2.4.51 (Win64) PHP/7.4.26",
"x-frame-options": "SAMEORIGIN",
"x-powered-by": "PHP/7.4.26"
}
The code:
<?php
// Incluye los archivos necesarios de Dolibarr
require_once DOL_DOCUMENT_ROOT . '/main.inc.php';
// Definir la clase Colores
class Colores
{
private $db;
public function __construct($db)
{
// Inicializa la conexión con la base de datos
$this->db = $db;
}
/**
* Lista todos los colores.
* @url GET /listar
* @return array Lista de colores
*/
public function listar()
{
// Consulta SQL para obtener todos los colores
$sql = "SELECT rowid, nombre, codigo FROM " . MAIN_DB_PREFIX . "colores";
$resql = $this->db->query($sql);
if (!$resql) {
// Si ocurre un error en la consulta, lanza una excepción
throw new RestException(500, "Error al recuperar los colores: " . $this->db->lasterror());
}
// Array para almacenar los resultados
$colores = [];
// Recorre los resultados y llena el array
while ($obj = $this->db->fetch_object($resql)) {
$colores[] = [
'id' => $obj->rowid,
'nombre' => $obj->nombre,
'codigo' => $obj->codigo
];
}
// Devuelve los colores
return ['success' => true, 'data' => $colores];
}
}