网页
综合
更新时间 2026-11-14 05:19:33
First Steps - FastAPI
自动蜘蛛
fastapi.tiangolo.com
正文约 3451 字
Use the official FastAPI skill for your installed version. Install it from your project directory:
See Develop with AI.
The simplest FastAPI file could look like this:
Copy that to a file main.py.
FastAPI has an official extension for VS Code (and Cursor), which provides a lot of features, including a path operation explorer, path operation search, CodeLens navigation in tests (jump to definition from tests), and FastAPI Cloud deployment and logs, all from your editor.
Run the live server:
In the output, there's a line with something like:
That line shows the URL where your app is being served on your local machine.
Open your browser at http://127.0.0.1:8000.
You will see the JSON response as:
Now go to http://127.0.0.1:8000/docs.
You will see the automatic interactive API documentation (provided by Swagger UI):
And now, go to http://127.0.0.1:8000/redoc.
You will see the alternative automatic documentation (provided by ReDoc):
FastAPI generates a "schema" with all your API using the OpenAPI standard for defining APIs.
A "schema" is a definition or description of something. Not the code that implements it, but just an abstract description.
In this case, OpenAPI is a specification that dictates how to define a schema of your API.
This schema definition includes your API paths, the possible parameters they take, etc.
The term "schema" might also refer to the shape of some data, like a JSON content.
In that case, it would mean the JSON attributes, and data types they have, etc.
OpenAPI defines an API schema for your API. And that schema includes definitions (or "schemas") of the data sent and received by your API using JSON Schema, the standard for JSON data schemas.
If you are curious about what the raw OpenAPI schema looks like, FastAPI automatically generates a JSON (schema) with the descriptions of all your API.
You can see it directly at: http://127.0.0.1:8000/openapi.json.
It will show a JSON starting with something like:
The OpenAPI schema is what powers the two interactive documentation systems included.
And there are dozens of alternatives, all based on OpenAPI. You could easily add any of those alternatives to your application built with FastAPI.
You could also use it to generate code automatically, for clients that communicate with your API. For example, frontend, mobile or IoT applications.
You can configure where your app is located in a pyproject.toml file like:
That entrypoint will tell the fastapi command that it should import the app like:
If your code was structured like:
Then you would set the entrypoint as:
which would be equivalent to:
You can also pass the file path to the fastapi dev command, and it will guess the FastAPI app object to use:
Or, you can also pass the --entrypoint option to the fastapi dev command:
But you would have to remember to pass the correct path\entrypoint every time you call the fastapi command.
Additionally, other tools might not be able to find it, for example the VS Code Extension or FastAPI Cloud, so it is recommended to use the entrypoint in pyproject.toml.
You can optionally deploy your FastAPI app to FastAPI Cloud with a single command. 🚀
The CLI will automatically detect your FastAPI application and deploy it to the cloud. If you are not logged in, your browser will open to complete the authentication process.
That's it! Now you can access your app at that URL. ✨
FastAPI is a Python class that provides all the functionality for your API.
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。