# Adding a Backend Server to Your LARS and CARS Project

Canonical: https://docs.projectbabbage.com/docs/quickstarts/lars-cars-backend-project

Implement a backend server that can communicate with your frontend project with LARS and CARS!

This guide walks you through:

1. Creating a rudimentary backend server
2. Running it locally with **LARS**
3. Pushing it to production with **CARS**
4. Attaching (or sharing) a custom domain

> Assumes you've completed the **Starting a Frontend Project…** quickstart.  
> Later guides will cover implementing the Topic Manager and Lookup Service in depth.

----------

### Prerequisites

- **Node.js** 22 or 24 (maintained and patched)
- **Docker** (with Compose plugin)
- **Git** (optional, but recommended)
- A **BRC-100 wallet** such as BSV Desktop installed and running
- Your **frontend** project already set up with LARS & CARS
    

----------


### 1. Scaffold your backend

```bash
# 1. Create workspace and enter it
mkdir backend && cd backend

# 2. Initialize npm and install dependencies
npm init -y
npm install express
npm install --save-dev typescript ts-node-dev @types/node @types/express
```

Create a `tsconfig.json` in `backend/`:

```json
{
  "compilerOptions": {
    "target": "ES6",
    "module": "commonjs",
    "rootDir": "src",
    "outDir": "dist",
    "esModuleInterop": true,
    "strict": true
  }
}
```

Add a boilerplate express `src/index.ts`:
```ts
import express, { Request, Response } from 'express';

const app = express();
const port = 3000;

app.get('/', (req: Request, res: Response) => {
  res.send('Hello, World!');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});
```

> **Note:** this boilerplate is a placeholder and will be replaced by the overlay system (Topic Manager & Lookup Service) during actual LARS/CARS deployment.

This allows LARS and CARS to invoke your server.

----------

### 2. Update `deployment-info.json` for LARS

Open your root `deployment-info.json` and update your existing frontend deployment config to include `"backend"`:

```diff
"configs": [
  {
    "name": "Local LARS",
    "provider": "LARS",
    "network": "testnet",
    "run": [
      "backend",
      "frontend"
    ]
  },
  …,
]
```

----------

## Local development with LARS

### 3. Launch LARS with both services

```bash
npx lars
```

#### LARS Main Menu (template)

-   **Manage LARS configuration** → **Edit LARS Deployment Info**
-   **Select** “Local LARS” and ensure `run: ["frontend", "backend"]`
-   **Start LARS (local only)**

This will:

- Run your frontend (`npm run start`) and the default backend SHIP and SLAP overlay service.
- Open the frontend in your browser
- Hot-reload both on file changes

----------

### 4. Hook in Topic Manager & Lookup Service

Once your server is live, you'll plug in the Topic Manager and Lookup Service (covered in upcoming [guides](https://docs.projectbabbage.com/docs/guides/overlays/topic-managers)) under `/backend/src`. This will work with the default SHIP and SLAP implementation to add utility to your backend, allowing you to store and access data from the database.

> **Note:** to deploy a backend with CARS, you must implement and register both the Topic Manager and Lookup Service—CARS will not function with only the default overlay logic. See the Guides section for setup instructions.

----------

## Production deployment with CARS

### 5. Update Your Production config

In the same `deployment-info.json`, add:

```diff
"configs": [
  …,
    {
      "name": "Production",
      "provider": "CARS",
      "CARSCloudURL": "https://cars.babbage.systems",
      "network": "mainnet",
      "deploy": [
        "frontend",
        "backend"
      ],
      "frontendHostingMethod": "HTTPS",
      "projectID": "YOUR_PROJECT_ID"
    }
]
```

----------

### 6. Register your backend

Here, we register your backend with CARS, provision a cloud project, and link your deploy configuration.

```bash
cars
```

#### CARS Configurations Menu

- **Manage CARS configurations** → **Edit an existing CARS configuration**
- **Select** the `Production` entry
- Confirm that the following prompts are set:
    
    1.  **Configuration name** (e.g. `my-bsv-deployment`)
    2.  **CARS Cloud URL** Custom → Default ([https://cars.babbage.systems](https://cars.babbage.systems/))
    3. **Network**  Default (mainnet)
    4.  **What to release?** select both `frontend` and `backend`
    5.  **Frontend hosting method** HTTPS

-   Then choose to use your existing Project ID and your Prod deployment should be updated
    
----------

### 7. Build a deployable artifact

```bash
cars build            # or `cars build Production`
```

-   Installs dependencies & runs `npm run build` in `backend/`
-   Packages your code + metadata into `my-bsv-backend-<timestamp>.tgz`
    
Verify:

```bash
cars artifact ls
```

----------

### 8. Release to the cloud

**Fast path:**

```bash
cars release now
```

**Manual path:**

```bash
cars release get-upload-url
cars release upload-files <uploadURL> ./my-bsv-backend-*.tgz
```

Check logs under **Releases** and find your backend URL in **Project Info**.

----------

## What you’ve achieved

- A live **backend** service alongside your frontend that can communicate with one another
- A comprehensive understanding of how to deploy your applications for both **local** (LARS) and **prod** (CARS) usage
- A ready path to integrate Topic Manager and Lookup Service
    
Happy coding!
