Our use case was for a 'free trial' we were offering. The trial was initiated from Cognito Forms via a Webhook.
So we created a custom controller ("CognitoForm.php") and it has this code in it:
Code:
/* Up here is all the class code, constructor, and dependences. Below is just one function */
public function postActionFreeTrial($params,$data,$response)
{
if (empty($params['apiKey'])) throw new BadRequest('No API key provided.');
if (empty($data)) throw new BadRequest('No payload provided.');
$allowOrigin = $this->getConfig()->get('leadCaptureAllowOrigin', '*');
$response->headers->set('Access-Control-Allow-Origin', $allowOrigin);
$apiKey = $params['apiKey'];
$validKey = $this->getServiceFactory()->create('LeadCapture')->isApiKeyValid($apiKey);
if ($validKey) {
$lead = $this->getServiceFactory()->create('Lead')->cognitoFreeTrialLead($data, $apiKey);
return $lead;
} else {
return 'This is not a valid API Key! Make sure you have the right environment and it\'s active!';
}
}
Then, in our routes.json file (custom/Espo/Custom/Resources/routes.json)
Code:
[
{
"route": "/CognitoForm/:action/:apiKey",
"method": "post",
"params": {
"controller": "CognitoForm",
"action": ":action",
"apiKey": ":apiKey"
},
"conditions": {
"auth": false
}
}
]
In this case, ":action" is a variable as we have multiple actions going to that controller and we didn't want to have to write a new route everytime we wanted a new entry point.
To access, we send a POST request from CognitoForms:
Code:
POST: https://{espocrm-domain.com}/api/v1/CognitoForm/freeTrial/{apiKeyGeneratedInLeadCaptureAdminPanel}
Anyways, hope this is helpful to someone!

Leave a comment: