Example app
FastAPI apps can be structured in many ways. For demonstration purposes, this example provides a simple, deployable FastAPI app on Sevalla. The concepts shown here can be applied to any FastAPI application. What’s included in the example app:Database models
This app uses SQLModel as its ORM. All database models should inherit from theSQLModel class, which is imported in models.py. This ensures consistency and smooth integration with the app’s database layer.
SQLALCHEMY_DATABASE_URI setting, which will be defined in config.py later.
alembic directory and its files were generated by the alembic init app/alembic command. Inside that directory, the app/alembic/env.py and app/alembic/script.py.mako files need to be updated to work with your app.
For alembic.env.py, the SQLModel class needs to be imported from models.py, and the database URL needs to be referenced when migrations are run.
app/alembic/script.py.mako to include an import for sqlmodel.
App settings
Thepydantic-settings package manages all configuration values for the app. If a .env file is present, its values will be loaded automatically. Otherwise, the app will fall back to the environment variables defined on the system.
The only required setting for this example is SQLALCHEMY_DATABASE_URI, which specifies the connection string used to connect to your database.
.env file to store configuration values that will be loaded when the app starts. When deploying to Sevalla, these environment variables can be configured directly through Sevalla instead.
Main file
The app’s entry point ismain.py, where you need to instantiate the FastAPI app object.
Requirements
The following installs all the requirements for this example app:Add a hosted database
The filesystem used for your app will be recreated on each deploy, so an SQLite database isn’t suitable for production. Instead, you can use a hosted database on Sevalla for your app. Create a database in Sevalla and select either Postgres, MySQL, or MariaDB. You also need to install a database driver if you haven’t already done so. For example, with Postgres, you can usepsycopg2-binary.
Nixpacks
By default, Sevalla uses Nixpacks to build your application. After you add the application, you need to add custom commands that will both start your app and run migrations before every deploy. To update the start command, go to Processes > Web process > Update process and addfastapi run app/main.py as your custom start command.
For migrations, you can create a job to run the migrate command before the container is started. To do this, after you add your application, go to Processes > Create job > Job. For the start command, add alembic upgrade head. The start policy should be before deployment, and the smallest instance size should be sufficient for migrations.
Dockerfile
To build your application using a Dockerfile, ensure it ends with your FastAPI app being started by the app server. Below is a sample Dockerfile that sets up a Python environment, installs dependencies, and launches the server.Migrations
For migrations, you can create a job to run the migrate command before the container is started. To do this, after you add your application, go to Processes > Create job > Job. For the start command, addalembic upgrade head. The start policy should be before deployment, and the smallest instance size should be sufficient for migrations.
Build settings
By default, Sevalla builds applications using Nixpacks, so the build strategy must be updated before your Dockerfile can be used. To update the build strategy, after you add your application, go to Settings > Update build strategy and change the build strategy from Nixpacks to Dockerfile.Deploy on Sevalla
To deploy your app to Sevalla using Git, your code must be hosted in a Git repository. Sevalla supports any public Git repository or private repositories from GitHub, Bitbucket, and GitLab. You’ll need to connect your repo host account with Sevalla if you are using a private repo. Your repo should have a.gitignore that ignores SQLite files, .env files, virtual environments, __pycache__/, any other files that either have sensitive information or don’t need to be tracked in git.
Here is an example .gitignore file:
SQLALCHEMY_DATABASE_URI is needed.
Since this variable holds your database URL, go to Networking and click Add internal connection. Select the database you created earlier, and then select Add environment variables to the application. Rename DB_URL to SQLALCHEMY_DATABASE_URI and click Add internal connection.
If you are using PostgreSQL, make sure the connection string starts with
postgresql:// (not postgres://) so SQLModel can connect properly.