> For the complete documentation index, see [llms.txt](https://sdk.logicdialog.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sdk.logicdialog.ai/sample-project.md).

# Sample Project

Along with the [Quick Start](/quick-start.md) pages, we've also provided a [sample project via gitlab](https://drive.google.com/file/d/1G6CUM1B1FIR_bo9W6A9m1X8djfuKx_FR/view?usp=sharing) which can help you get up and running. This project is designed as as Google Cloud Function meaning that it'll only be running for the times that it needs to be used. You can [find out more about cloud functions here](https://cloud.google.com/functions).&#x20;

Within the project you'll find the following folders :&#x20;

* `src` - this is the main folder for the project. All of your application logic should go in here.&#x20;
  * `src/config` - here we've provided a way of allowing you to swap configuration variables between environments.&#x20;
  * `src/const` - Any constants that might be used in your code. As an example we provide `const` variables for environment.
  * `src/handlers` - This contains the logic for your function and will be the entry point for the function.&#x20;
  * `src/model` - this provides a utility function to help manage the configuration between environments.&#x20;
  * `src/util` - a few other utility functions that you might find useful.&#x20;
* `scripts` - add any build scripts you need in here. We've provided one script in this folder to help get your environment variables into you project.&#x20;
* `package.json` - As a NodeJS project, this file determines the tasks that you can run, as well as any dependancies you might need for your application.&#x20;
* `tsconfig.json` - This is the configuration for the `TypeScript` files.&#x20;
* `webpack.config.js` - This is the configuration for `Webpack`

### Setup

To get the project running you will first need a NodeJS environment on your computer. We'd recommend using `Node Version Manager` or `nvm` for short to help you manage the various different versions of NodeJS to install.&#x20;

{% embed url="<https://github.com/nvm-sh/nvm>" %}

The page above provides instructions for installing `nvm`. Once installed, we recommend a version `12.x` of NodeJS. You can install this on your computer by running **`nvm install 12`**.&#x20;

### Building the Project

The file contains all the various scripts and commands you will need to build the source code into an executable format.&#x20;

To start with you'll need to run **`npm install`** from within the project folder to install all the external dependancies listed in the `package.json`. These dependancies will include `TypeScript`.&#x20;

The project source code is written in `TypeScript` however as part of the build process we compile the typescript into normal JavaScript. This is done using the **`tsc`** command. As defined in the `package.json` file, we have provided a `watch` command which will run this command with the `-w` flag so that it automatically builds any modified files in the source directory. Depending on how you are working you can either run `tsc` once, or **`npm run watch`** to build the files as they change.&#x20;

{% hint style="info" %}
If you are running `npm run watch` its useful to do this in a different terminal window so that you can also start the cloud function in another.&#x20;
{% endhint %}

The result of the `tsc` command will be a series of files in the `.build` folder which mirror that of the `src` folder, but are `.js` files rather than `.ts` files. It is these files that are used to run the cloud function.&#x20;

### Running the Cloud Function

To start the cloud function, use **`npm start`**. This will use the `functions-framework` tool that emulates what it will be like running the function via Google Cloud. This process will start the webserver and expose the application on typically port `8080`. This means that once running you should be able to send network requests to your application via `http://localhost:8080`&#x20;

{% hint style="info" %}
Note that the application itself doesn't provide any user interface and so accessing that URL via a browser will not work.&#x20;
{% endhint %}

As mentioned in the documentation, to test this when running locally you will need something like `ngrok` to make your local application available to the internet. [You can read more about that here](/quick-start.md). Once you have done this, or if the application is deployed to a public URL then you can modify the settings of your digital assistant to point to it.&#x20;

{% embed url="<https://intelagent.wbb.ai/integrations/webhooks>" %}

### Package & Deploy

Also provided within the `package.json` file is a `build` task that will help you package the files in an appropriate format for a live environment. This process will combine all the files into one, and "minimise" them so that they can be transferred and loaded as quick as possible. To run this process use **`npm run build`**. The output of this task will be in the `.webpack` folder and will be essentially an `index.js` file, along with a `package.json` file.&#x20;

As each environment is different we have not provided samples to help you deploy your application to something like Google Cloud, however if you have followed the steps above then you should have everything you need. For reference, the documentation for Google Cloud is below.&#x20;

{% embed url="<https://cloud.google.com/functions/docs/deploying/filesystem>" %}

In this context, we would use the following command to deploy this project, however please adapt this to suit your needs.&#x20;

```
gcloud functions deploy sample_project --source=.webpack --entry-point handler --runtime nodejs12 --trigger-http --allow-unauthenticated --region europe-west2
```

If you have used environment variables with your application then we'd recommend the use of a env.yaml file to provide the variables to the Cloud environment. More information is available below.&#x20;

{% embed url="<https://cloud.google.com/functions/docs/configuring/env-var>" %}

To make use of this file, simply add **`--env-vars-file env.yaml`** to the end of the deployment command line options.&#x20;
