Integrating Rest API with BPMN
Tags: camunda, beginners, workflowengine
Published: 2022-02-15 19:30:18

In previous article, we learned about how to create BPMN and set up an environment. If you have not gone through yet, follow this link: https://dev.to/jeyaauthithan/how-to-get-started-with-camunda-platform-5eik 

At the end of this article, you will be able to write your own BPMN and integrate with REST API. 

Let’s get started!

As we have already downloaded bpm spring boot project , Lets add below dependency in pom.xml to make use of Groovy scripting in BPMN.

          <dependency>
             <groupId>org.codehaus.groovy</groupId>
	     <artifactId>groovy-all</artifactId>
	     <version>3.0.0-rc-2</version>
	     <type>pom</type>
	  </dependency>

We have below BPMN with us which we created in previous article. Now let’s integrate it with Business logic.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pss5mcs4z5aj0r1wlmxo.png)

T1 is the service task which can be used to integrate with REST APIs.

In Camunda modeler, Click on T1 and expand Properties panel which is present in right middle.
In properties panel we can see id, name, Implementation dropdown. Leave id and name as is and click on Implementation dropdown.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xr7vhqkuwi5vv61zgu8l.png)

Choose “Connector” option to integrate Rest API. Upon selection, the link “Must Configure Connector” will appear. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jejj1gya8zasgvpe15a6.png)

Click on the link and you will be directed to the next tab. 
Before filling the values, lets checkout the fields here.

- Connector-id: We can provide “http-connector” for REST call integration and “soap-http-connector” for Soap call integration.

- Input parameters: These are the parameters required to make the REST call. 
      url – End point of REST API
      method – HTTP method (ex. GET, POST, PATCH, PUT, DELETE etc.)
      headers – HTTP headers
      payload – Request payload (Not applicable for GET method)

Now that you understood these input parameters, click on the “+” symbol next to Input parameters and fill the values as shown in below screenshots:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3w9ubwwrodh34hvc282.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0q3qvpukxnj4pdzk574.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zcci4v5p77iz55nc9dp.png)

Output parameters: These are the parameters we are expecting to receive from the REST API.

- statusCode – HTTP status code which denotes the request status (ex. 200, 201, 500)
- headers – HTTP response headers
- response – response body returned from REST API.

Let’s fill output parameters as shown in below screenshot.
    
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y0dwlvyi8kgcissr0sau.png)

Now that REST API is integrated and we have response for Checking ticket availability, We can write the logic and add condition to our sequence flow.
Add one more output parameter and store the variable with the result (preferably Boolean).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wa6cb5ikukpr9z66qvt5.png)

Use this variable and add condition in the Sequence flow.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfrrwfndda0wuodbr9mp.png)

We have used conditionType as Expression which will be of below format:
Format: ${conditions}
Ex. ${condition1 && condition2 || condition3}

As for second “Ticket Not available” sequence, we can either add negative condition of same flag or we can set it as default sequence flow. 
Sequence flow which is marked as Default will be chosen if no other sequence flow(s) conditions are met.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhr7r2topnm62cc201m1.png)
   
Now for our T2 task, as it is script task, we can incorporate any business logic. Instead of directly integrating REST API from bpm, if we choose to do it in microservice layer, we can write the code to make Rest call in our microservice class and invoke it from bpm.

Checkout below script. utilityClass is the object of the class which contains method bookTicket.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qed7y6aa4n08iyxbjerb.png)

In other script tasks T3, T4, T5, Lets simply put print statements.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xwb9zb2popisf1tt84h7.png)
  

## How to deploy BPMN in the engine:

If you have not yet integrated the BPMN with REST API, make all the tasks as script tasks and add some print statements and try deploying it engine in by following below steps.

   1. Place the BPMN in src/main/resources folder in the Spring boot microservice.
   2. Start the microservice. All the BPMNS placed in src/main/resources will be deployed into engine automatically.
   3. Open Camunda GUI URL:     
 http://localhost:8080/service/demo/rest/camunda/app/welcome/default/#!/welcome
   4. Login with default username and password configured in the 
     application.yaml file
   5. Click on cockpit

Under process definition, we can see the processes deployed. Currently we can see 1 process deployed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a4xt1lagosagpm56v294.png)
On click of the process name, we can see the BPMN we created. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt7nodiby1eyz5tds4we.png)

We can debug running and failed process instances here along with error message.
Take your time and explore the options here in GUI.

Hope you understood how to integrate REST API with BPMN in this article. Let’s see about more notations and its usages and also how to start process instance in the next article.

Happy Learning! 😊