Deploy fastapi-mysql-backend
FastAPI + MySQL backend API with JWT auth, migrations, and seeding.
MySQL
Just deployed
/var/lib/mysql
Just deployed
FastAPI MySQL Backend — Railway Template
A production-ready FastAPI backend API with MySQL, derived from the official Full Stack FastAPI Template (frontend removed, PostgreSQL replaced by MySQL) and packaged for one-click deployment on Railway.
What you get
- ⚡ FastAPI with automatic interactive docs (
/docs) and an OpenAPI schema (/api/v1/openapi.json). - 🔐 JWT authentication (OAuth2 password flow), secure Argon2 password hashing, email-based password recovery.
- 🗄️ MySQL 8 provisioned as a Railway database service, accessed through SQLModel + PyMySQL.
- 🧱 SQLModel ORM models for
UserandItem(UUID primary keys), with typed CRUD utilities. - 🧳 Alembic migrations plus automatic first-superuser seeding, run on every deploy by the container start command.
- 📧 Email sending via SMTP with Jinja2 HTML templates (Mailpit for local development).
- 🩺 A redirect-free
GET /healthendpoint used by Railway's healthcheck. - 🧪 A full Pytest suite covering auth, users, and items CRUD.
Deploying
- Click the deploy button above.
- Railway provisions two services from the template: MySQL and the backend app, and creates a public domain for the backend.
- The deploy form prompts for the required variables. MySQL section: set
MYSQLPORT3306,MYSQLUSERroot, andMYSQL_DATABASErailway(the standard plugin values). Backend section: setPROJECT_NAME,SECRET_KEY(e.g.openssl rand -hex 32),FIRST_SUPERUSER,FIRST_SUPERUSER_PASSWORD, andFRONTEND_HOST(the allowed CORS origin — you can update it to the generated domain after deploy). - The backend's
DATABASE_URLis pre-wired to the MySQL service (${{MySQL.MYSQL_URL}}) — nothing to configure there. - On boot the container runs
alembic upgrade headand seeds the first superuser — no manual migration step. - After the first deploy, set
FRONTEND_HOSTon the backend to the generated domain (e.g.https://backend-production-xxxx.up.railway.app) if you will call the API from a browser.
Never set FASTAPI_ENV on Railway: the config only allows development or unset, and any other value crash-loops the container.
Deploy and Host
Deploy a FastAPI REST API with a managed MySQL database on Railway in one click. The template provisions two services: a MySQL database and a Dockerized FastAPI backend that runs its own Alembic migrations and superuser seeding on every boot, then serves the API on Railway's dynamically assigned PORT with a /health healthcheck.
About Hosting
Hosting this template gives you a self-contained JSON/REST API: JWT auth with Argon2 password hashing, users and items CRUD backed by MySQL through SQLModel, Alembic schema migrations, and SMTP email with Jinja2 templates for password recovery. The backend listens on $PORT, is healthchecked at /health, and restarts on failure (ON_FAILURE, up to 10 retries). Migrations and the initial superuser seed (from the FIRST_SUPERUSER / FIRST_SUPERUSER_PASSWORD variables) run automatically inside the container start command, so every fresh deploy comes up ready to serve authenticated requests. After the first deploy, generate a public domain and set it as FRONTEND_HOST (the CORS origin) if you will call the API from a browser.
Why Deploy
The upstream template assumes a full-stack repo with React, a Bun workspace, PostgreSQL, and docker-compose + Traefik for self-hosting — none of which fits a platform like Railway. This template removes all of that for you: it ships a backend-only API, swaps the database layer to MySQL (MySQLDsn parsing, mysql+pymysql driver, CHAR(32) UUID columns, a squashed MySQL-safe migration), bakes migrations into the container start command because Railway ignores preDeployCommand, binds to Railway's PORT, and exposes a redirect-free /health endpoint so healthchecks pass first try. Deploying this instead of hand-porting the official template saves you every one of those integration failures.
Common Use Cases
- A standalone REST API backend for a separately hosted web or mobile frontend.
- Internal tools and admin APIs needing ready-made JWT auth, superuser roles, and user management endpoints.
- Prototyping a SaaS API with database migrations and seeding already wired up.
- A reference implementation of FastAPI + SQLModel + MySQL on Railway, with a test suite to build on.
Dependencies for
Deployment Dependencies
- MySQL — provisioned by the template as a Railway database plugin service; the backend connects using
DATABASE_URLreferenced from the plugin'sMYSQL_URL(${{MySQL.MYSQL_URL}}), so no database credentials are hardcoded. - Backend variables —
PROJECT_NAME,SECRET_KEY(generate withopenssl rand -hex 32),FIRST_SUPERUSER,FIRST_SUPERUSER_PASSWORD, and after the first deployFRONTEND_HOST(set it to the backend's public domain). Optional:SENTRY_DSN,SMTP_HOST/SMTP_USER/SMTP_PASSWORD/EMAILS_FROM_EMAILfor password-recovery emails. Do not setFASTAPI_ENV. - Dockerfile + railway.json — the repo root contains the Dockerfile Railway builds from and
railway.jsonwith the builder, start command, and healthcheck configuration, so GitHub-triggered and CLI deploys behave identically.
Template Content
MySQL
mysql:9.4MYSQLPORT
MYSQLUSER
MYSQL_DATABASE
SECRET_KEY
PROJECT_NAME
FRONTEND_HOST
FIRST_SUPERUSER
FIRST_SUPERUSER_PASSWORD
