API-based agents enable the outsourcing of complex business logic to external services. They combine the power of AI agents with the flexibility of external APIs to automate complex processes and seamlessly integrate existing systems. These agents are ideal for scenarios that require specialized computations, access to external data sources, or integration with existing microservices.

API-based agents form the bridge between the Enneo platform and your individual IT landscape. They enable the outsourcing of all business logic to external services, maintaining full control over data flows and processing steps. Standardized communication via HTTP allows for the connection of any system - from internal microservices to external SaaS solutions.

Unlike rule-based agents, whose logic is directly implemented in Enneo, API-based agents rely on a clear separation between the Enneo platform and the actual business logic. This not only allows for higher scalability and better maintainability, but also the reuse of existing services and infrastructures.

<?php

use EnneoSDK\Api;
use EnneoSDK\IntentInfo;
use EnneoSDK\IntentOption;
use EnneoSDK\Interaction;

require(getenv()['SDK'] ?? 'sdk.php');
/** @var stdClass $in */

// Create a mocked API response:
//{
//    "data": {
//    "_action": null
//  },
//  "options": [
//    {
//        "type": "success",
//      "name": "Ok",
//      "icon": "check",
//      "recommended": true,
//      "order": 0,
//      "handler": ""
//    }
//  ],
//  "infos": [
//    {
//        "type": "success",
//      "message": "API-Aufruf erfolgreich",
//      "extraInfo": null,
//      "code": null
//    }
//  ]
//}
$interaction = new Interaction($in);
$interaction->infos[] = new IntentInfo(
    type: 'success',
    message: 'API-Aufruf erfolgreich'
);
$interaction->options[] = new IntentOption(
    type: 'success',
    name: 'Ok',
    recommended: true
);

// TODO: replace this "echo" API call by your own API call. Use ApiEnneo::executeUdf to use custom code.
// E.g.:
//ApiEnneo::executeUdf(
//    'customApiCall',
//    [
//        "method" => "POST",
//        "api" => sprintf('contract/%s/anyApi', $in->_metadata->inputParameters->contractId),
//        "params" => (array) $in
//    ]
//);
$response = Api::call(
    method: 'POST',
    url: 'https://echo.enneo.ai',// echo.enneo.ai
    params: json_decode(json_encode($interaction), true)
);
$result = $response;

echo json_encode($result);
exit();