# Application APIs

## Introduction

Sometimes you need to interact with your Application or its data outside of Airkit. With Airkit's Application APIs, you can build custom API endpoints for an application that allow you to create journeys, modify existing journeys, or get information back from journeys.

## Add Web Resource to org

Before starting, a URL resource is required to be set up in order to be utilized by the API. Go to [console.airkit.com](http://console.airkit.com), go to [Resources](adding-and-modifying-resources) (left-hand side nav), and create a new resource by clicking the “**New**” button. The URL resource will have to be configured depending on the scenario - for this example, the configuration looks like this:

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

This will generate a url that will serve as the API endpoint. For this example, that endpoint will be [https://app.airkit.com/c/AppApi](https://app.airkit.com/c/AppApi).&#x20;

## Tokens

If the app API requires Authentication and should not be publicly available, a token must be created. For this you will need an API key that can be generated from the studio console (API > Tokens):

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-ba86ccfcb94e094458a567e19a9c7c284fc309c7%2Fapplication-apis-v1715-1.gif?alt=media) \[block:callout] { "type": "warning", "body": "Make sure to safely store the generated token at that moment since it will not be retrievable afterwards." } \[/block] ## Securing API Endpoints with Tokens and Permissions

An App API can be configured to require authentication or to be publicly available.

#### Creating a Token

If the App API requires authentication, a token must be created. To create a token, go to [console.airkit.com](http://console.airkit.com) and click on **API** in the left hand navigation. Then click on **Tokens** > '**+New**' > enter in a name for the token. You can also configure user variables and IP addresses for additional layers of permissions.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Fapplication-apis-v1715-2?alt=media).gif)

Then click **Create**, which will generate a token.

#### Configuring the API to require Authentication

To configure the API to require Authentication, go to [Connection Builder](../../studio/builder-bar/connection-builder) and select the API created under '**Web Links**'.

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-b1df715b553c22260fff85e76b2accfcfbaf455b%2Fapplication-apis-v1715-3.png?alt=media) Then Click on the checkbox under Requires Authentication. This will enforce the API to have a Bearer token with the API Request.  ![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-68494f57e769a6741daea1426383e3a335c0e3e0%2Fapplication-apis-v1715-4.png?alt=media)

To add an API Key Group, which is used to give API access to particular users within an organization, go to Configuration Builder, scroll down to **API Key Filtering**, add an **API Key Group**. Then select the Token name created previously.

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

Once the API Key group is configured, then it can be configured to the API.

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

#### Testing the App API

To test an API that requires authentication, the following header must be set:\
"**Authorization: Bearer** " where is replaced with the token generated in the "Creating a Token" section. Below is a screenshot of testing the API from Postman.&#x20;

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-1faad27d1f0ed28e8483955c92d6d78b93c87fce%2Fapplication-apis-v1715-7.png?alt=media)

## Configure HTTP resource in Configuration Builder

Once the URL resource is created you will need to configure it in your app. Open the app in the studio, and navigate to [Configuration Builder](../../studio/builder-bar/configuration-builder), and scroll down to the Integrations section.

Create a new “HTTP resource” connector: set any name that is meaningful , and under the Credential dropdown, select the URL resource created in the previous step.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01bc5b6a3d37b7544354bd700ac1bce11bcddd6e%2Fapplication-apis-v1715-8.png?alt=media)

## Create App API

### Creating an API request handler / Endpoint

This section will provide an overview on how to create and configure an endpoint, this is, a specific route within an app that can be invoked externally and that can handle certain types of operations.

To create an endpoint within an app, go to [Connection Builder](../../studio/builder-bar/connection-builder) > Web Links, and click on the Plus (**+**) button. Then select HTTP Request.

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

The http method can be **GET** or **POST**. Typically, GET is to obtain data from the App API, and POST is to send data to the App API (for example, to perform insert or update operations).

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

'URL' will be the resource you created under the Configuration Builder section of the App in the previous step. Please note that the resource must not be in use (this means, each endpoint will have a unique url, and be either GET or POST).  There cannot be endpoints that share the same url, even if they implement different http methods.

To enforce authentication, check the “Requires Authentication” checkbox  - otherwise, if you want your API to be publicly accessible, leave it unchecked. If checked, you’ll need to include an “Authorization” header. For more information, refer to [Securing API Endpoints with Airkit API Tokens & Permissions](https://github.com/airkit/airkit-docs/blob/master/docs/v17-15/securing-api-endpoints-with-airkit-api-tokens-and-permissions.md).

Under Url Format you can specify the URL path parameters (not to be confused with query parameters) to be sent with the request. This is optional. The parameters need to be set between curly braces and separated by a slash. Example:

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

### Configuring the Journey Mapping

Journey Mapping is where to configure the mapping of an API Request to an existing session or conversely, starts a new session. This is also where you can define what validation is needed as a part of the API request by using a Data Flow and can also set a response body and response status.&#x20;

In this section, you’ll be able to configure the behavior and the events to occur once a request is received in an endpoint. To achieve this, there are a few different sub-sections at disposal.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-77fc860086a820ace5757f76127c9588de92f2e4%2Fapplication-apis-v1715-12.png?alt=media)

### Sample Payload

This is a sample so you can know how to build the request. After expanding the Sample Payload, 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-5caeae34c85f13076cb37911fba69217f6f95cb6%2Fapplication-apis-v1715-13.png?alt=media)

### Validation

Validation offers the ability to validate the HTTP request before continuing on to the Journey Mapping step. This is useful to ensure that the HTTP Request received is valid and has all the proper elements that the API is expecting. For example, if the API Request is expecting an integer, the validation step is where you can pass the payload to a Data Flow and use Airscript to check to see if the request is valid. The output of the Data Flow is then passed to the **connection** namespace, which can be used in the validation step.&#x20;

In the example below, we have two path parameters (the same that were specified in the example in in the “Creating an API request handler” section). Since it’s a GET endpoint, there’s no body. If this was a POST endpoint, there could be a body in this payload. You will need to build a Data Flow separately, and set it as the validator step under the **Data Flow** title. This example has a Data Flow set up called "Validation Data Flow", which outputs a concatenated string.&#x20;

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

To pass data to the input fields from the API endpoint, use the namespace **payload** to parse the object coming from the Sample Payload.&#x20;

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

After running the Data Flow by hitting the 'play' icon, the validation action can be used to validate the outputs of the Data Flow using Airscript. The namespace '**connection**' is the result of the Data Flow configured in the validation step. Also, if the validation returns 'FALSE', then the journey mapping will actually be skipped and will return the response. &#x20;

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

**Quick Note**: pay attention to the relevant namespaces here (**payload**, **connection**).

&#x20;

### Mapping

This section is where to define what happens after the request has been received and validated, in regards to the journey in particular. You can start a new journey, replace the existing one, abort it or resume it if a match is found (an identifier must be provided).

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

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

**Journey Identifier Expression**![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-2ff60c421e2a73bde6047eaa8dffde9075120499%2Fapplication-apis-v1715-18.png?alt=media)

The [journey identifier expression](../../concepts/journeys) is where to 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 ID that came out of the request payload, that ID can then be used as the journey identifier. The identifiers can be of type Text, Journey ID, or Phone.&#x20;

**Journey Event Payload** ![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-93517071bd2401facbc0469a56ed450e10d88b6b%2Fapplication-apis-v1715-19.png?alt=media) Journey event payload is where to pass data from the API endpoint into the API Request **event** namespace.

```javascript
event
```

### Response

This is what the App API will return after the specified chain of operations are executed. The status and the body are both customizable.

**isValid** will return the outputs from the validation step.&#x20;

![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-0b4083c8db1c1b5d6709b47a08642640699d0c9f%2Fapplication-apis-v1715-20.png?alt=media) ![organizing info](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-fbff9cf33c95b4e2cca4c40c3ddd87e54bdbaa29%2Fapplication-apis-v1715-21.png?alt=media)

NOTE: pay attention to the relevant namespaces here (**payload**, **connection**, **metadata**, **isValid**)

### 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). One example that this can be useful is for redirecting to a URL. To do this, set the Response Status to 302 if the response is valid.&#x20;

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Fapplication-apis-v1715-22?alt=media).png)

Then, add an HTTP parameter with the Field Name **"Location"** and the value **metadata.canvas\_link.**

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-578ceb6a3f7907c636115abcfd79d465c9e7edc9%2Fapplication-apis-v1715-23.png?alt=media)

## Publishing and Testing

Once finished configuring the App API, the application needs to be published to make the HTTP resource live. To publish, click on the '**Publish**' button in the top right hand corner.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Fapplication-apis-v1715-24?alt=media).png)

For further information on publishing, refer to [Publishing Your Application.](../../building-apps/publishing/publishing-your-application) Once the application is published, the API endpoint is now live and can be receive requests.

To test out the API, there is a feature to simulate HTTP events in the App Preview. This is how to make a request within the App Preview and where to debug and see the different states as well as test out the mapping of the request as well.

When in the studio, go to **App Preview** > **API**. Select **HTTP Request** and enter in the URL of the endpoint and click on **Simulate HTTP Event**. It also provides the capability to pass a request payload as well.

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-01fb9b612c234f682459bf3c96d9a661ad020cf9%2Fapplication-apis-v1715-25?alt=media).png)
