Keep your Team in sync with API Status
Tags: vrestng, testing, automation, tutorial
Published: 2020-06-02 08:03:05

APIs have become a necessity for every organization to sustain in this modern world. Organizations can incur a great loss of revenue if their APIs are not healthy. If there are any APIs failures, keeping your team in sync with API status becomes extremely important. So that they can react to it quickly.

In this article, we will look at how your team can remain in sync with API status using slack notifications. But you can apply the same procedure with any messaging tool which you are using within your organization.

vREST NG is an API automation tool that provides hooks functionality to make this task extremely easy. And helps us to integrate any eco-system tool of your choice. We will use these hooks to integrate the slack messaging tool.

vREST NG provides several types of hooks but for this integration, we will use the following type of hook:

*Post Test Case Iteration Hook* which executes after each API test case iteration and also provides the test iteration result to the hook.

If you would like to know more about hooks, please read this guide on [Hooks Functionality](https://ng.vrest.io/docs/app/hooks/).

**Download and Setup a project**
You may download vREST NG from our website (https://ng.vrest.io) and upon installation, use the Pro version (comes with 15 days trial period) in order to proceed further.

For setting up a sample test application, we will use this repository https://github.com/Optimizory/examples-vrest-ng

I am assuming that you have already installed NodeJS on your machine. This is only required for our sample test application. If you are following this tutorial for your own application then you don't have to install NodeJS.

Execute the following command to clone this repository:
```
$ cd <DIR_OF_YOUR_CHOICE>
$ git clone git@github.com:Optimizory/examples-vrest-ng.git
$ cd examples-vrest-ng
$ npm install
$ npm start
```
By default, the application will start on port 5090. You may access the application in your browser at https://localhost:5090.

This test application already contains multiple vREST NG projects. For this article, we will use the project `test/scenario-tests` and we will set up this project in the vREST NG Application.

For setting up this project, simply drag the `scenario-tests` directory in the vREST NG Application workspace area. For more information, please read this [guide link](https://ng.vrest.io/docs/app/new-user.html).

Now, our project has been set up, You may execute the test cases in the Test Cases Tab to see if they are working. If any of the test cases are failing then it is fine for this tutorial. Now, we will look at how we can integrate slack with vREST NG.

**Slack Integration**
**1. Configure Slack incoming webhook**
You will need to first configure the incoming webhook in your slack app. Incoming Webhooks are a simple way to post messages from external sources into Slack. To learn more about configuring slack's incoming webhook, please visit the slack guide on incoming webhook.

**2. Configure global variables in vREST NG Application**
Just visit the Configuration tab and specify the following variables:

slackWebHookURL: The value which you have received in the previous step.
slackChannel: The name of the slack channel.
slackUsername: The user name on behalf of you will send the message.

![Global Variables required for Slack Integration](https://dev-to-uploads.s3.amazonaws.com/i/k2sd4qk2p7y0njy08j0r.png)

I am attaching a sample screenshot for your reference. But specify the values as per your context.

**3. Send individual API failure notification through Slack**
Now that you have configured the incoming webhook in Slack. Now we will use that hook to send the API failure notifications through vREST NG. For this, we will use the "Post Test Case Iteration Hook". We will be using the following details for setting up this hook.
```
Method: POST

URL: {{slackWebHookURL}}

Summary: Send Slack Notification on API Test Case Failure

Condition: {{$tc.result.isExecuted}} && !{{$tc.result.isPassed}} && '{{$tr.details.environment}}' === 'default'

Request Body:
{
  "channel": "{{slackChannel}}",
  "username": "{{slackUsername}}",
  "icon_emoji": ":ghost:",
  "text": "--- *Test Case Failure Notification* --- \n*Test Run Name:* {{$tr.details.name}} \n*API Endpoint:* {{$tc.request.method}} {{$tc.request.url}} \n*Summary:* {{$tc.details.summary}} \n*Executed By:* {{$tr.details.executor.name}} - {{$tr.details.executor.email}} \n*Assertions Summary:* \n{{$tc.result.assertionSummary}} \n*Result Link:* <{{$tc.result.resultLink}}>"
}
```
Now, let's see the step by step instructions for setting up this hook:

1. Visit Hooks Tab in vREST NG Application

2. Now create a Post Test Case Iteration Hooks as shown in the figure below:
![Post Test Case Iteration Hook Dialog](https://dev-to-uploads.s3.amazonaws.com/i/g0b8auy2upson8qxzlqc.png)

3. Click on the Confirm button to create the Hook. Now, specify the Request Body as per the following screenshot:
![Post Test Case Iteration Hook Request Body](https://dev-to-uploads.s3.amazonaws.com/i/o3bfkk2wdoplhkh3f2rj.png)

4. That's it. Your hook is now ready. It will send notifications on slack on each API failure. Change the hook condition if you would like to execute this hook only in the "Production" environment. You may customize the request body to whatever notification text you want to send to Slack. vREST NG makes available all of the test case data and test run data as well to the hook using variables `{{$tc}}` and `{{$tr}}`. For more information on the data format, read this guide on Hooks format.

5. Now, let's try to execute our test cases. Make sure there should be some test case that is failing. e.g. In our test application, the third test case is failing as shown in below screenshot:
![Failing Test Case](https://dev-to-uploads.s3.amazonaws.com/i/63xg4s3jlex6496zf3ls.png)
Now click on Hooks sub-tab available in the rightmost pane to see the details of the executed hook. Hooks are again similar to test case, so it will show you the following details:
![Hook Results](https://dev-to-uploads.s3.amazonaws.com/i/mtkd650rg9aj1wqtpn8a.png)

6. Now let's see the notification we received in our slack application:
![Slack Notification](https://dev-to-uploads.s3.amazonaws.com/i/9bodkddb7hufkbl1h5we.png)

It's really cool. It provides all the necessary information to analyze API failure. It even provides the Diff report. As you can see in the last that there is a result link which will only be available if you opt for the vREST NG Dashboard service. This link will provide an online report to debug the API failure in much more details. And that report is shareable among all the configured team members. You may see the following reports in the vREST NG Dashboard service:

![vREST NG Dashboard Results](https://dev-to-uploads.s3.amazonaws.com/i/y68wly69f5bgecix4f42.png)
![vREST NG Dashboard Execution Details](https://dev-to-uploads.s3.amazonaws.com/i/9msntahsdu83a5l7p466.png)
This is really awesome. Your team is always in sync with the API status using slack notification and using the vREST NG Dashboard service. Everybody knows which APIs have failed and why they have failed.

Do let me know your feedback on what you think about this solution. Do comment and share your experience with us if you are also facing issues related to communication within your organization.