Skip to content

CiviCRM API4 Best Practices

When calling api4 in a page (registration, event or contribution), be sure to use ->setCheckPermissions(FALSE) to avoid "Authorization failed" error when a page is access by an anonymous (not loggend in) user.

$contacts = \Civi\Api4\Contact::get()
  ->setCheckPermissions(FALSE)
  ->setLimit(25)
  ->execute();
Alternate method

Joinery code should use the ->setCheckPermissions(FALSE) method mentioned above. Developers should also be aware of (but not use in new code) a shorthand method \Civi\Api4\Contact::get(FALSE) which is available starting in CiviCRM v5.29.0.

$contacts = \Civi\Api4\Contact::get(FALSE)
  ->setLimit(25)
  ->execute();
Check this StackExchange question for more info about the two methods.