مستندات API التفاعلية باستخدام Swagger UI
استكشف التوثيق التفاعلي التلقائي الذي تنشئه FastAPI، وخصّصه باستخدام البيانات الوصفية والوسوم، واستخدمه لاختبار نقاط النهاية مباشرة في المتصفح.
مستندات API التفاعلية باستخدام Swagger UI درس مجاني في FastAPI Backend Development Bootcamp على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في FastAPI Backend Development Bootcamp، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة FastAPI Backend Development Bootcamp 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Docs for Free
FastAPI's standout perk: automatic interactive docs. From your type hints and models it builds an OpenAPI schema and serves Swagger UI at /docs and ReDoc at /redoc.
Where Docs Come From
Both doc UIs render one OpenAPI JSON spec that FastAPI builds from your routes, params, and models. View the raw spec at /openapi.json.
App-Level Metadata
Pass app metadata — title, description, version — to the FastAPI constructor to brand your docs.
from fastapi import FastAPI
app = FastAPI(
title='Bookstore API',
description='Manage books and orders',
version='1.0.0',
)Summaries and Descriptions
Give each route a summary plus a docstring. The docstring becomes the long description in the docs and even supports Markdown.
@app.get('/books', summary='List all books')
async def list_books():
"""Return **every** book in the catalog."""
return booksGrouping with Tags
Use tags to group related endpoints into collapsible sections, keeping large APIs tidy in the docs.
@app.get('/users', tags=['users'])
async def list_users():
return users
@app.get('/books', tags=['books'])
async def list_books():
return booksDocumenting Responses
Declare alternate responses with the responses parameter so the docs list every status code a client might get, like a 404.
@app.get('/books/{id}', responses={404: {'description': 'Book not found'}})
async def get_book(id: int):
return books[id]Example Values
Add examples via Pydantic Field so Swagger UI pre-fills realistic values, making Try it out far easier.
from pydantic import BaseModel, Field
class Book(BaseModel):
title: str = Field(examples=['Dune'])
pages: int = Field(examples=[412])Building OpenAPI Mentally
The OpenAPI schema is just structured data. Here is a tiny Python sketch of grouping routes by tag to make that concrete.
routes = [
{'path': '/users', 'tag': 'users'},
{'path': '/books', 'tag': 'books'},
{'path': '/orders', 'tag': 'books'},
]
grouped = {}
for r in routes:
grouped.setdefault(r['tag'], []).append(r['path'])
print(grouped)Try It Out
In Swagger UI, hit Try it out, fill params, and Execute. You get a real request, the curl command, and the live response — no client code needed.
Customizing or Disabling Docs
Change the doc URLs or disable docs entirely (handy for production) right from constructor arguments.
app = FastAPI(docs_url='/api-docs', redoc_url=None)
# Disable both:
# app = FastAPI(docs_url=None, redoc_url=None)Why It Matters
Because docs are generated from your code, they stay accurate and never drift like handwritten ones — cutting friction for frontend teams and API consumers.
Quick Check
Where does FastAPI get the information it uses to build the interactive docs?
Recap
You explored FastAPI's automatic docs: Swagger and ReDoc from the OpenAPI spec, metadata, tags and examples, and how to customize or disable them.
الأسئلة الشائعة
هل درس «مستندات API التفاعلية باستخدام Swagger UI» مجاني؟
نعم — نص درس «مستندات API التفاعلية باستخدام Swagger UI» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة FastAPI Backend Development Bootcamp، انتقل إلى CoddyKit PRO. تتضمن دورة FastAPI Backend Development Bootcamp 4 دروس في المجموع.
ماذا ستتعلم في «مستندات API التفاعلية باستخدام Swagger UI»؟
استكشف التوثيق التفاعلي التلقائي الذي تنشئه FastAPI، وخصّصه باستخدام البيانات الوصفية والوسوم، واستخدمه لاختبار نقاط النهاية مباشرة في المتصفح. تتمرن على FastAPI Backend Development Bootcamp مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ FastAPI Backend Development Bootcamp؟
لا تُشترط خبرة سابقة. FastAPI Backend Development Bootcamp على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «مستندات API التفاعلية باستخدام Swagger UI»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس FastAPI Backend Development Bootcamp هذا؟
نعم. كل درس في FastAPI Backend Development Bootcamp يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مقدمة إلى FastAPI وإعداده
- نقطة API الأولى
- معلمات المسار والاستعلام
- مستندات API التفاعلية باستخدام Swagger UI