Definition of program
A program can refer to: the code written in Python files; the file that can be executed by the operating system (e.g., python, python.exe, or uvicorn); or a particular program while running on the operating system, using the CPU and storing things in memory, also called a process.
Definition of process
A process is a particular program while running on the operating system, managed by the operating system. It refers specifically to the thing being executed, not to the file or code. A process can only do things when being executed. A process can be terminated by the user or operating system, at which point it stops running. Multiple processes of the same program can run at the same time.
Deployment concepts to consider
Main concepts to keep in mind when deciding how to deploy an application are: Security - HTTPS, Running on startup, Restarts, Replication (the number of processes running), Memory, and Previous steps before starting.
What deployment means for web APIs
Deployment of a web API means performing the necessary steps to make it available to users, typically by putting it in a remote machine with a server program that provides good performance and stability so users can access the application efficiently without interruptions or problems. This contrasts with development stages where you are constantly changing code, breaking it, fixing it, and restarting the development server.
Deployment strategies for FastAPI applications
There are several ways to deploy a FastAPI application depending on your specific use case and the tools you use. You could deploy a server yourself using a combination of tools, use a cloud service that does part of the work for you, or explore other possible options. FastAPI Cloud is an official tool built by the FastAPI team to make deploying FastAPI apps to the cloud as streamlined as possible with the same developer experience of working with FastAPI.
fastapi run command basic usage
The fastapi run command is used to serve a FastAPI application. Run it with the main module file as an argument, for example: fastapi run main.py. This command starts a production server using Uvicorn by default and searches for a FastAPI app object named 'app' in the specified module.
fastapi run default server behavior
The fastapi run command starts the server at http://0.0.0.0:8000 by default. The documentation is available at http://0.0.0.0:8000/docs.
ASGI standard and FastAPI
FastAPI is an ASGI (Asynchronous Server Gateway Interface) web framework. To run a FastAPI application or any other ASGI application on a remote server, you need an ASGI server program such as Uvicorn.
ASGI server alternatives
Several ASGI server options are available for running FastAPI applications: Uvicorn (high performance ASGI server), Hypercorn (ASGI server compatible with HTTP/2 and Trio), Daphne (ASGI server built for Django Channels), and Granian (a Rust HTTP server for Python applications).
FastAPI includes Uvicorn by default
When you install FastAPI, it comes with Uvicorn as a production server, which you can start with the fastapi run command. Uvicorn can also be installed manually and run directly with uvicorn commands.
Install Uvicorn with standard extras
Install Uvicorn using: uv add "uvicorn[standard]". Adding the 'standard' extra installs recommended dependencies including uvloop, which is a high-performance drop-in replacement for asyncio that provides concurrency performance improvements.
Uvicorn command syntax with import string
Run Uvicorn directly with: uv run uvicorn main:app --host 0.0.0.0 --port 80. The import string format 'main:app' refers to the 'app' object created in the 'main.py' file. This is equivalent to 'from main import app'.
Uvicorn --reload flag is for development only
Uvicorn and other servers support a --reload option that is useful during development because it automatically restarts the server when code changes. However, the --reload option consumes much more resources and is more unstable, so you should not use it in production.
Basic server deployment runs single process
The basic examples with server programs like Uvicorn start a single process listening on all IPs (0.0.0.0) on a predefined port such as 80.
Deployment concepts to consider
Beyond basic server setup, deployment requires consideration of: Security (HTTPS), running on startup, restarts, replication (number of running processes), memory management, and prerequisite steps before starting the server.
Server terminology distinction
The term 'server' can refer to both the remote/cloud computer (physical or virtual machine) and the program running on it (like Uvicorn). The remote machine is also called a machine, VM (virtual machine), or node, typically running Linux.
FastAPI with standard extras includes uvicorn[standard]
When you install FastAPI with standard extras using uv add "fastapi[standard]", you automatically get uvicorn[standard] as a dependency.
Deployment concepts checklist
The main deployment concepts to address are: Security (HTTPS), Running on startup, Restarts, Replication (the number of processes running), Memory, and Previous steps before starting.
FastAPI versioning follows Semantic Versioning
FastAPI follows Semantic Versioning conventions. Versions below 1.0.0 could potentially add breaking changes. The PATCH version (the last number, e.g., 3 in 0.2.3) is for bug fixes and non-breaking changes. The MINOR version (the middle number, e.g., 2 in 0.2.3) is where breaking changes and new features are added.
Pin FastAPI version exactly or within a minor version range
You should pin the FastAPI version you are using to a specific version that works correctly with your application. You can specify an exact version like fastapi[standard]==0.112.0, or pin within a minor version range like fastapi[standard]>=0.112.0,<0.113.0 to allow PATCH version updates while preventing breaking changes from MINOR version updates. An alternative approach is fastapi>=0.45.0,<0.46.0 to lock to a specific MINOR version.
Starlette version should not be pinned
You should not pin the version of Starlette. Different versions of FastAPI will use a specific newer version of Starlette. You can let FastAPI use the correct Starlette version automatically.
Pydantic compatibility with FastAPI
Pydantic versions above 1.0.0 are always compatible with FastAPI. You can pin Pydantic to any version above 1.0.0 that works for your application, for example pydantic>=2.7.0,<3.0.0.
Upgrade FastAPI versions using tests
To upgrade FastAPI to a newer version, first write tests for your application. FastAPI makes testing easy using Starlette. After writing tests, upgrade to a more recent FastAPI version and run all tests to ensure your code is working correctly. Once all tests pass and any necessary changes are made, pin FastAPI to the new version.
FastAPI production readiness
FastAPI can be used in production applications right now with 100% test coverage. However, versions are currently 0.x.x format, which means each version could potentially have breaking changes. New features are added frequently and bugs are fixed regularly.