Deploy a FastAPI App
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python based on standard Python type hints.
This guide covers how to deploy a FastAPI app on Railway in four ways:
One-Click Deploy From a Template
We highly recommend that you eject from the template after deployment to create a copy of the repo on your GitHub account.
Note: You can also choose from a variety of FastAPI app templates created by the community.
Deploy From a GitHub Repo
To deploy a FastAPI app on Railway directly from GitHub, follow the steps below:
- Fork the basic FastAPI GitHub repo.
- If you already have a GitHub repo you want to deploy, you can skip this step.
- Create a New Project.
- Click Deploy from GitHub repo.
- Select the
fastapi
or your own GitHub repo.- Railway requires a valid GitHub account to be linked. If your Railway account isn't associated with one, you will be prompted to link it.
- Click Deploy Now.
Once the deployment is successful, a Railway service will be created for you. By default, this service will not be publicly accessible.
To set up a publicly accessible URL for the service, navigate to the Networking section in the Settings tab of your new service and click on Generate Domain.
The FastAPI app is run via a Hypercorn server as defined by the startCommand
in the railway.json file in the GitHub repository.
Railway makes it easy to define deployment configurations for your services directly in your project using a railway.toml or railway.json file, alongside your code.
Deploy From the CLI
- Install and authenticate with the CLI.
- Clone the forked fastapi GitHub repo and
cd
into the directory.- You can skip this step if you already have an app directory or repo on your machine that you want to deploy.
- Run
railway init
within the app directory to create a new project. - Run
railway up
to deploy.- The CLI will now scan, compress and upload our fastapi app files to Railway's backend for deployment.
Use a Dockerfile
Note: If you already have an app directory or repo on your machine that you want to deploy, you can skip the first two steps.
- Clone the forked
fastapi
repo andcd
into the directory. - Delete the
railway.json
file. - Create a
Dockerfile
in thefastapi
or app's root directory. - Add the content below to the
Dockerfile
:# Use the Python 3 alpine official image # https://hub.docker.com/_/python FROM python:3-alpine # Create and change to the app directory. WORKDIR /app # Copy local code to the container image. COPY . . # Install project dependencies RUN pip install --no-cache-dir -r requirements.txt # Run the web service on container startup. CMD ["hypercorn", "main:app", "--bind", "::"]
- Either deploy via the CLI or from GitHub.
Railway automatically detects the Dockerfile
, and uses it to build and deploy the app.
Note: Railway supports also deployment from public and private Docker images.
Next Steps
Explore these resources to learn how you can maximize your experience with Railway:
Edit this file on GitHub