# App Schema

Your app schema is defined in the `index.js` file. The app schema defines all the configurations as well as the plugins
that your app creates. The app schema is a defined in JSON format. When you scaffold an app, a default schema is
created.

```json
{
  "id": "YOUR_APP_NAME",
  "version": "YOUR_APP_VERSION",
  "platformVersion": "APP_PLATFORM_VERSION",
  "authentication": "THE_AUTHENTICATION_SCHEMA",
  "beforeRequest": "THE_BEFORE_REQUEST_SCHEMA",
  "afterResponse": "THE_AFTER_REQUEST_SCHEMA",
  "actions": "THE_ACTIONS_SCHEMA",
  "messaging": "THE_MESSAGING_SCHEMA",
  "resources": "THE_RESOURCES_SCHEMA"
}
```

## PROPERTIES

| PROPERTIES | DESCRIPTION | VALUE TYPE | DEFAULT VALUE |
|  --- | --- | --- | --- |
| id | The identifier of your App | [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#string_type) |  |
| version | The version of your app | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | version specified in package.json |
| platformVersion | The version of app-platform package you used to create the app | [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number) | version specified in package.json of the `@chatlayerai/app-platform` in node modules. |
| [authentication](/docs/chatlayer/basics/authentication) | The authentication schema. This schema is used to authenticate the accounts which will be used in this app | [Authentication Schema](/docs/chatlayer/app-schema/authentication-schema) | {} |
| [beforeRequest](/docs/chatlayer/app-schema/before-request-schema) | The schema of things to do before calling [sdk.request](/docs/chatlayer/actions/sdk-object) method. They are the middlewares that you add before making any sdk request. | Array of [functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) | [] |
| [afterResponse](/docs/chatlayer/app-schema/after-request-schema) | The schema of things to do after calling [sdk.request](/docs/chatlayer/actions/sdk-object) | Array of [functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) | [] |
| [actions](/docs/chatlayer/app-schema/actions-schema) | The schema of plugins that you want to create which shows up in [Action Dialogstate](https://docs.chatlayer.ai/bot-answers/dialog-state/action-bot-dialog) in chatlayer. | [Actions Schema](/docs/chatlayer/app-schema/actions-schema) | {} |
| [messaging](/docs/chatlayer/app-schema/messaging-schema) | The schema of sending and receiving messages from different channels like whatsapp, facebook messenger etc.. | [Messaging Schema](/docs/chatlayer/app-schema/messaging-schema) | {} |
| [resources](/docs/chatlayer/app-schema/resources-schema) | The schema of the resources that you want to provide to the app. This can be used later | [Resources Schema](/docs/chatlayer/app-schema/resources-schema) | {} |


## Example

```javascript
const includeApiKey = (request, sdk, bundle) => {
    if (bundle.authData.api_key) {
        request.headers = {
            ...request.headers,
            "Content-Type": "application/json",
            Authorization: `Bearer ${bundle.authData.api_key}`,
        };
    }

    return request;
};
const authenticationConfig = {
    type: "custom",
    test: (sdk, bundle) => {
        return sdk
            .request({
                url: `https://${bundle.authData.host}/v2/agents`,
            })
            .then((res) => {
                if (res.status === 400) {
                    throw new Error("Invalid region");
                }
                if (res.status === 401) {
                    throw new Error("Invalid API key");
                }
            });
    },
    connectionLabel: () => "FreshChat",
    fields: [
        {
            key: "host",
            label: "Region",
            placeholder: "Choose your datacenter region",
            default: "api.freshchat.com",
            choices: {
                "api.freshchat.com": "United States",
                "api.eu.freshchat.com": "Europe",
                "api.in.freshchat.com": "India",
                "api.au.freshchat.com": "Australia",
            },
        },
        {
            key: "api_key",
            label: "API Key",
            required: true,
            helpText:
                "You can generate an API Key by login to your FreshChat account and navigating to Admin > API Tokens > Generate Token",
        },
    ],
}

module.exports = {
    version: require("./package.json").version,
    platformVersion: require("@chatlayerai/app-platform").version,
    id: "freshdesk-messaging",
    authentication: authenticationConfig,
    beforeRequest: [includeApiKey],
    actions: {},
    messaging,
    resources: {},
};
```