Enneo allows for the embedding of custom code in the form of “User Defined Functions” and applying it in various areas. These functions can be used notably in the business logic of rule-based AI agents, in webhooks, or event implementations; anywhere where custom code or SDKs are utilized.
Enneo can store and utilize custom functions. The following calls are possible, using a User-Defined-Function named ‘my-udf’ as an example:
Within Enneo using Python SDK:
In the second step, a custom function fetch-thirdparty-token can be implemented to load the credentials. This approach is especially useful when loading the credentials can be reused in several other processes.
Copy
Ask AI
<?php// Load Enneo SDKrequire(getenv()['SDK']);// Load storage object for custom functions$udfStorage = \EnneoSDK\Setting::get('udfStorage');// Loading the token$clientId = $udfStorage->thirdParty->oauth->clientId;$clientSecret = $udfStorage->thirdParty->oauth->clientSecret;$result = \EnneoSDK\Api::call( method: 'POST', url: $udfStorage->thirdParty->oauth->tokenEndpoint, headers: [ 'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret), 'Content-Type: application/x-www-form-urlencoded', 'Accept: application/json' ], params: http_build_query(['grant_type' => $udfStorage->thirdParty->oauth->grantType ?? 'client_credentials', 'scope' => $udfStorage->thirdParty->oauth->scope ?? null]));// The output is in JSON format and can be used in the corresponding code after the callecho json_encode(['accessToken' => $result->access_token, 'accessTokenExpiresAt' => date("Y-m-d H:i:s T", time() + $result->expires_in), "baseUrl" => $udfStorage->thirdParty->baseUrl, "headers" => $udfStorage->thirdParty->headers ?? [], "apiName" => $udfStorage->thirdParty->apiName ?? "Customer API",]);
Subsequently, the implementation of the function third-party-api-call for the actual API call can occur:
Copy
Ask AI
<?php// Load SDKrequire(getenv()['SDK']);// Load the token using the previously defined "fetch-thirdparty-token" function$credentials = \EnneoSDK\ApiEnneo::executeUdf('fetch-thirdparty-token', []);// Load input parameters$in = \EnneoSDK\Input::load();// Prepare the header for the Third-Party-API request$headers = [ 'Authorization: Bearer '. $credentials->accessToken, 'Accept: application/json'];foreach (($credentials->headers ?? []) as $key => $val) { $headers[] = "$key: $val";}// API call and output$res = \EnneoSDK\Api::call( method: $in->method, url: $credentials->baseUrl.'/'.$in->api, headers: $headers, params: $in->params);// Output resultecho json_encode($res);
This structure allows for flexible use and integration of custom code within Enneo to depict specific business logic or seamlessly connect external systems.