> ## Knowledge Base Index
> Fetch the complete knowledge base index at: https://help.squarecloud.app/sitemap.xml
> Use this file to discover available pages before exploring further.
> Pure-Markdown content can be obtained by appending a '.md' suffix to the content URLs listed in the sitemap (without the trailing slash).

# How to host a Flask application (app.run host 0.0.0.0)

Need to **host your Flask application** and set `app.run(host="0.0.0.0")` to accept external connections? This guide shows the step-by-step to put your Flask app online on Square Cloud, with Gunicorn in production, running 24/7.

## 1. Prerequisites
* **Square Cloud Account:** Create your account via the [Registration Page](https://squarecloud.app/en/signup) (Email or GitHub).
* **Active Plan:** A paid plan is required for hosting. Check the [pricing here](https://squarecloud.app/en/pricing).

---

## 2. Production Configuration (Gunicorn)

Although Flask has a built-in server (`app.run()`), it is not suitable for production. **Gunicorn** allows your application to handle multiple concurrent requests efficiently.
For Square Cloud to route traffic correctly, Gunicorn must be configured to listen on the **0.0.0.0** interface and port **80**.

### Application Example (`app.py`)
```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Flask running in production on Square Cloud!'

if __name__ == '__main__':
    # app.run is only used locally
    app.run(host='0.0.0.0', port=80)
```

---

## 3. Performing the Deployment

1. **Compression:** Create a `.zip` file with your source code and the `requirements.txt`. Do not include cache folders like `__pycache__` or virtual environments (`venv`). The `requirements.txt` must be at the project root and contain at least:
```text
flask
gunicorn
```
2. **Upload:** In the Square Cloud Dashboard, click on upload new application.
3. **Web Publishing:** In the submission menu, check the **"Publish to Web"** option.
4. **Subdomain:** Choose a name for your free subdomain (e.g., `my-api.squareweb.app`).
5. **Custom Start Command:** To use Gunicorn, you must define a custom start command at the time of submission (or in the `squarecloud.app` configuration file). Use the following format:
```bash
python -m gunicorn --bind 0.0.0.0:80 app:app
```
* **`--bind 0.0.0.0:80`**: Exposes the application on the port required by the platform.
* **`app:app`**: Indicates that Gunicorn should look for the `app` object (the second "app") inside the `app.py` file (the first "app").

---

## 4. Custom Domains (Standard+ Plan)

Users on the **Standard plan or higher** can configure their own domains:
* Add your domain (e.g., `api.yoursite.com`) in the **Network** tab.
* Square Cloud automatically manages **SSL/HTTPS**, ensuring your communication is encrypted at no additional cost.

---

## 5. Optimization Tips

* **Workers:** If your application receives a lot of traffic, you can adjust the number of workers in the start command (e.g., `--workers 3`).
* **Environment Variables:** Use the **ENV** tab in the Dashboard to store `SECRET_KEY`, API keys, and database URLs.
* **Logs:** Monitor the Dashboard terminal to identify initialization errors or Python exceptions in real-time.