# Inbound Requests

## Inbound Requests with App APIs

When configuring App APIs, you can manage the events that occur when an inbound request comes in. For example, if an App API receives a request, that request can be handled through the API event, which can trigger a [Data Flow](../../studio/data-flows), [start a chat/voice bot](../../actions/the-start-chat-bot-action), or any other Action. This is useful because it allows you to handle the data from the API request however it is needed, and that data can be used throughout the Journey.

This section will cover how to handle an inbound API request and store that data into AirData. Prerequisites include the following:

* [An API has been created in Airkit](application-apis)
* An [App Object](../airdata/airdata-app-objects) has been created in AirData with the following properties: id, first\_name, last\_name
* User understands [Data Flows](../../studio/data-flows)

### Create the App API

For this example, the App API will be configured as a POST request and the request body will look like the following:

```javascript
{  
  "id": "1001",  
  "first_name": "Michael",  
  "last_name": "Scott"  
}
```

This will be placed in the request body under the Sample URL, which is used to create a sample payload. When clicking the '**Play**' icon, this will generate a sample payload that can then be accessed through the '**payload**' namespace.

![organizing info](https://a01-support.airkit.com/receiving-inbound-http-api-requests-with-your-app/mceclip0.png)

For information on how to create an App API, validation, mapping, and response codes, see [Creating an API for your Airkit App](application-apis).  &#x20;

### Mapping the Request to a Journey Event

In order to map the API request to the journey, there are two approaches - passing the request to **session.start** or passing it to the journey event. This example will show how to pass the request body to the **event** namespace, but you can easily pass it to **session.start** by passing the payload to the 'Start Parameters' under the Mapping section of the API configuration. For further reading, [Creating an API for your Airkit App](application-apis) includes a breakdown of each of the Mapping properties.&#x20;

Under the Mapping section, enter **payload.body** under Journey Event Payload. This will surface the request body in the API request handler event and allow you to access the id, first\_name, and last\_name from the payload.&#x20;

![](https://a01-support.airkit.com/receiving-inbound-http-api-requests-with-your-app/mceclip0%20\(1\).png)

### Creating the Data Flow to pass the request to AirData

Once the API is set up, a Data Flow will need to be created to pass the request body to an App Object in AirData. The Data Flow created for this example will need to capture the id, first\_name, and last\_name and then write that to AirData.

Create a Data Flow and name it 'Insert User Object'. Add three text input variables. The request properties will be passed into the data flow inputs.&#x20;

![](https://a01-support.airkit.com/receiving-inbound-http-api-requests-with-your-app/mceclip1.png)

Then add the [AirData data operation](../../data-operations/airdata-request-data-operation), select the App Object that you want to store the values to, and change the Type to **PUT**. Then in Objects to insert or update, add:

```javascript
[  
  {  
   "id":id,  
   "first_name":first_name,  
   "last_name":last_name  
  }  
]
```

The object service Data Operation should look like the image below:

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-0716243436b1991bc61220617281b73fe6595d94%2Finbound-requests-v1715-0.png?alt=media) &#x20;

### Running the Data Flow in the API event

Every API created in Airkit has an event associated with it. In the event, you have the option to run many different actions when that API endpoint receives a request. To add actions to the event, go to **Journey Builder** > **Integrations** > **App API** > Select the API you created > Go to the **Actions** tab in the inspector.  ![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Finbound-requests-v1715-1?alt=media).gif)

Go to **App** > Add the [Run Data Flow action](../../data-operations/the-run-data-flow-data-operation) and select the Data Flow created in the previous step. In the inputs, enter in the following:

* **id**: event.id
* **first\_name**: event.first\_name
* **last\_name**: event.last\_name

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-69c9acd972e3d7f35990da5e3da923d4a8a03a57%2Finbound-requests-v1715-2.png?alt=media)

Using the event namespace, you can access the data that was passed to the Journey Event Payload in the previous Mapping step.&#x20;

Now the application is set up to receive a POST request and will write the request body back to AirData.&#x20;

### Additional Use Cases

An inbound API can be used to trigger many different actions within Airkit. One use case, is to use that inbound API to initiate a chat conversation via SMS from an external system. For example, for tracking statuses, an external system can be used to call the inbound API to initiate a journey that sends an update to that user about their status being updated or changed. They can then be sent a 'canvas link' to view more details for what is being tracked within that SMS as well.&#x20;

Another use for an inbound API is to use it to change or update a web page on an existing session. For example, an inbound API can be mapped to an existing session and when an external system makes a request to that API, it can trigger an update to that user's session and take them to a new view. For example, this is useful for waiting on an external system to verify information of a user and will make a request back to the application once it is done verifying.

## Inbound Requests with Subscriptions (web hooks)

### Understanding Subscription and Events

Subscriptions (webhooks) are a mechanism to send data when triggered by an event in an application. Within an Airkit application, you can set up subscriptions with the Airkit's first party integrations such as Twilio, Zendesk, Salesforce, Google, and Stripe. For example if you use the Salesforce Subscription, you can start a journey when a the status of a lead is updated or if a record is created.&#x20;

**Setting up a Subscription**

In this example, we'll be setting up a subscription to Salesforce whenever a Lead is added.

1. Before creating a subscription, [set up a connection to Salesforce](../../building-apps/integration-guides/create-a-salesforce-lead) and then add the integration to your Airkit Application. Once connected, go to **Configuration Builder**and add the Salesforce adapter in **Integrations.** Click on **+New** and then **Adapters,** and select **Salesforce**.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-4d3bed44866d07ae9fb0b65c64e22c53f9f214e2%2Finbound-requests-v1715-3.png?alt=media)

2. Select the **Credential** associated with Salesforce that you would like to use and **Save**. This will enable your app to use the Salesforce integrations when creating a Subscription.&#x20;

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-5f8e941b529c84dafefa1553d124b9ada1a11a7f%2Finbound-requests-v1715-4.png?alt=media)

3. Go to **Connection Builder** and click on '**+**' to add a Salesforce subscription.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-e6b24b46ba08b6af6bc4056601ba50f57a3ff596%2Finbound-requests-v1715-5.png?alt=media)

4. Select a webhook category. For this example, we'll be starting a journey when a new record is added.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-8882b5d219115d6dd356b1b7dc5bf062a24a769c%2Finbound-requests-v1715-6.png?alt=media)

5. Choose an **Object Type**. This example will be using the Lead object type.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-770fb1fbe39a56dc846d04f5c3a1d41444909277%2Finbound-requests-v1715-7.png?alt=media)

After selecting the Object Type, you can define what happens after a lead is added by configuring the journey mapping.

**Configuring the Journey Mapping**

If you have [created an API for your Airkit App,](application-apis) then the journey mapping configuration may look familiar.&#x20;

### Sample Payload

This is a sample so you can use to know how to build your request. After expanding the Sample Payload, you can hit the Play button to see what that payload would look like.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-795272a8bca358503465387bb3d35df6a1fb179f%2Finbound-requests-v1715-8.png?alt=media)

**Mapping** The mapping section is where you can specify what happens once your subscription receives an event. For example, when a lead gets added into salesforce, you can:

* Start a new journey
* Replace a current journey
* Abort a journey if a match is found
* Lookup and use an existing journey

\*\*Start Parameters \*\*

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-d25d7deb521b263814a5abf85c8428c01843a0e8%2Finbound-requests-v1715-9.png?alt=media)

Start parameters is where you can pass any type of data that is available from your subscription to the journey to **session.start.** Start parameters are only applicable when your **Journey Behavior** is 'Start or Create a new Journey'.&#x20;

```javascript
session.start
```

**Journey Identifier Expression**![](https://a01-support.airkit.com/passing-data-from-external-systems/mceclip2.png)

The [journey identifier expression](../../concepts/journeys) is where you can map an identifier on a session from a piece of data out of the payload or connection namespace. For example, if there was a specific Salesforce ID that came out of the request payload, you could then use that Salesforce ID as the journey identifier. You are given the options of what type the identifier is as well -- Text, Journey ID, or Phone.&#x20;

**Journey Event Payload**![](https://a01-support.airkit.com/passing-data-from-external-systems/mceclip3.png)Journey event payload is where you can pass data from your subscription into the **event** namespace.

For example, if you pass

```javascript
payload.result
```

into the Journey event payload, then on your subscription event, you can access the data via

```javascript
event.property_name
```

**Response** This is where you can define what your subscription will return after the actions are executed.&#x20;

Response Status is where you can perform validation using Airscript to have your subscription return a particular response status code. This example checks to see if the Phone property is empty, and returns a 200 if it is not empty.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-fa2f6e836479617b1164f81c5fa89162142c52d4%2Finbound-requests-v1715-10.png?alt=media)

Response Body is the response from the subscription. The response body accepts data from the metadata, payload, and connection namespace.&#x20;

### How Publishing an App subscribes and unsubscribes from an external web hook

When publishing an app, the subscription (web hook) is set on the external system. So when creating the subscription, the subscription has not actually been registered yet until the app has been published. Alternatively, when the app is unpublished, the web hook is unset from the external system.
