apiAccessor in Groovy Function

I’m writing a very simple Groovy Function to return a list, example below:

import org.bonitasoft.engine.api.ProcessRuntimeAPI;

def static fn(String processName, String key){

ProcessRuntimeAPI processRuntimeAPI = apiAccessor.getProcessAPI();
String processName = processRuntimeAPI.getProcessInstance(processInstanceId).getName();

List groupList = apiAccessor.getIdentityAPI().getGroups(0, 1, GroupCriterion.NAME_ASC);

return GroupList;
}

but when I try to use apiAccessor it tells me

Groovy:Apparent variable 'apiAccessor' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:

How do I use apiAccessor within a Groovy Function?

Many thanks in advance,
regards
Seán