# Cloudflared with Custom Domain

This documentation outlines how to set up a **Cloudflare Tunnel (`cloudflared`)** with **OpenAlgo** to receive TradingView alerts securely on your local machine&#x20;

### 1. Overview

A Cloudflare Tunnel creates a secure, outbound-only connection between your local OpenAlgo server and the Cloudflare network. This allows TradingView (which is on the public web) to send HTTP POST requests (Webhooks) to your local machine (`localhost:5000`) securely.

{% embed url="<https://www.youtube.com/watch?v=Lld-TgysQp8>" %}

***

### 2. Installation

To follow the video tutorial, install `cloudflared` on your Windows machine using PowerShell:

```powershell
# Install via Winget (Windows Package Manager)
winget install Cloudflare.cloudflared
```

*Note: After installation, restart your terminal to ensure the command is recognized.*

***

### 3. Configuration in OpenAlgo

Before starting the tunnel, you must tell OpenAlgo what its public address will be.

1. Open your OpenAlgo folder in **VS Code**.
2. Open the `.env` file.
3. Locate the `HOST_SERVER` variable and update it with your intended domain:

   ```env
   HOST_SERVER="https://demo.openalgo.in"
   ```
4. Save the file and start OpenAlgo: `python app.py` (running on port 5000).

***

### 4. Setting Up the Tunnel (Two Options)

#### Option A: Custom Domain (Recommended for Production)

This keeps your Webhook URL permanent.

1. **Login to Cloudflare:**

   ```powershell
   cloudflared tunnel login
   ```

   *A browser window will open. Select your domain to authorize.*
2. **Create the Tunnel:**

   ```powershell
   cloudflared tunnel create openalgo
   ```

   *This generates a JSON credentials file. Note the path provided in the terminal.*
3. **Configure `config.yml`:** In your OpenAlgo project, locate or create `.cloudflared/config.yml` and fill it:

   ```yaml
   tunnel: <YOUR-TUNNEL-ID>
   credentials-file: C:\Users\<Name>\.cloudflared\<TUNNEL-ID>.json

   ingress:
     - hostname: demo.openalgo.in
       service: http://127.0.0.1:5000
     - service: http_status:404
   ```
4. **Route DNS:**

   ```powershell
   cloudflared tunnel route dns openalgo demo.openalgo.in
   ```
5. **Run the Tunnel:**

   ```powershell
   cloudflared tunnel --config .\.cloudflared\config.yml run openalgo
   ```

***

#### Option B: Free Temporary Tunnel (Quick Testing)

If you don't own a domain, you can generate a random public URL.

1. **Run the command:**

   ```powershell
   cloudflared tunnel --url http://127.0.0.1:5000
   ```
2. Cloudflare will generate a URL like `https://random-words-generated.trycloudflare.com`.
3. **Warning:** Every time you restart this tunnel, the URL will change. You will have to update your TradingView Webhook URL and `.env` file accordingly.

***

### 5. Integrating with TradingView

Once the tunnel is "Active" in your terminal:

1. **Get Webhook URL:** Your URL will be `https://yourdomain.com/api/v1/placeorder`.
2. **Generate Payload:**
   * Go to OpenAlgo Local Dashboard -> **Platforms** -> **TradingView**.
   * Configure your symbol (e.g., `SILVERMARFUT`), action (`BUY`), and quantity.
   * Click **Generate JSON** and Copy the code.
3. **Create Alert in TradingView:**
   * Set your Condition (e.g., Price Crossing).
   * Under **Notifications**, check **Webhook URL** and paste your tunnel URL.
   * Under **Message**, paste the JSON payload generated by OpenAlgo.

***

### 6. Verification & Performance

* **Order Book:** When the alert triggers, check the **Order Book** tab in OpenAlgo to see the executed trade.
* **Latency Monitor:** Go to **Logs** -> **Latency Monitor**.
  * Cloudflare Tunnels typically offer latency between **150ms – 300ms**, which is faster than Ngrok or Pagekite in many regions.
* **Security:** Because the tunnel is outbound-only, your local IP address remains hidden from the public internet.

***

### 7. Troubleshooting

* **404 Error:** Ensure the `service` URL in `config.yml` matches the port OpenAlgo is running on (default is `5000`).
* **HTTPS Requirement:** TradingView **requires** an `https` URL. Ensure you use the Cloudflare provided link and not your local IP.
* **Tunnel Fails to Start:** Check if another instance of `cloudflared` is running in the background.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.openalgo.in/installation-guidelines/getting-started/cloudflared-with-custom-domain.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
