Skip to content

Hosting bridge

  • Lesson 19
  • 40 min
  • Module 7: Deploy and verify

What you will be able to do

Know exactly what a hosting platform does with your project, so that a deployment is not a mystery. Then prove on your own computer that the API runs the way the platform will run it. It runs in production mode, with its settings from the environment, and the same Postman collection checks it.

Before you startWhat to know and to have readyAbout 40 minutes

What you need to know

What you need on your computer

Tested with Python 3.14.6, FastAPI 0.142.2 and the Postman CLI 1.69.0 on macOS.

Until now, your API ran on your computer, for you. To let other programs use it, it must run on a computer that is always on, with a public HTTPS address. You can rent a server and set up everything yourself: the operating system, Python, HTTPS certificates, restarts after a crash. Or you use managed app hosting: you give the platform your code and your settings, and it does the rest.

Every managed platform does the same five things, with different names:

Step What the platform does What it needs from you
Build Copies your code, installs Python and your dependencies A list of dependencies, and the Python version
Start Runs your application with a start command The command, or a setting that tells it which app to run
Listen Sends the internet’s requests to your app, on a port An app that listens on the right address and port
Configure Gives your app its environment variables The settings and secrets, entered on the platform, not in the code
Keep it running HTTPS, restarts after a crash, logs A health endpoint, and logs that are useful

You do not need Docker for this. Docker is a different way to describe the same steps, which a later course teaches.

fastapi dev is for your computer. A platform runs fastapi run instead:

fastapi dev fastapi run
Restarts when a file changes Yes No. Production code does not change while it runs.
Listens on 127.0.0.1: only your computer can connect 0.0.0.0: every network of the computer
For Writing code Serving clients

127.0.0.1 accepts connections only from the same computer. On a server, the requests come from other computers, so the app must listen on 0.0.0.0, which means “on every network address of this computer”. An app that listens on 127.0.0.1 on a server runs, but nobody can reach it.

Many platforms also choose the port for you, and give it to the app in an environment variable, often called PORT. FastAPI Cloud, which you use in the next lesson, starts the app itself, so you do not need to handle PORT there. On other platforms, the start command is often fastapi run --port $PORT.

Worked example: describe the project for a platform

Section titled “Worked example: describe the project for a platform”

A platform needs to know the dependencies, the Python version and the app to run. The standard place for all three is a file called pyproject.toml, in the project folder:

pyproject.toml
[project]
name = "ticket-api"
version = "1.0.0"
description = "Classifies support tickets into a category and a priority."
requires-python = ">=3.12"
dependencies = ["fastapi[standard]==0.142.2"]
[tool.fastapi]
entrypoint = "ticket_api.main:app"
[tool.pytest.ini_options]
testpaths = ["tests"]
Section Meaning
[project] The project’s name, version, the Python versions that it supports, and the packages that it needs to run.
dependencies Only what the server needs. pytest and httpx2 are for working on the project, so they stay in requirements.txt.
[tool.fastapi] entrypoint Tells the fastapi command which application to run: the object app in the module ticket_api.main.
[tool.pytest.ini_options] pytest looks for tests only in tests.

With the entry point in the file, the commands become shorter, on your computer too:

Terminal
fastapi dev
Output (first lines)
⚡️ Starting FastAPI in development mode
🐍 Using import string: ticket_api.main:app

Worked example: production mode on your computer

Section titled “Worked example: production mode on your computer”

Amira runs the API the way the server will run it, with production settings from the environment:

Terminal window
API_KEY=local-dev-key SHOW_DOCS=false fastapi run
Output
⚡️ Starting FastAPI in production mode
🐍 Using import string: ticket_api.main:app
🌐 Server started at http://0.0.0.0:8000
Documentation at http://0.0.0.0:8000/docs
Logs:
INFO: Started server process [55799]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

It listens on 0.0.0.0. The line about documentation is the fastapi command’s default text: with SHOW_DOCS=false, /docs returns 404. /health works. Then she runs the Postman collection against it, with the key in the command:

Second terminal
postman collection run postman/ticket-api.postman_collection.json -e postman/local.postman_environment.json --env-var "apiKey=local-dev-key"

All requests and tests pass. The same code, in the same mode, with the same settings mechanism as the server. This run is also the local deployment simulation. If you cannot use a hosting platform, it is the evidence that your API is ready to be deployed.

On your computer: the project folder with ticket_api and pyproject.toml, and a new API key that is generated and never saved in a file. fastapi deploy sends the folder to FastAPI Cloud, which builds it by installing from pyproject.toml, then runs it with fastapi run on an HTTPS URL. fastapi cloud env set --secret sends the key to the environment variables API_KEY and SHOW_DOCS, which the running app reads. The running app writes logs with one line per request and its ID. Checks: /health for liveness, /ready for readiness, and the Postman collection run with the Deployed environment.
The path to production: code and secrets go separately, and three checks prove that the result works.
Text description of the diagram

From your computer, two things go to FastAPI Cloud by separate paths. The project folder goes with fastapi deploy: the platform builds it, installing from pyproject.toml, and runs it with fastapi run on an HTTPS URL. The new API key goes with fastapi cloud env set --secret: it becomes the environment variable API_KEY, next to SHOW_DOCS, and the running app reads them. The app writes logs, one line per request with its ID. Three checks prove that it works: /health (the process runs), /ready (it can classify) and the Postman collection with the Deployed environment.

  1. Make pyproject.toml as in the worked example.
  2. Stop the development server, and start it again with only fastapi dev. It finds the app from the entry point.
  3. Stop it, and start the API in production mode with API_KEY=local-dev-key SHOW_DOCS=false fastapi run. Check that it listens on 0.0.0.0:8000.
  4. Call /health (200) and /docs (404).
  5. Run the Postman collection from a second terminal, with --env-var "apiKey=local-dev-key". Every test passes.
  6. Run python -m pytest. Every test passes. (The tests make their own app with their own settings, so the environment variables of the running server do not affect them.)
  7. Stop the server. Make sure that .gitignore lists .venv/, __pycache__/, .env and .fastapicloud/, as in Environment and first endpoint: Files that stay on your computer. If a line is missing, add it. The deploy command in the next lesson leaves out the files that .gitignore lists. (.fastapicloud/ is made in the next lesson, and only links your folder to your own app.)

Find your computer’s address on your home network (on macOS: ipconfig getifaddr en0; on Windows: ipconfig, the IPv4 address; on Linux: hostname -I). Start the API with fastapi run, and open http://<that address>:8000/health on your phone, on the same Wi-Fi. Then try the same with fastapi dev. What is different, and why?

Show the answer

With fastapi run, the phone gets {"status":"ok"}: the app listens on 0.0.0.0, so it accepts connections from the network. With fastapi dev, the phone cannot connect: the app listens only on 127.0.0.1, which is this computer only. That is exactly the difference between your computer and a server. (If neither works, a firewall on your computer may block port 8000. Do not turn it off for this test.)

Common mistake

It works on my computer, but not on the server

Symptom: the platform says that the deployment worked, but the URL gives an error. Or the platform says that the app does not answer, or keeps restarting it.

Cause: one of the five steps is different on the server. The most common:

  1. A dependency that is installed on your computer, but not listed in pyproject.toml, so the import fails on the server.
  2. The app listens on 127.0.0.1, or on a different port than the platform expects.
  3. A setting that exists only in your terminal. On the server, the default is used, or the app stops at start (for example an unknown CLASSIFIER_MODE).

Fix:

  1. Read the platform’s logs first. They show the start command’s output, including the traceback of an import error.
  2. Run the app in production mode on your computer, with only the settings that the server has. Use a new virtual environment that has only the packages from dependencies in pyproject.toml (python -m pip install "fastapi[standard]==0.142.2").
  3. Compare the server’s environment variables with the ones that your app reads in config.py.

Check your understanding

Why does pyproject.toml list only fastapi[standard] in dependencies, and not pytest and httpx2?

Show the answer

dependencies is what the application needs to run, and the platform installs exactly that. pytest and httpx2 are only needed to work on the code: run the tests, check the contract. Installing them on the server would make the build slower and the server larger, with no benefit. They stay in requirements.txt, which you use on your computer.

Recap

  • A managed platform builds, starts, routes, configures and keeps your app running. It needs dependencies, a start command or entry point, and settings.
  • fastapi run is production mode: no reloading, and it listens on 0.0.0.0.
  • pyproject.toml lists what the app needs to run, its Python versions, and its entry point.
  • Running in production mode on your computer, with production settings and the Postman collection, is the local deployment simulation.

Next, you deploy the API to the internet, with its secrets outside the code.

Next: Publish safely