# Building an App API

This guide will walk through how to create an App API that triggers off a Journey and passes the data in the body of the POST request to `session.start`. This is a common use of App APIs that allows users to build endpoints that can be triggered from other systems to start an Airkit journey.

## Initializing the App API

App APIs can created in either the Journey Builder or in Triggers Builder. To create an App API in Journey Builder, drag the API trigger to the Triggers pane in the stage of Journey Builder.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-39aa32d8dc5e343cb0ba5ba0e85feb0a80d252c5%2Fbuilding-an-app-api-v181-0.png?alt=media)

You can also click on the '+' icon next to App APIs in the Triggers Builder.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-b91071e5cde61dcf1e627767b80b333d71ddabbf%2Fbuilding-an-app-api-v181-1.png?alt=media)

## Configuring the Base Domain

The base domain is the Base URL for your API endpoint. If your app does not have a base domain configured, go to **Settings** > **Base Domain** and select a base domain. You can also bring your own URL domain in the console. For more information, see [Connecting Your Domain](https://docs.airkit.ai/?q=connecting-your-domain).

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-3cb427cc9cd7d65b978840a6aa226ccba0942f61%2Fbuilding-an-app-api-v181-2.png?alt=media)

### Configuring the URL Route

Once the base domain is configured in settings, you can either select an existing route or create a new URL route. The URL route is a combination of the Base domain and the route for the API endpoint. To create a new URL route, type the name of the route in the input and click on create. This will automatically create the URL route for you and can be found in the console under **Resources**.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-e775a21c8f1c83622e0faecef49f78229049ee42%2Fbuilding-an-app-api-v181-3.png?alt=media)

## Designing the API endpoint

Once your URL Route is configured, click on the nested endpoint in the tree under the App API. This is where you design the flow of your App API, similar to how you would design a Data Flow. This example will walk through creating a POST request to trigger an Airkit Journey.

Start by changing the request type to a POST request and adding a path.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-fec148a123fe3d00e90494b051a76412545bd61c%2Fbuilding-an-app-api-v181-4.png?alt=media)

You can also add Path Parameters and Query parameters to the input as well. To add a Path Parameter, surround a parameter name with curly braces, such as `{paramName}`. Path parameter values can then be accessed via `payload.PathMapping.paramName`.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-c2f8f7deff8fb968c287a518f1b45a83c5709d5c%2Fbuilding-an-app-api-v181-5.png?alt=media)

### Request Body

The Body input is where you can put a sample json object of what your endpoint is expecting. In the Body input, use the following sample data:

```json
{
    "name":"Michael",
    "company":"Airkit",
    "dob":"08/23/73"
}
```

This data is what will be passed through to the journey as well as the `userID` from the path parameters.

#### Sample Payload

The sample payload provides a sample of how the data is structured and what you have access to. For example, if you wanted to parse the request body, that would be accessed through:

```javascript
payload.body
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-424556a827d5a407a04f326d6eb34c11dff8c7c4%2Fbuilding-an-app-api-v181-6.png?alt=media)

## Adding data operations

Once parameters and request body are configured, you can add any data operations as part of your App API.

Create a new object and format the `dob` value to be a formatted date object using the [DATE\_FROM\_FORMAT](../../airscript-expressions/datetime-functions/date_from_format) airscript function. Add a [Transform](../../data-operations/the-transform-data-operation) data operation and in the transform expression, add the following:

```javascript
  { 
     "name": payload.body.name, 
     "company": payload.body.company, 
     "dob": DATE_FROM_FORMAT(payload.body.dob, "MM/DD/YY") 
 }
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-1c81cf0a9583619480eb80733b32a4540c0ec85c%2Fbuilding-an-app-api-v181-7.png?alt=media)

This will output a single object that can then be passed to the journey.

## Validating the request

Before adding a data operation to trigger a Journey, you can add the [Early Return](../../data-operations/early-return) data operation to validate the request. The Early Return data operation can be used to validate an expression and will exit the connection if returned false.

Add the Early Return data operation and use the following expression to check if any of the fields were empty.

```javascript
ISNOTEMPTY(transform.name) AND ISNOTEMPTY(transform.company)
  AND ISNOTEMPTY(transform.dob)
  AND ISNOTEMPTY(payload.pathMapping.userId)
```

If any of the fields are empty, the expression will return false, which will provide the return value of the following:

```json
{ "status": 400, "statusBody": "invalid payload" }
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-a13c875395e8b09f024203f4f265937ee261e881%2Fbuilding-an-app-api-v181-8.png?alt=media)

## Triggering the Journey

To have your App API trigger a Journey, add the [Journey Mapping](../../data-operations/start-journey-data-operation) data operation.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-1cfa8b55242edd464db12a8c934c94742849a8ad%2Fbuilding-an-app-api-v181-9.png?alt=media)

Under **Start Parameters** in the data operation, pass the `transform` variable, which is the result of the previous transform data operation.

If you wanted to also pass the `userId` to the start parameters, you can use the following expression:

```javascript
MERGE_OBJECTS(transform, payload.pathMapping)
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-cf575af4eb93bbad2b9c98975e09c7a210a65464%2Fbuilding-an-app-api-v181-10.png?alt=media)

By running this Journey Mapping Data operation, you are able to get some metadata about the Journey that is being started.

To run an event on this Journey, add the [Run Session Event](../../data-operations/the-run-journey-event-data-operation) data operation and pass the session id into the data operation.

```javascript
journey.session_id
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-0aa3ca26c967f4c387b821141ad277719c1c3f76%2Fbuilding-an-app-api-v181-11.png?alt=media)

#### Creating a session event

Session Events are how you can run actions when a request is made to the API endpoint. To create a session event, go to **Connections Builder** > **Session Events** > '**+**'.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-af48241057558710adfcda60c15abc8ee68e6f6d%2Fbuilding-an-app-api-v181-12.png?alt=media)

On this event, rename it to `API Trigger Event` and in the action chain in the inspector, add the [Navigate To Web Flow](../../actions/navigate-to-web-flow-action) action and navigate to a Web flow configured in the application.

Here is also where you can add a [Send SMS Notification](../../actions/send-sms-notification-action) if you wanted to send an SMS to the end user when their journey starts. This would require having the user's phone number to pass into the start parameters.

Once the Session Event is configured, go back to **Triggers Builder** and select the Session event that was just created in the dropdown list.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-822bce6c1dbfc02cad87f7fe6ba11b613d45c1dd%2Fbuilding-an-app-api-v181-13.png?alt=media)

## Configuring the App API's response

The App API's response is what comes back as a response from the HTTP request when the API endpoint is hit.

### Response Headers

This provides the ability to add an [HTTP header in an HTTP response](https://developer.mozilla.org/en-US/docs/Glossary/Response_header).

### Response Status

This indicates whether a specific HTTP request has been successfully completed. The response status expects an HTTP response status code.

For more information on HTTP response status codes, see [HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).

Using the output of the Early Return data operation, write an `IF()` statement to check to see what response status code to output. If the expression returns true (no fields missing), then return `200`. If the expression returns false (fields are missing), return a `400`.

```javascript
IF(isValid, 200, 400)
```

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-e2f32f714dcc5ba6545391d34c1b6c8be9af75a5%2Fbuilding-an-app-api-v181-14.png?alt=media)

### Response Body

The response body is what is returned as a response to the HTTP Request.

In the response body input, return the details of the journey if the Early Return data operation returns `true`, or return the `Return Value if Validation Fails` value from the Early Return data operation, using the `connection` variable.

![](https://4185914948-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FSItjRhbHaLQ4ar3DUljt%2Fuploads%2Fgit-blob-26a8cf7f67c2a6d0f1aa41d8e037b56bd456979d%2Fbuilding-an-app-api-v181-15.png?alt=media)

## Testing the App API

Now that the App API is configured, you can test out the API endpoint by publishing the application and making a POST request to the endpoint. **Save** the app and then click on **Publish**.

> 📘 If you are sending an SMS as part of the App API workflow, you will need to configure a phone number resource with the application. See [Connecting Twilio Numbers](../../sms/connecting-twilio-numbers) for more information. The endpoint based on the configuration in this example can look like the following with `123456789` as the `userId` configured as a path parameter:

```
https://app.airkit.com/api-example/trigger-journey/123456789
```

You also need to include a request body with the POST request:

```json
{
    "name":"John",
    "company":"Airkit",
    "dob":"04/14/88"
}
```

The response body based off of this should include the metadata of the session that was triggered, including the `canvas_links`, `session_id`, `started_time`, `expiration_time`, and more.
