Skip to content

🗄 ⏲

👆 💪 ✍ 🛠️ ⏮️ ➡ 🛠️ 👈 💪 ⏲ 📨 🔢 🛠️ ✍ 👱 🙆 (🎲 🎏 👩‍💻 👈 🔜 ⚙️ 👆 🛠️).

🛠️ 👈 🔨 🕐❔ 👆 🛠️ 📱 🤙 🔢 🛠️ 📛 "⏲". ↩️ 🖥 👈 🔢 👩‍💻 ✍ 📨 📨 👆 🛠️ & ⤴️ 👆 🛠️ 🤙 🔙, 📨 📨 🔢 🛠️ (👈 🎲 ✍ 🎏 👩‍💻).

👉 💼, 👆 💪 💚 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖. ⚫️❔ ➡ 🛠️ ⚫️ 🔜 ✔️, ⚫️❔ 💪 ⚫️ 🔜 ⌛, ⚫️❔ 📨 ⚫️ 🔜 📨, ♒️.

📱 ⏮️ ⏲

➡️ 👀 🌐 👉 ⏮️ 🖼.

🌈 👆 🛠️ 📱 👈 ✔ 🏗 🧾.

👉 🧾 🔜 ✔️ id, title (📦), customer, & total.

👩‍💻 👆 🛠️ (🔢 👩‍💻) 🔜 ✍ 🧾 👆 🛠️ ⏮️ 🏤 📨.

⤴️ 👆 🛠️ 🔜 (➡️ 🌈):

  • 📨 🧾 🕴 🔢 👩‍💻.
  • 📈 💸.
  • 📨 📨 🔙 🛠️ 👩‍💻 (🔢 👩‍💻).
    • 👉 🔜 🔨 📨 🏤 📨 (⚪️➡️ 👆 🛠️) 🔢 🛠️ 🚚 👈 🔢 👩‍💻 (👉 "⏲").

😐 FastAPI 📱

➡️ 🥇 👀 ❔ 😐 🛠️ 📱 🔜 👀 💖 ⏭ ❎ ⏲.

⚫️ 🔜 ✔️ ➡ 🛠️ 👈 🔜 📨 Invoice 💪, & 🔢 🔢 callback_url 👈 🔜 🔌 📛 ⏲.

👉 🍕 📶 😐, 🌅 📟 🎲 ⏪ 😰 👆:

from typing import Union

from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Invoice(BaseModel):
    id: str
    title: Union[str, None] = None
    customer: str
    total: float


class InvoiceEvent(BaseModel):
    description: str
    paid: bool


class InvoiceEventReceived(BaseModel):
    ok: bool


invoices_callback_router = APIRouter()


@invoices_callback_router.post(
    "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
    pass


@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
    """
    Create an invoice.

    This will (let's imagine) let the API user (some external developer) create an
    invoice.

    And this path operation will:

    * Send the invoice to the client.
    * Collect the money from the client.
    * Send a notification back to the API user (the external developer), as a callback.
        * At this point is that the API will somehow send a POST request to the
            external API with the notification of the invoice event
            (e.g. "payment successful").
    """
    # Send the invoice, collect the money, send the notification (the callback)
    return {"msg": "Invoice received"}

Tip

callback_url 🔢 🔢 ⚙️ Pydantic 📛 🆎.

🕴 🆕 👜 callbacks=messages_callback_router.routes➡ 🛠️ 👨‍🎨. 👥 🔜 👀 ⚫️❔ 👈 ⏭.

🔬 ⏲

☑ ⏲ 📟 🔜 🪀 🙇 🔛 👆 👍 🛠️ 📱.

& ⚫️ 🔜 🎲 🪀 📚 ⚪️➡️ 1️⃣ 📱 ⏭.

⚫️ 💪 1️⃣ ⚖️ 2️⃣ ⏸ 📟, 💖:

callback_url = "https://example.com/api/v1/invoices/events/"
httpx.post(callback_url, json={"description": "Invoice paid", "paid": True})

✋️ 🎲 🏆 ⚠ 🍕 ⏲ ⚒ 💭 👈 👆 🛠️ 👩‍💻 (🔢 👩‍💻) 🛠️ 🔢 🛠️ ☑, 🛄 💽 👈 👆 🛠️ 🔜 📨 📨 💪 ⏲, ♒️.

, ⚫️❔ 👥 🔜 ⏭ 🚮 📟 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖 📨 ⏲ ⚪️➡️ 👆 🛠️.

👈 🧾 🔜 🎦 🆙 🦁 🎚 /docs 👆 🛠️, & ⚫️ 🔜 ➡️ 🔢 👩‍💻 💭 ❔ 🏗 🔢 🛠️.

👉 🖼 🚫 🛠️ ⏲ ⚫️ (👈 💪 ⏸ 📟), 🕴 🧾 🍕.

Tip

☑ ⏲ 🇺🇸🔍 📨.

🕐❔ 🛠️ ⏲ 👆, 👆 💪 ⚙️ 🕳 💖 🇸🇲 ⚖️ 📨.

✍ ⏲ 🧾 📟

👉 📟 🏆 🚫 🛠️ 👆 📱, 👥 🕴 💪 ⚫️ 📄 ❔ 👈 🔢 🛠️ 🔜 👀 💖.

✋️, 👆 ⏪ 💭 ❔ 💪 ✍ 🏧 🧾 🛠️ ⏮️ FastAPI.

👥 🔜 ⚙️ 👈 🎏 💡 📄 ❔ 🔢 🛠️ 🔜 👀 💖... 🏗 ➡ 🛠️(Ⓜ) 👈 🔢 🛠️ 🔜 🛠️ (🕐 👆 🛠️ 🔜 🤙).

Tip

🕐❔ ✍ 📟 📄 ⏲, ⚫️ 💪 ⚠ 🌈 👈 👆 👈 🔢 👩‍💻. & 👈 👆 ⏳ 🛠️ 🔢 🛠️, 🚫 👆 🛠️.

🍕 🛠️ 👉 ☝ 🎑 ( 🔢 👩‍💻) 💪 ℹ 👆 💭 💖 ⚫️ 🌅 ⭐ 🌐❔ 🚮 🔢, Pydantic 🏷 💪, 📨, ♒️. 👈 🔢 🛠️.

✍ ⏲ APIRouter

🥇 ✍ 🆕 APIRouter 👈 🔜 🔌 1️⃣ ⚖️ 🌅 ⏲.

from typing import Union

from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Invoice(BaseModel):
    id: str
    title: Union[str, None] = None
    customer: str
    total: float


class InvoiceEvent(BaseModel):
    description: str
    paid: bool


class InvoiceEventReceived(BaseModel):
    ok: bool


invoices_callback_router = APIRouter()


@invoices_callback_router.post(
    "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
    pass


@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
    """
    Create an invoice.

    This will (let's imagine) let the API user (some external developer) create an
    invoice.

    And this path operation will:

    * Send the invoice to the client.
    * Collect the money from the client.
    * Send a notification back to the API user (the external developer), as a callback.
        * At this point is that the API will somehow send a POST request to the
            external API with the notification of the invoice event
            (e.g. "payment successful").
    """
    # Send the invoice, collect the money, send the notification (the callback)
    return {"msg": "Invoice received"}

✍ ⏲ ➡ 🛠️

✍ ⏲ ➡ 🛠️ ⚙️ 🎏 APIRouter 👆 ✍ 🔛.

⚫️ 🔜 👀 💖 😐 FastAPI ➡ 🛠️:

  • ⚫️ 🔜 🎲 ✔️ 📄 💪 ⚫️ 🔜 📨, ✅ body: InvoiceEvent.
  • & ⚫️ 💪 ✔️ 📄 📨 ⚫️ 🔜 📨, ✅ response_model=InvoiceEventReceived.
from typing import Union

from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Invoice(BaseModel):
    id: str
    title: Union[str, None] = None
    customer: str
    total: float


class InvoiceEvent(BaseModel):
    description: str
    paid: bool


class InvoiceEventReceived(BaseModel):
    ok: bool


invoices_callback_router = APIRouter()


@invoices_callback_router.post(
    "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
    pass


@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
    """
    Create an invoice.

    This will (let's imagine) let the API user (some external developer) create an
    invoice.

    And this path operation will:

    * Send the invoice to the client.
    * Collect the money from the client.
    * Send a notification back to the API user (the external developer), as a callback.
        * At this point is that the API will somehow send a POST request to the
            external API with the notification of the invoice event
            (e.g. "payment successful").
    """
    # Send the invoice, collect the money, send the notification (the callback)
    return {"msg": "Invoice received"}

📤 2️⃣ 👑 🔺 ⚪️➡️ 😐 ➡ 🛠️:

  • ⚫️ 🚫 💪 ✔️ 🙆 ☑ 📟, ↩️ 👆 📱 🔜 🙅 🤙 👉 📟. ⚫️ 🕴 ⚙️ 📄 🔢 🛠️. , 🔢 💪 ✔️ pass.
  • 💪 🔌 🗄 3️⃣ 🧬 (👀 🌖 🔛) 🌐❔ ⚫️ 💪 ⚙️ 🔢 ⏮️ 🔢 & 🍕 ⏮️ 📨 📨 👆 🛠️.

⏲ ➡ 🧬

💪 ✔️ 🗄 3️⃣ 🧬 👈 💪 🔌 🍕 ⏮️ 📨 📨 👆 🛠️.

👉 💼, ⚫️ str:

"{$callback_url}/invoices/{$request.body.id}"

, 🚥 👆 🛠️ 👩‍💻 (🔢 👩‍💻) 📨 📨 👆 🛠️ :

https://yourapi.com/invoices/?callback_url=https://www.external.org/events

⏮️ 🎻 💪:

{
    "id": "2expen51ve",
    "customer": "Mr. Richie Rich",
    "total": "9999"
}

⤴️ 👆 🛠️ 🔜 🛠️ 🧾, & ☝ ⏪, 📨 ⏲ 📨 callback_url ( 🔢 🛠️):

https://www.external.org/events/invoices/2expen51ve

⏮️ 🎻 💪 ⚗ 🕳 💖:

{
    "description": "Payment celebration",
    "paid": true
}

& ⚫️ 🔜 ⌛ 📨 ⚪️➡️ 👈 🔢 🛠️ ⏮️ 🎻 💪 💖:

{
    "ok": true
}

Tip

👀 ❔ ⏲ 📛 ⚙️ 🔌 📛 📨 🔢 🔢 callback_url (https://www.external.org/events) & 🧾 id ⚪️➡️ 🔘 🎻 💪 (2expen51ve).

🚮 ⏲ 📻

👉 ☝ 👆 ✔️ ⏲ ➡ 🛠️(Ⓜ) 💪 (1️⃣(Ⓜ) 👈 🔢 👩‍💻 🔜 🛠️ 🔢 🛠️) ⏲ 📻 👆 ✍ 🔛.

🔜 ⚙️ 🔢 callbacks 👆 🛠️ ➡ 🛠️ 👨‍🎨 🚶‍♀️ 🔢 .routes (👈 🤙 list 🛣/➡ 🛠️) ⚪️➡️ 👈 ⏲ 📻:

from typing import Union

from fastapi import APIRouter, FastAPI
from pydantic import BaseModel, HttpUrl

app = FastAPI()


class Invoice(BaseModel):
    id: str
    title: Union[str, None] = None
    customer: str
    total: float


class InvoiceEvent(BaseModel):
    description: str
    paid: bool


class InvoiceEventReceived(BaseModel):
    ok: bool


invoices_callback_router = APIRouter()


@invoices_callback_router.post(
    "{$callback_url}/invoices/{$request.body.id}", response_model=InvoiceEventReceived
)
def invoice_notification(body: InvoiceEvent):
    pass


@app.post("/invoices/", callbacks=invoices_callback_router.routes)
def create_invoice(invoice: Invoice, callback_url: Union[HttpUrl, None] = None):
    """
    Create an invoice.

    This will (let's imagine) let the API user (some external developer) create an
    invoice.

    And this path operation will:

    * Send the invoice to the client.
    * Collect the money from the client.
    * Send a notification back to the API user (the external developer), as a callback.
        * At this point is that the API will somehow send a POST request to the
            external API with the notification of the invoice event
            (e.g. "payment successful").
    """
    # Send the invoice, collect the money, send the notification (the callback)
    return {"msg": "Invoice received"}

Tip

👀 👈 👆 🚫 🚶‍♀️ 📻 ⚫️ (invoices_callback_router) callback=, ✋️ 🔢 .routes, invoices_callback_router.routes.

✅ 🩺

🔜 👆 💪 ▶️ 👆 📱 ⏮️ Uvicorn & 🚶 http://127.0.0.1:8000/docs.

👆 🔜 👀 👆 🩺 ✅ "⏲" 📄 👆 ➡ 🛠️ 👈 🎦 ❔ 🔢 🛠️ 🔜 👀 💖: