Skip to content

๐Ÿฅ‡ ๐Ÿ”

๐Ÿ™… FastAPI ๐Ÿ“ ๐Ÿ’ช ๐Ÿ‘€ ๐Ÿ’– ๐Ÿ‘‰:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

๐Ÿ“ ๐Ÿ‘ˆ ๐Ÿ“ main.py.

๐Ÿƒ ๐Ÿ–– ๐Ÿ’ฝ:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
<span style="color: green;">INFO</span>:     Started reloader process [28720]
<span style="color: green;">INFO</span>:     Started server process [28722]
<span style="color: green;">INFO</span>:     Waiting for application startup.
<span style="color: green;">INFO</span>:     Application startup complete.

Note

๐Ÿ“‹ uvicorn main:app ๐Ÿ”—:

  • main: ๐Ÿ“ main.py (๐Ÿ "๐Ÿ•น").
  • app: ๐ŸŽš โœ ๐Ÿ”˜ main.py โฎ๏ธ โธ app = FastAPI().
  • --reload: โš’ ๐Ÿ’ฝ โ โฎ๏ธ ๐Ÿ“Ÿ ๐Ÿ”€. ๐Ÿ•ด โš™๏ธ ๐Ÿ› ๏ธ.

๐Ÿ”ข, ๐Ÿ“ค โธ โฎ๏ธ ๐Ÿ•ณ ๐Ÿ’–:

INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

๐Ÿ‘ˆ โธ ๐ŸŽฆ ๐Ÿ“› ๐ŸŒโ” ๐Ÿ‘† ๐Ÿ“ฑ โž– ๐Ÿฆ, ๐Ÿ‘† ๐Ÿ‡ง๐Ÿ‡ฟ ๐ŸŽฐ.

โœ… โšซ๏ธ

๐Ÿ“‚ ๐Ÿ‘† ๐Ÿ–ฅ http://127.0.0.1:8000.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐ŸŽป ๐Ÿ“จ:

{"message": "Hello World"}

๐ŸŽ“ ๐Ÿ› ๏ธ ๐Ÿฉบ

๐Ÿ”œ ๐Ÿšถ http://127.0.0.1:8000/docs.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐Ÿง ๐ŸŽ“ ๐Ÿ› ๏ธ ๐Ÿงพ (๐Ÿšš ๐Ÿฆ ๐ŸŽš):

Swagger UI

๐ŸŽ› ๐Ÿ› ๏ธ ๐Ÿฉบ

& ๐Ÿ”œ, ๐Ÿšถ http://127.0.0.1:8000/redoc.

๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐ŸŽ› ๐Ÿง ๐Ÿงพ (๐Ÿšš ๐Ÿ“„):

ReDoc

๐Ÿ—„

FastAPI ๐Ÿ— "๐Ÿ”—" โฎ๏ธ ๐ŸŒ ๐Ÿ‘† ๐Ÿ› ๏ธ โš™๏ธ ๐Ÿ—„ ๐Ÿฉ โš– ๐Ÿ”—.

"๐Ÿ”—"

"๐Ÿ”—" ๐Ÿ”‘ โš–๏ธ ๐Ÿ“› ๐Ÿ•ณ. ๐Ÿšซ ๐Ÿ“Ÿ ๐Ÿ‘ˆ ๐Ÿ› ๏ธ โšซ๏ธ, โœ‹๏ธ ๐Ÿ“ ๐Ÿ“›.

๐Ÿ› ๏ธ "๐Ÿ”—"

๐Ÿ‘‰ ๐Ÿ’ผ, ๐Ÿ—„ ๐Ÿ”ง ๐Ÿ‘ˆ ๐Ÿค” โ” ๐Ÿ”ฌ ๐Ÿ”— ๐Ÿ‘† ๐Ÿ› ๏ธ.

๐Ÿ‘‰ ๐Ÿ”— ๐Ÿ”‘ ๐Ÿ”Œ ๐Ÿ‘† ๐Ÿ› ๏ธ โžก, ๐Ÿ’ช ๐Ÿ”ข ๐Ÿ‘ซ โœŠ, โ™’๏ธ.

๐Ÿ’ฝ "๐Ÿ”—"

โš– "๐Ÿ”—" ๐Ÿ’ช ๐Ÿ”— ๐Ÿ’  ๐Ÿ’ฝ, ๐Ÿ’– ๐ŸŽป ๐ŸŽš.

๐Ÿ‘ˆ ๐Ÿ’ผ, โšซ๏ธ ๐Ÿ”œ โ›“ ๐ŸŽป ๐Ÿ”ข, & ๐Ÿ“Š ๐Ÿ†Ž ๐Ÿ‘ซ โœ”๏ธ, โ™’๏ธ.

๐Ÿ—„ & ๐ŸŽป ๐Ÿ”—

๐Ÿ—„ ๐Ÿ”ฌ ๐Ÿ› ๏ธ ๐Ÿ”— ๐Ÿ‘† ๐Ÿ› ๏ธ. & ๐Ÿ‘ˆ ๐Ÿ”— ๐Ÿ”Œ ๐Ÿ”‘ (โš–๏ธ "๐Ÿ”—") ๐Ÿ“Š ๐Ÿ“จ & ๐Ÿ“จ ๐Ÿ‘† ๐Ÿ› ๏ธ โš™๏ธ ๐ŸŽป ๐Ÿ”—, ๐Ÿฉ ๐ŸŽป ๐Ÿ“Š ๐Ÿ”—.

โœ… openapi.json

๐Ÿšฅ ๐Ÿ‘† ๐Ÿ˜Ÿ ๐Ÿ”ƒ โ” ๐Ÿฃ ๐Ÿ—„ ๐Ÿ”— ๐Ÿ‘€ ๐Ÿ’–, FastAPI ๐Ÿ” ๐Ÿ— ๐ŸŽป (๐Ÿ”—) โฎ๏ธ ๐Ÿ“› ๐ŸŒ ๐Ÿ‘† ๐Ÿ› ๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ‘€ โšซ๏ธ ๐Ÿ”—: http://127.0.0.1:8000/openapi.json.

โšซ๏ธ ๐Ÿ”œ ๐ŸŽฆ ๐ŸŽป โ–ถ๏ธ โฎ๏ธ ๐Ÿ•ณ ๐Ÿ’–:

{
    "openapi": "3.0.2",
    "info": {
        "title": "FastAPI",
        "version": "0.1.0"
    },
    "paths": {
        "/items/": {
            "get": {
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {



...

โšซ๏ธโ” ๐Ÿ—„

๐Ÿ—„ ๐Ÿ”— โšซ๏ธโ” ๐Ÿ‹๏ธ 2๏ธโƒฃ ๐ŸŽ“ ๐Ÿงพ โš™๏ธ ๐Ÿ”Œ.

& ๐Ÿ“ค ๐Ÿ’ฏ ๐ŸŽ›, ๐ŸŒ โš“๏ธ ๐Ÿ”› ๐Ÿ—„. ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ’ช ๐Ÿšฎ ๐Ÿ™† ๐Ÿ“š ๐ŸŽ› ๐Ÿ‘† ๐Ÿˆธ ๐Ÿ— โฎ๏ธ FastAPI.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ โšซ๏ธ ๐Ÿ— ๐Ÿ“Ÿ ๐Ÿ”, ๐Ÿ‘ฉโ€๐Ÿ’ป ๐Ÿ‘ˆ ๐Ÿ”— โฎ๏ธ ๐Ÿ‘† ๐Ÿ› ๏ธ. ๐Ÿ–ผ, ๐Ÿ•ธ, ๐Ÿ“ฑ โš–๏ธ โ˜ ๐Ÿˆธ.

๐ŸŒƒ, ๐Ÿ” ๐Ÿ”

๐Ÿ” 1๏ธโƒฃ: ๐Ÿ—„ FastAPI

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

FastAPI ๐Ÿ ๐ŸŽ“ ๐Ÿ‘ˆ ๐Ÿšš ๐ŸŒ ๐Ÿ› ๏ธ ๐Ÿ‘† ๐Ÿ› ๏ธ.

๐Ÿ“ก โ„น

FastAPI ๐ŸŽ“ ๐Ÿ‘ˆ ๐Ÿ˜– ๐Ÿ”— โšช๏ธโžก๏ธ Starlette.

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŒ ๐Ÿ’ƒ ๐Ÿ› ๏ธ โฎ๏ธ FastAPI ๐Ÿ’โ€โ™‚๏ธ.

๐Ÿ” 2๏ธโƒฃ: โœ FastAPI "๐Ÿ‘"

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

๐Ÿ“ฅ app ๐Ÿ”ข ๐Ÿ”œ "๐Ÿ‘" ๐ŸŽ“ FastAPI.

๐Ÿ‘‰ ๐Ÿ”œ ๐Ÿ‘‘ โ˜ ๐Ÿ”— โœ ๐ŸŒ ๐Ÿ‘† ๐Ÿ› ๏ธ.

๐Ÿ‘‰ app ๐ŸŽ 1๏ธโƒฃ ๐Ÿ”— uvicorn ๐Ÿ“‹:

$ uvicorn main:app --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

๐Ÿšฅ ๐Ÿ‘† โœ ๐Ÿ‘† ๐Ÿ“ฑ ๐Ÿ’–:

from fastapi import FastAPI

my_awesome_api = FastAPI()


@my_awesome_api.get("/")
async def root():
    return {"message": "Hello World"}

& ๐Ÿšฎ โšซ๏ธ ๐Ÿ“ main.py, โคด๏ธ ๐Ÿ‘† ๐Ÿ”œ ๐Ÿค™ uvicorn ๐Ÿ’–:

$ uvicorn main:my_awesome_api --reload

<span style="color: green;">INFO</span>:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)

๐Ÿ” 3๏ธโƒฃ: โœ โžก ๐Ÿ› ๏ธ

โžก

"โžก" ๐Ÿ“ฅ ๐Ÿ”— ๐Ÿ ๐Ÿ• ๐Ÿ“› โ–ถ๏ธ โšช๏ธโžก๏ธ ๐Ÿฅ‡ /.

, ๐Ÿ“› ๐Ÿ’–:

https://example.com/items/foo

...โžก ๐Ÿ”œ:

/items/foo

Info

"โžก" ๐Ÿ›Ž ๐Ÿค™ "๐Ÿ”—" โš–๏ธ "๐Ÿ›ฃ".

โช ๐Ÿ— ๐Ÿ› ๏ธ, "โžก" ๐Ÿ‘‘ ๐ŸŒŒ ๐ŸŽ "โš " & "โ„น".

๐Ÿ› ๏ธ

"๐Ÿ› ๏ธ" ๐Ÿ“ฅ ๐Ÿ”— 1๏ธโƒฃ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” "๐Ÿ‘ฉโ€๐Ÿ”ฌ".

1๏ธโƒฃ:

  • POST
  • GET
  • PUT
  • DELETE

...& ๐ŸŒ… ๐Ÿ˜ ๐Ÿ•:

  • OPTIONS
  • HEAD
  • PATCH
  • TRACE

๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ› ๏ธ, ๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”— ๐Ÿ”  โžก โš™๏ธ 1๏ธโƒฃ (โš–๏ธ ๐ŸŒ…) ๐Ÿ‘ซ "๐Ÿ‘ฉโ€๐Ÿ”ฌ".


๐Ÿ•โ” ๐Ÿ— ๐Ÿ”—, ๐Ÿ‘† ๐Ÿ›Ž โš™๏ธ ๐Ÿ‘ซ ๐ŸŽฏ ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐ŸŽญ ๐ŸŽฏ ๐ŸŽฏ.

๐Ÿ›Ž ๐Ÿ‘† โš™๏ธ:

  • POST: โœ ๐Ÿ’ฝ.
  • GET: โœ ๐Ÿ’ฝ.
  • PUT: โ„น ๐Ÿ’ฝ.
  • DELETE: โŽ ๐Ÿ’ฝ.

, ๐Ÿ—„, ๐Ÿ”  ๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ ๐Ÿค™ "๐Ÿ› ๏ธ".

๐Ÿ‘ฅ ๐Ÿ”œ ๐Ÿค™ ๐Ÿ‘ซ "๐Ÿ› ๏ธ" ๐Ÿ’โ€โ™‚๏ธ.

๐Ÿ”ฌ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

@app.get("/") ๐Ÿ’ฌ FastAPI ๐Ÿ‘ˆ ๐Ÿ”ข โ–ถ๏ธ๏ธ ๐Ÿ”› ๐Ÿˆš ๐Ÿšš ๐Ÿ“จ ๐Ÿ‘ˆ ๐Ÿšถ:

  • โžก /
  • โš™๏ธ get ๐Ÿ› ๏ธ

@decorator โ„น

๐Ÿ‘ˆ @something โ• ๐Ÿ ๐Ÿค™ "๐Ÿ‘จโ€๐ŸŽจ".

๐Ÿ‘† ๐Ÿšฎ โšซ๏ธ ๐Ÿ”› ๐Ÿ” ๐Ÿ”ข. ๐Ÿ’– ๐Ÿ“ถ ๐Ÿ“” ๐Ÿ‘’ (๐Ÿ‘ค ๐Ÿ’ญ ๐Ÿ‘ˆ ๐ŸŒโ” โš– ๐Ÿ‘Ÿ โšช๏ธโžก๏ธ).

"๐Ÿ‘จโ€๐ŸŽจ" โœŠ ๐Ÿ”ข ๐Ÿ”› & ๐Ÿ”จ ๐Ÿ•ณ โฎ๏ธ โšซ๏ธ.

๐Ÿ‘† ๐Ÿ’ผ, ๐Ÿ‘‰ ๐Ÿ‘จโ€๐ŸŽจ ๐Ÿ’ฌ FastAPI ๐Ÿ‘ˆ ๐Ÿ”ข ๐Ÿ”› ๐Ÿ”— โžก / โฎ๏ธ ๐Ÿ› ๏ธ get.

โšซ๏ธ "โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ".

๐Ÿ‘† ๐Ÿ’ช โš™๏ธ ๐ŸŽ ๐Ÿ› ๏ธ:

  • @app.post()
  • @app.put()
  • @app.delete()

& ๐ŸŒ… ๐Ÿ˜ ๐Ÿ•:

  • @app.options()
  • @app.head()
  • @app.patch()
  • @app.trace()

Tip

๐Ÿ‘† ๐Ÿ†“ โš™๏ธ ๐Ÿ”  ๐Ÿ› ๏ธ (๐Ÿ‡บ๐Ÿ‡ธ๐Ÿ” ๐Ÿ‘ฉโ€๐Ÿ”ฌ) ๐Ÿ‘† ๐ŸŽ‹.

FastAPI ๐Ÿšซ ๐Ÿ› ๏ธ ๐Ÿ™† ๐ŸŽฏ ๐Ÿ”‘.

โ„น ๐Ÿ“ฅ ๐ŸŽ ๐Ÿ“„, ๐Ÿšซ ๐Ÿ“„.

๐Ÿ–ผ, ๐Ÿ•โ” โš™๏ธ ๐Ÿ•น ๐Ÿ‘† ๐Ÿ›Ž ๐ŸŽญ ๐ŸŒ ๐ŸŽฏ โš™๏ธ ๐Ÿ•ด POST ๐Ÿ› ๏ธ.

๐Ÿ” 4๏ธโƒฃ: ๐Ÿ”ฌ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข

๐Ÿ‘‰ ๐Ÿ‘† "โžก ๐Ÿ› ๏ธ ๐Ÿ”ข":

  • โžก: /.
  • ๐Ÿ› ๏ธ: get.
  • ๐Ÿ”ข: ๐Ÿ”ข ๐Ÿ”› "๐Ÿ‘จโ€๐ŸŽจ" (๐Ÿ”› @app.get("/")).
from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

๐Ÿ‘‰ ๐Ÿ ๐Ÿ”ข.

โšซ๏ธ ๐Ÿ”œ ๐Ÿค™ FastAPI ๐Ÿ•โ” โšซ๏ธ ๐Ÿ“จ ๐Ÿ“จ ๐Ÿ“› "/" โš™๏ธ GET ๐Ÿ› ๏ธ.

๐Ÿ‘‰ ๐Ÿ’ผ, โšซ๏ธ async ๐Ÿ”ข.


๐Ÿ‘† ๐Ÿ’ช ๐Ÿ”ฌ โšซ๏ธ ๐Ÿ˜ ๐Ÿ”ข โ†ฉ๏ธ async def:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def root():
    return {"message": "Hello World"}

Note

๐Ÿšฅ ๐Ÿ‘† ๐Ÿšซ ๐Ÿ’ญ ๐Ÿ”บ, โœ… ๐Ÿ”: "๐Ÿƒ โ“".

๐Ÿ” 5๏ธโƒฃ: ๐Ÿ“จ ๐ŸŽš

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello World"}

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ dict, list, โญ ๐Ÿ’ฒ str, int, โ™’๏ธ.

๐Ÿ‘† ๐Ÿ’ช ๐Ÿ“จ Pydantic ๐Ÿท (๐Ÿ‘† ๐Ÿ”œ ๐Ÿ‘€ ๐ŸŒ… ๐Ÿ”ƒ ๐Ÿ‘ˆ โช).

๐Ÿ“ค ๐Ÿ“š ๐ŸŽ ๐ŸŽš & ๐Ÿท ๐Ÿ‘ˆ ๐Ÿ”œ ๐Ÿ” ๐Ÿ—œ ๐ŸŽป (๐Ÿ”Œ ๐Ÿœ, โ™’๏ธ). ๐Ÿ”„ โš™๏ธ ๐Ÿ‘† ๐Ÿ’• ๐Ÿ•, โšซ๏ธ ๐Ÿ† ๐ŸŽฒ ๐Ÿ‘ˆ ๐Ÿ‘ซ โช ๐Ÿ•โ€๐Ÿฆบ.

๐ŸŒƒ

  • ๐Ÿ—„ FastAPI.
  • โœ app ๐Ÿ‘.
  • โœ โžก ๐Ÿ› ๏ธ ๐Ÿ‘จโ€๐ŸŽจ (๐Ÿ’– @app.get("/")).
  • โœ โžก ๐Ÿ› ๏ธ ๐Ÿ”ข (๐Ÿ’– def root(): ... ๐Ÿ”›).
  • ๐Ÿƒ ๐Ÿ› ๏ธ ๐Ÿ’ฝ (๐Ÿ’– uvicorn main:app --reload).