FastAPI
Step-by-step guide to deploy a FastAPI Python application on Out Plane
This guide walks you through deploying a FastAPI application on Out Plane using Git-driven deployment with a Dockerfile.
Prerequisites
- An Out Plane account
- A GitHub account
- Python 3.10+ installed on your machine
- Git installed on your machine
Create the FastAPI App
Create a new directory for your project:
mkdir my-fastapi-app
cd my-fastapi-appCreate a virtual environment and activate it:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activateInstall FastAPI and Uvicorn:
pip install fastapi "uvicorn[standard]"Create requirements.txt:
pip freeze > requirements.txtCreate a file called main.py:
from fastapi import FastAPI
app = FastAPI(title="My FastAPI App")
@app.get("/")
async def root():
return {"message": "Hello from Out Plane"}
@app.get("/health")
async def health():
return {"status": "healthy"}Create the Dockerfile
Create a file called Dockerfile in your project root:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]Create a .dockerignore file:
__pycache__
*.pyc
.git
.env
venv
.venv
*.md
.DS_StoreTest Locally
Run the development server:
uvicorn main:app --reloadOpen http://localhost:8000 in your browser. You should see the JSON response.
FastAPI automatically generates interactive API documentation at http://localhost:8000/docs.
Push to GitHub
Initialize a Git repository and push to GitHub:
git init
git add .
git commit -m "Initial commit"
git remote add origin [email protected]:YOUR_USERNAME/my-fastapi-app.git
git branch -M main
git push -u origin mainReplace YOUR_USERNAME with your GitHub username.
Deploy on Out Plane
Step 1: Install GitHub App
- Go to console.outplane.com
- Navigate to Applications from the sidebar
- Click Deploy Application
- If prompted, click Install GitHub App
- Select which repositories Out Plane can access
- Click Install and you'll be redirected back
Step 2: Select Repository
- Find your repository in the list
- Click to select it
- Choose the branch to deploy (default:
main) - Set Root Directory to
/
Step 3: Configure Build
| Setting | Value |
|---|---|
| Build Method | Select Dockerfile |
| Application Name | Enter a unique name (e.g., my-fastapi-app) |
| Port | 8000 |
Step 4: Select Resources
| Instance | vCPU | RAM | Recommended For |
|---|---|---|---|
| op-20 | 0.5 | 512 MB | Development, small APIs |
| op-46 | 1 | 1 GB | Production APIs |
| op-82 | 2 | 2 GB | Heavy workloads |
Step 5: Configure Scaling
- Min Scale: Set to
1for always-on, or0for scale-to-zero - Max Scale: Set based on expected traffic
Step 6: Environment Variables (Optional)
Click Add Variable to add environment variables:
| Key | Value |
|---|---|
DATABASE_URL | Your database connection string |
SECRET_KEY | Your application secret |
Step 7: Deploy
- Review your configuration
- Click Create Application
- Wait for the build to complete (typically 2-3 minutes)
Step 8: Access Your App
Once the deployment status shows Ready:
- Go to the application Overview tab
- Find your domain:
my-fastapi-app.outplane.app - Click to open your live application
- Access API docs at
my-fastapi-app.outplane.app/docs
Adding a Database
Step 1: Create a Database
- Go to Databases in the sidebar
- Click Create Database
- Choose a name, PostgreSQL version, and region
- Click Create Database
Step 2: Get Connection String
- Click on your database
- In the Overview tab, find the connection string
- Copy the connection URL
Step 3: Connect from FastAPI
Install the async PostgreSQL driver:
pip install asyncpg sqlalchemy
pip freeze > requirements.txtCreate a database connection:
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
import os
DATABASE_URL = os.environ.get("DATABASE_URL")
# Convert postgres:// to postgresql+asyncpg://
if DATABASE_URL and DATABASE_URL.startswith("postgres://"):
DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql+asyncpg://", 1)
engine = create_async_engine(DATABASE_URL)
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)Step 4: Add Environment Variable
- Go to your application in Out Plane
- Click Settings → Environment
- Add
DATABASE_URLwith your connection string - Click Save and redeploy
Production Dockerfile
For production, use Gunicorn with Uvicorn workers:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt gunicorn
COPY . .
EXPOSE 8000
CMD ["gunicorn", "main:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "-b", "0.0.0.0:8000"]Automatic Deployments
Push to your branch to trigger automatic deployments:
git add .
git commit -m "Add new endpoint"
git push origin mainNext Steps
Buildpack alternative: Instead of Dockerfile, select Buildpack and add a Procfile with: web: uvicorn main:app --host 0.0.0.0 --port $PORT