# Backend API

## Jump to

* [Running Locally](#running-locally)
* [Prerequisites](#prerequisites)
* [Environment Variables](#environment-variables)
* [Deployment](#deployment)

## Running Locally

Running this application requires [Node.js (Version 20+)](https://nodejs.org/en/download/).&#x20;

To run the application, [fulfill all prerequisites](#additional-prerequisites) and `cd` into the `/backend` folder.&#x20;

Add a [.env file](https://dev.to/dallington256/how-to-use-env-file-in-nodejs-578h) with all the [requisite environment variables](#environment-variables) - you don't need to specify the `HOST` and `PORT` variables to run the app locally.

Install dependencies, by running:

```bash
npm install
```

Run the app, using the following command:

```bash
node app
```

Please ensure that port `8080` is available and that you are not connected to a VPN before you run the app.&#x20;

## Prerequisites

Setting up the Backend API requires an instance of MongoDB Atlas, Clerk, Cloudflare Images and an API key for the OpenAI Platform.

### Setup up a MongoDB Atlas instance

The project uses MongoDB Atlas as its datastore.

[This tutorial](https://www.mongodb.com/basics/create-database) provides guidance on setting up a database on MongoDB Atlas. You do not need to create collections once your database is setup - this will be done automatically when the application connects with the database.

Make sure to name the database "`ninjarecipes`".

Using any instance of MongoDB, other than MongoDB Atlas, will break the search feature and the [scheduled workers](https://dhruv-tech.gitbook.io/ninjachefs/scheduled-workers) used for [cleaning submission tracking logs](https://dhruv-tech.gitbook.io/ninjachefs/scheduled-workers/chuck-norris).

### Setup up a Clerk instance

[**Clerk** ](https://clerk.com)is used as the authentication service for the project.

Thus, setting up a Clerk account and obtaining an API key is a mandatory requirement for running the application.

[This tutorial](https://clerk.com/docs/quickstarts/setup-clerk) provides a detailed walkthrough of setting up an account and obtaining a `Clerk Secret Key`.

### Configure Cloudflare Images

The API is designed to use Cloudflare Images as the CDN for image delivery. Documentation for Cloudflare Images is available [here](https://developers.cloudflare.com/images/cloudflare-images/).&#x20;

Once your account is setup and you've obtained your `Cloudflare Account ID` and `API key`. You'll need to [configure](https://developers.cloudflare.com/images/cloudflare-images/transform/resize-images/) the following image variants in your dashboard:

| Property         | Value        |
| ---------------- | ------------ |
| **Variant Name** | **ncHeader** |
| Width            | 1366         |
| Height           | 768          |
| Fit              | Cover        |
| Metadata         | Strip All    |
| Make Public      | Yes          |
| Blur             | 0            |

| Property         | Value           |
| ---------------- | --------------- |
| **Variant Name** | **ncThumbnail** |
| Width            | 405             |
| Height           | 300             |
| Fit              | Cover           |
| Metadata         | Strip All       |
| Make Public      | Yes             |
| Blur             | 0               |

### Get an OpenAI Platform Key

All AI features in the application have been built on top of the OpenAI SDK and require an `API Key` to access models hosted by OpenAI. You can obtain an API key by following this [guide](https://www.howtogeek.com/885918/how-to-get-an-openai-api-key/).

## Environment Variables

<table><thead><tr><th width="216">Variable</th><th>Description</th><th>Purpose</th></tr></thead><tbody><tr><td><pre class="language-properties"><code class="lang-properties">MONGODB_SRV
</code></pre></td><td>API Key to communicate with MongoDB Atlas Instance.</td><td>Auth</td></tr><tr><td><pre class="language-properties"><code class="lang-properties">CLERK_API_KEY
</code></pre></td><td>API Key to authenticate and communicate with Clerk.</td><td>Auth, Session Management</td></tr><tr><td><pre class="language-properties"><code class="lang-properties">OPENAI_KEY
</code></pre></td><td>API Key to authenticate with OpenAI Models.</td><td>Auth</td></tr><tr><td><pre class="language-properties"><code class="lang-properties">CLOUDFLARE_ID
</code></pre></td><td>Cloudflare Account ID</td><td>Auth</td></tr><tr><td><pre class="language-properties"><code class="lang-properties">CLOUDFLARE_TOKEN
</code></pre></td><td>API key to access Cloudflare Images</td><td>Auth</td></tr><tr><td><pre data-overflow="wrap"><code>HOST
</code></pre></td><td>Host for binding the runtime process, <code>127.0.0.1</code> if unspecified.</td><td>Config</td></tr><tr><td><pre><code>PORT
</code></pre></td><td>Port for binding the runtime process, <code>8080</code> if unspecified.</td><td>Config</td></tr></tbody></table>

## Deployment

The application can be simply deployed by specifying its [configuration details](#configuration-details) and adding [environment variables](#environment-variables), when you use modern app hosting services such as Railway and DigitalOcean App Platform.

You may refer to these guides, for such services:

* [Deploy Node.js app on DigitalOcean App Platform](https://www.youtube.com/watch?v=4hdDDPLvpnQ).
* [Deploy Node.js app on Railway](https://alphasec.io/how-to-deploy-a-nodejs-app-on-railway/).

If you wish to manually deploy this application on a server, please follow these guides:

* [Deploy Node.js app on a Linux server (Debian-based)](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-20-04).
* [Deploy Node.js app on a Windows server.](https://dev.to/massivebrains/deploying-node-express-app-on-a-windows-server-2l5c)

### Configuration Details

| Property               | Value         | Addl. Details                                                                                |
| ---------------------- | ------------- | -------------------------------------------------------------------------------------------- |
| Runtime                | Node.js       | `Version 20`                                                                                 |
| Build Command          | None          |                                                                                              |
| Build output directory | None          |                                                                                              |
| Install Command        | `npm install` |                                                                                              |
| Start Command          | `node app`    | Default `HOST` is `127.0.0.1`, Default `PORT` is `8080`. Change using environment variables. |
| Root directory         | `/backend`    |                                                                                              |
| Entrypoint             | `./app.js`    |                                                                                              |
