# Invoking REST API inside a process/diagram

**URL:** <https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217>\
**Category:** Q&A\
**Created:** [8 August 2018 12:27 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217 "2018-08-08T12:27:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gulack](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gulack](https://community.ofelia.com/u/gulack)\
**Post date:** [8 August 2018 12:27 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217/1 "2018-08-08T12:27:25Z")

</div>

Hi,

What’s the correct way to invoke the REST API from inside a BPM/process? I’m getting the 401 Unauthorized response when trying to use the REST Connector to a bonita API.

When invoking it externally, I understand that you need to call the login service and use the received X-Bonita-API-Token cookie. But inside a BPM what’s the proper way? Should we use the same logic, with the execution of a REST Connector to the login service, processing the response headers and then send it as a cookie?

If i’m already executing the process inside the bonita scope, it seems that you’d have to do this kind of logic. Is there any other way?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/community_bonitasoft/original/2X/6/6b7b18c0478f2645369e82e009b65e3b97f2cc12.svg) [@system](https://community.ofelia.com/u/system)\
**Post date:** [16 August 2018 21:24 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217/2 "2018-08-16T21:24:31Z")

</div>

What about using the Java API (from a Groovy Script Connector) instead of REST API Connector? Usually, REST API is a good choice when your client does not use Java language.

Here is the official [documentation](https://documentation.bonitasoft.com/bonita/7.7/engine-api-overview) in case you wanna give a try.

---

<div class="post-metadata">

**Author:** ![gulack](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gulack](https://community.ofelia.com/u/gulack)\
**Post date:** [24 August 2018 10:54 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217/3 "2018-08-24T10:54:03Z")

</div>

Hi Pierrick,

That sent me in the right direction. I hadn’t come back to this since your reply, but in a couple of minutes I’m already being able to read/search for processes.

For now, and for future reference, here’s how it is:

```
import org.bonitasoft.engine.api.LoginAPI
import org.bonitasoft.engine.api.ProcessAPI
import org.bonitasoft.engine.api.TenantAPIAccessor
import org.bonitasoft.engine.bpm.flownode.ArchivedActivityInstanceSearchDescriptor
import org.bonitasoft.engine.bpm.flownode.impl.internal.UserTaskInstanceImpl
import org.bonitasoft.engine.search.Order
import org.bonitasoft.engine.search.SearchOptions
import org.bonitasoft.engine.search.SearchOptionsBuilder
import org.bonitasoft.engine.search.SearchResult
import org.bonitasoft.engine.session.APISession

LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI();
APISession session = loginAPI.login("%USERNAME%", "%PASSWORD%");

ProcessAPI papi = TenantAPIAccessor.getProcessAPI(session);

SearchOptionsBuilder searchBuilder = new SearchOptionsBuilder(0, 100);
//searchBuilder.filter(ArchivedActivityInstanceSearchDescriptor.ROOT_PROCESS_INSTANCE_ID, processInstance.getId());
//searchBuilder.filter(ArchivedActivityInstanceSearchDescriptor.ASSIGNEE_ID, myUser.getId());
searchBuilder.sort(ArchivedActivityInstanceSearchDescriptor.NAME, Order.ASC);

SearchOptions so = searchBuilder.done();
SearchResult res = papi.searchAssignedAndPendingHumanTasks(so);

Iterator it = res.getResult().iterator();

while (it.hasNext()){
	UserTaskInstanceImpl o = it.next();

	// Do whatever
}

return "whatever";

```

Thank you very much!

---

<div class="post-metadata">

**Author:** ![gulack](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gulack](https://community.ofelia.com/u/gulack)\
**Post date:** [8 August 2018 13:31 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217/4 "2018-08-08T13:31:39Z")

</div>

The final goal is to read all BDM entities/objects (in a specific state). Maybe there’s some better way for that, without using the REST Connector.

---

<div class="post-metadata">

**Author:** ![gulack](https://avatars.discourse-cdn.com/v4/letter/g/f08c70/32.png) [@gulack](https://community.ofelia.com/u/gulack)\
**Post date:** [24 August 2018 13:37 UTC](https://community.ofelia.com/t/invoking-rest-api-inside-a-process-diagram/9217/5 "2018-08-24T13:37:46Z")

</div>

For completion/correction of my previous answer, if you’re using this in a groovy script inside a process, you won’t need to perform the API login. You may get the ProcessAPI (and others) directly from the apiAccessor variable.

Greetings
