---
parser: v2
time: 20
auto_validation: true
tags: [ tutorial>intermediate, topic>cloud, software-product>sap-business-technology-platform]
primary_tag: software-product>sap-btp--kyma-runtime
slug: cp-kyma-microservice-trigger
canonical_url: https://developers.sap.com/tutorials/cp-kyma-microservice-trigger
---

# Trigger a Microservice with an Event
<!-- description --> Trigger a microservice to run when an event is published into SAP BTP, Kyma runtime.

## Prerequisites
 - [`kubectl` configured to kubeconfig downloaded from SAP BTP, Kyma runtime](cp-kyma-download-cli)
 - [Eventing and NATS modules enabled](https://help.sap.com/docs/btp/sap-business-technology-platform/enable-and-disable-kyma-module)
 - [Kyma Eventing backend configured](https://help.sap.com/docs/btp/sap-business-technology-platform/choose-backend-for-kyma-eventing)
 - [Deploy a Go PostgreSQL API Endpoint in SAP BTP, Kyma Runtime](cp-kyma-api-postgres-golang) tutorial completed
 - [Deploy the SAPUI5 Frontend in SAP BTP, Kyma Runtime](https://developers.sap.com/tutorials/cp-kyma-frontend-ui5-postgres.html) tutorial completed
 - [Deploy the Commerce Mock Application in SAP BTP, Kyma Runtime](cp-kyma-mocks) tutorial completed

## You will learn
  - How to trigger a microservice with an event

## Intro
This tutorial relies on the Commerce mock application to publish events into SAP BTP, Kyma runtime. After binding the commerce mock application to the `dev` namespace, we will create a service instance of the SAP Commerce Cloud - Events. The service instance will allow for any microservice or lambda function within the `dev` namespace to subscribe to these events by defining an event subscription. The subscription pairs an event source, the commerce mock application, and the event type, **order.created**, to a subscriber which in this case will be the Go PostgreSQL API microservice.

---

### Clone the Git repository

1. Go to [kyma-runtime-extension-samples](https://github.com/SAP-samples/kyma-runtime-extension-samples).

2. Choose the **Code** button and choose one of the options to download the code locally, or simply run the following command using your CLI at your desired folder location:

   ```Shell/Bash
   git clone https://github.com/SAP-samples/kyma-runtime-extension-samples
   ```

### Explore the sample

1. Open the `api-postgresql-go` directory in your local editor.

2. Explore the content of the sample.

    Within the `internal/api/events.go` file you can find the code that handles the consumption of the event data. This code expects to receive a JSON payload containing an `orderCode`. This function is exposed on the path `/orderCodeEvent`, which is defined in `cmd/api/main.go`.

    Within the `k8s` directory you can find the `event.yaml` file containing the event subscription definition which defines how an event will be consumed. The fields of interest include the following which would have to be altered if a different namespace or application name was used:

| Property                                | Description                                                   | Value                                                         |
|-----------------------------------------|---------------------------------------------------------------|---------------------------------------------------------------|
| **spec.filter.filters.eventType.value**     | The event source and version to subscribe to                  | `sap.kyma.custom.mp-commerce-mock.order.created.v1`             |
| **spec.sink**                               | The URI endpoint of the service that receives the event       | `http://api-postgresql-go.dev.svc.cluster.local:80/orderCodeEvent` |

### Apply an event subscription

In this step, you will define an event subscription used to create an event subscriber. This allows you to specify that your `api-postgresql-go` API application, by referencing its service, should receive the payload of the **order.created** event.

1. Apply the subscription by running the following command in the CLI:

   ```Shell/Bash
   kubectl -n dev apply -f ./k8s/event.yaml
   ```

2. Verify that the subscription was created successfully by running this command:

   ```Shell/Bash
   kubectl get subscription api-postgresql-go-event-sub -n dev -o yaml
   ```

    After the event subscription definition, find a status object indicating the status of the related resources:

   ```yaml
   status:
     conditions:
     - lastTransitionTime: "2021-05-06T14:54:50Z"
       reason: NATS Subscription active
       status: "True"
       type: Subscription active
     emsSubscriptionStatus: {}
     ready: true
   ```

### Test scenario

With the configuration steps completed, you can now test the scenario to validate that it is working as intended.

1. Open the mock application in the browser by choosing **Discovery and Network > API Rules** from the menu.

2. Choose the **Host** entry for the **commerce-mock** APIRule to open it in the browser. This URL should be similar to:
`https://commerce-mock.*******.kyma.ondemand.com`

3. Choose the **Remote APIS** tab.

    ![Test the Scenario](https://raw.githubusercontent.com/sap-tutorials/btp-foundation/main/tutorials/cp-kyma-microservice-trigger/test-scenario-1.png)

4. Choose the **SAP Commerce Cloud - Events** option.

5. For the **Event Topics**, choose **order.created.v1**.

6. Modify the `orderCode` value as desired and choose **Send Event**.

    ![Test the Scenario](https://raw.githubusercontent.com/sap-tutorials/btp-foundation/main/tutorials/cp-kyma-microservice-trigger/test-scenario-2.png)

7. In Kyma dashboard, choose **Discovery and Network > API Rules** from the menu.

8. Choose the **Host** entry for the **fe-ui5-postgresql** APIRule to open the application in the browser. This should be similar to:
`https://fe-ui5-postgresql.*******.kyma.ondemand.com`

9. You should now see the data received by the event as shown below:

    ![Test the Scenario](https://raw.githubusercontent.com/sap-tutorials/btp-foundation/main/tutorials/cp-kyma-microservice-trigger/test-scenario-3.png)

---