# How to deploy the MongoDB Atlas Boilerplate

import Tag from '~/components/webkit/Tag.vue';

<Tag severity="info">
Preview
</Tag>

The **MongoDB Atlas Boilerplate** allows you to deploy an application integrated with MongoDB Atlas on the edge. Besides, due to the integration with the Atlas Data API, you're able to create, retrieve, update, and delete items in your collections through standard HTTPS requests.

By using **Azion Web Platform** to run your database project, your digital data and assets benefit from edge computing capabilities, including faster delivery, lower latency, and enhanced security.

---

## Requirements

Before using this template, you need to: 

- Create a [MongoDB account](https://www.mongodb.com/cloud/atlas/register).
- Create a [cluster](https://www.mongodb.com/docs/atlas/create-database-deployment/) and add a new database.
- Create a collection to be used with this template.
- Active the service [Atlas Data API](https://www.mongodb.com/docs/atlas/app-services/data-api/).
- Generate API key in [Atlas Service](https://www.mongodb.com/docs/atlas/app-services/authentication/api-key/#std-label-api-key-authentication).
- Have a [GitHub account](https://github.com/signup) to connect with Azion and create your new repository.
  - Every push will be deployed automatically to this repository to keep your project updated.
- This template uses [Application Accelerator](/en/documentation/products/build/applications/application-accelerator/) and [Functions](/en/documentation/products/build/applications/functions/), and it may generate usage-related costs. Check the [pricing page](https://www.azion.com/en/pricing/) for more information.

Any cost generated by MongoDB usage will be processed and billed separately on the MongoDB platform. Visit the [MongoDB Atlas documentation](https://www.mongodb.com/docs/atlas/) for more details.
:::

---

## Deploying the template

You can obtain and configure your template through the Azion Console. To easily deploy it at the edge, click the button below.

 <DocButton
    label="Deploy"
    href="https://console.azion.com/create/mongodb-atlas/mongodb-atlas"
    icon="ai ai-azion"
    size="medium"
 />

---

## Setting up the template

In the configuration form, you must provide the information to configure your application. Fill in the presented fields. 

Fields identified with an asterisk are mandatory.

1. Connect Azion with your GitHub account.
- A pop-up window will open to confirm the installation of the [Azion GitHub App](https://www.azion.com/en/documentation/products/guides/azion-github-app/), a tool that connects your GitHub account with Azion's platform.
- Define your permissions and repository access as desired.
2. Select the **Git Scope** to work with.
3. Fill in the fields:
- **Application Name** *: the name of your application on Azion.
  - The bucket for storage and the function will use the same name.
  - Use a unique and easy-to-remember name. If the name has already been used, the platform returns an error message.
- **Atlas API Token** *: the token to authorize the connection with the AtlasData API.
- **Atlas API URL** *: the assigned URL endpoint to connect with Atlas Data API.
- **Collection Name** *: the name of a created collection in the specified database.
- **Database Name** *: the name of the created database in the MongoDB Atlas data source.
- **dataSource Name** *: the name of the created MongoDB Atlas data source or cluster.
4. Click the **Deploy** button to start the deployment process.

During the deployment, you'll be able to follow the process through a window showing the logs. When it's complete, the page shows information about the application and some options to continue your journey.

:::note
The link to the application allows you to see it on the browser. However, it takes a certain time to propagate and configure the application in Azion's edge locations. It may be necessary to wait a few minutes for the URL to be activated and for the application page to be effectively displayed in the browser.
:::

### Key configurations

By deploying this template, the script creates:

- An application to run your project and manage all your settings.
- An Azion Workload domain to access your application. You can also set up a custom domain to run on this application.
- An function containing custom logic.
- A GitHub repository for your project. The repository includes a GitHub Action that guarantees a continuous deployment workflow. 

The execution of this template also includes integration with Mongo DB Atlas and Atlas Data API, acting as a middleware to manage and access your database.

---

## HTTPS requests examples

The integration with the Atlas Data API enables you to access the data using standard HTTPS requests. Acting as a middleware to access and manage your data, the API sits between your cluster and the client requests.

You're able to create, retrieve, update, and delete items in your collections through any tools that allow you to send standard HTTPS requests, such as your terminal or Postman.

The API has dedicated endpoints according to the operation you want to complete.

#### Finding a document

```bash
curl -s "https://data.mongodb-api.com/app/$CLIENT_APP_ID/endpoint/data/v1/action/findOne" \
  -X POST \
  -H "apiKey: $API_KEY" \
  -H 'Content-Type: application/ejson' \
  -H "Accept: application/json" \
  -d '{
    "dataSource": "my-datasource",
    "database": "my-database-azion",
    "collection": "template",
    "filter": {
      "text": "Do the dishes"
    }
  }'
```

Where: 

| Variable | Required | Type | Description |
|---|---|---|---|
| `dataSource` | Yes | String | The name of your data source or cluster |
| `database` | Yes | String | The name of the database in the data source |
| `collection` | Yes | String | The name of a created collection in the specified database |
| `filter` | Yes | Object | A query operator to filter the documents |

#### Inserting a document

```bash
curl -s "https://data.mongodb-api.com/app/$CLIENT_APP_ID/endpoint/data/v1/action/insertOne" \
  -X POST \
  -H "apiKey: $API_KEY" \
  -H 'Content-Type: application/ejson' \
  -H "Accept: application/json" \
  -d '{
    "dataSource": "my-datasource",
    "database": "my-database-azion",
    "collection": "template",
    "document": {
      "status": "open",
      "text": "Do the dishes"
    }
  }'

Where: 

| Variable | Required | Type | Description |
|---|---|---|---|
| `dataSource` | Yes | String | The name of your data source or cluster |
| `database` | Yes | String | The name of the database in the data source |
| `collection` | Yes | String | The name of a created collection in the specified database |
| `document` | Yes | Object | The document to insert into the collection |

#### Updating a document

```bash
curl -s "https://data.mongodb-api.com/app/$CLIENT_APP_ID/endpoint/data/v1/action/updateOne" \
  -X POST \
  -H "apiKey: $API_KEY" \
  -H 'Content-Type: application/ejson' \
  -H "Accept: application/json" \
  -d '{
    "dataSource": "my-datasource",
    "database": "my-database-azion",
    "collection": "template",
    "filter": {
      "_id": { "$oid": "64224f4d089104f1766116a5" }
    },
    "update": {
      "$set": {
        "status": "complete",
        "completedAt": { "$date": { "$numberLong": "1680105272788" } }
      }
    }
  }'

Where: 

| Variable | Required | Type | Description |
|---|---|---|---|
| `dataSource` | Yes | String | The name of your data source or cluster |
| `database` | Yes | String | The name of the database in the data source |
| `collection` | Yes | String | The name of a created collection in the specified database |
| `filter` | Yes | Object | A query operator to filter the documents |
| `update` | Yes | Object | An update expression to apply to matching documents |

#### Deleting a document

```bash
curl -s "https://data.mongodb-api.com/app/$CLIENT_APP_ID/endpoint/data/v1/action/deleteOne" \
  -X POST \
  -H "apiKey: $API_KEY" \
  -H 'Content-Type: application/ejson' \
  -H "Accept: application/json" \
  -d '{
    "dataSource": "my-datasource",
    "database": "my-database-azion",
    "collection": "template",
    "filter": {
      "_id": { "$oid": "64224f3cd79f54ad342dd9b2" }
    }
  }'

```

Where: 

| Variable | Required | Type | Description |
|---|---|---|---|
| `dataSource` | Yes | String | The name of your data source or cluster |
| `database` | Yes | String | The name of the database in the data source |
| `collection` | Yes | String | The name of a created collection in the specified database |
| `filter` | No | Object | A query operator to filter the documents |

All the requests return a response in `JSON` or `EJSON` format, including the information according to the status code: `200 Success`, `400 Bad Request`, and `401 Unauthorized`.

:::tip
For more examples and to learn how to work with batches, go to the [Atlas Data API](https://www.mongodb.com/docs/atlas/app-services/data-api/) documentation.
:::

---

## Managing the template

Considering that this initial setup may not be optimal for your specific application, all settings can be customized any time you need by using Azion Console.

To manage and edit your application's settings, proceed as follows:

1. [Access Console](https://console.azion.com).
2. On the upper-left corner, select **Products menu** > **Applications**.
- You'll be redirected to the **Applications** page. It lists all the applications you've created.
3. Find the application related to your template and select it. 
- The list is organized alphabetically. You can also use the **search bar** located in the upper-left corner of the list; currently, it filters only by **Application Name**.

After selecting the application you'll work on, you'll be directed to a page containing all the settings you can configure.

:::tip
Read the documentation about [managing applications](/en/documentation/products/build/applications/first-steps/) for more details.
:::

### Adding a custom domain

The application created during the deployment has an assigned Azion Workload domain to make it accessible through the browser. The domain has the following format: `xxxxxxxxxx.map.azionedge.net/`. However, you can add a custom domain for users to access your application through it.

import DocButton from '~/components/webkit/DocButton.vue';

<DocButton href="/en/documentation/products/build/applications/domains/" label="go to Workloads reference" kind="secondary" size="medium" />