Hosting bridge
- Lesson 19
- 40 min
- Module 7: Deploy and verify
Lesson 19 of 21 · Build and Deploy Your First APIHosting bridge
1. Understand the web request
2. Build a small FastAPI service
3. Design a usable contract
4. Document the API with OpenAPI and Swagger UI
5. Explore and test with Postman
6. Test and harden the service
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 ready
What you need to know
- Development mode with
fastapi dev, from Environment and first endpoint: Step 3: start the server. - The settings
API_KEYandSHOW_DOCS, from Basic protection: The API key and Keep the documentation true: Decide what to expose in production. - Ports and the address
127.0.0.1, from Browser-to-server basics: Ports: which program.
What you need on your computer
- The protected, tested and logged API, from Basic protection: Guided practice: protect and check.
python -m pytestpasses. requirements.txt, with the two groups of packages from Keep the documentation true: Install the test tools.- The collection in
postman. You exported it again in step 8 of Basic protection: Guided practice: protect and check. - The Postman CLI, from Tests and repeatable runs: Run it from the terminal: the Postman CLI.
.gitignorein the project, from Environment and first endpoint: Files that stay on your computer.- For the “Your turn” task: a phone or a second device on the same Wi-Fi network.
- If your project is missing or broken, copy the snapshot
end-of-m06, the project at the end of Module 6. Use a snapshot has the steps.
Tested with Python 3.14.6, FastAPI 0.142.2 and the Postman CLI 1.69.0 on macOS.
What a hosting platform does
Section titled “What a hosting platform does”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.
Development mode and production mode
Section titled “Development mode and production mode”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:
[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:
fastapi dev ⚡️ Starting FastAPI in development mode
🐍 Using import string: ticket_api.main:appWorked 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:
API_KEY=local-dev-key SHOW_DOCS=false fastapi run$env:API_KEY = "local-dev-key"$env:SHOW_DOCS = "false"fastapi runAPI_KEY=local-dev-key SHOW_DOCS=false fastapi run ⚡️ 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:
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.
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.
Guided practice: ready for hosting
Section titled “Guided practice: ready for hosting”- Make
pyproject.tomlas in the worked example. - Stop the development server, and start it again with only
fastapi dev. It finds the app from the entry point. - Stop it, and start the API in production mode with
API_KEY=local-dev-key SHOW_DOCS=false fastapi run. Check that it listens on0.0.0.0:8000. - Call
/health(200) and/docs(404). - Run the Postman collection from a second terminal, with
--env-var "apiKey=local-dev-key". Every test passes. - 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.) - Stop the server. Make sure that
.gitignorelists.venv/,__pycache__/,.envand.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.gitignorelists. (.fastapicloud/is made in the next lesson, and only links your folder to your own app.)
Your turn: from another device
Section titled “Your turn: from another device”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:
- A dependency that is installed on your computer, but not listed in
pyproject.toml, so the import fails on the server. - The app listens on
127.0.0.1, or on a different port than the platform expects. - 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:
- Read the platform’s logs first. They show the start command’s output, including the traceback of an import error.
- 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
dependenciesinpyproject.toml(python -m pip install "fastapi[standard]==0.142.2"). - 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.
Does your answer have the main points of ours?
Sign in and enroll in the course to save your answers. Every check counts toward the certificate.
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 runis production mode: no reloading, and it listens on0.0.0.0.pyproject.tomllists 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