จุดปลายทาง API แรกของคุณ
เรียนรู้การสร้างจุดปลายทาง GET อย่างง่าย กำหนดเส้นทาง และส่งคืนการตอบกลับ JSON พื้นฐาน
จุดปลายทาง API แรกของคุณ เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน FastAPI Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What's an API Endpoint?
An API endpoint is a specific URL your API exposes — an address that receives requests and returns a service. We start with GET, used to fetch data.
Your FastAPI Application
Every app starts from a FastAPI instance, conventionally named app. This object is the core where all your routes and logic live.
from fastapi import FastAPI
app = FastAPI()
# This 'app' object is where your API lives!Defining Routes with Decorators
FastAPI uses decorators to bind a URL path to a function. @app.get("/") says: on a GET to the root path, run the function below.
Your First 'Hello World' Endpoint
Here is the classic API Hello World — a single GET endpoint at /. Run it and hit the root in your browser or Postman.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Hello, CoddyKit!"}
# To run: Save as main.py, then 'uvicorn main:app --reload'Running Your FastAPI App
Run your app with Uvicorn: uvicorn main:app --reload finds app in main.py and auto-restarts whenever you edit code.
FastAPI's JSON Magic
FastAPI's JSON magic: return a Python dict or list and it serializes to a JSON response automatically — no manual conversion needed.
Returning Structured JSON Data
Return a richer Python dict and FastAPI ships it as structured JSON. Here, an endpoint hands back full product details.
from fastapi import FastAPI
app = FastAPI()
@app.get("/product")
def get_product_info():
return {
"id": "CK001",
"name": "CoddyKit Pro Course",
"price": 49.99,
"available": True
}
# Run with: uvicorn main:app --reload
# Access this endpoint at /productCreating Multiple Endpoints
An API has many endpoints, each for a different resource. Define as many as you need on one app — here, two distinct GET routes.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"message": "Welcome to our API!"}
@app.get("/items")
def get_items():
return [
{"id": 1, "name": "Laptop"},
{"id": 2, "name": "Mouse"},
{"id": 3, "name": "Keyboard"}
]
# Run with: uvicorn main:app --reload
# Access at / and /itemsEndpoint Naming Conventions
Good endpoint naming keeps an API readable: plural nouns for collections, short lowercase hyphenated paths, and no verbs — the HTTP method implies the action.
Quick Check
Which of the following code snippets correctly defines a FastAPI GET endpoint at the path /status that returns a JSON object {"status": "OK"}?
Lesson Summary
Solid! You built endpoints with decorators, returned dicts as JSON, and ran the app via Uvicorn. Next: making routes dynamic with URL data.
คำถามที่พบบ่อย
บทเรียน “จุดปลายทาง API แรกของคุณ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “จุดปลายทาง API แรกของคุณ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “จุดปลายทาง API แรกของคุณ”
เรียนรู้การสร้างจุดปลายทาง GET อย่างง่าย กำหนดเส้นทาง และส่งคืนการตอบกลับ JSON พื้นฐาน คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “จุดปลายทาง API แรกของคุณ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- รู้จัก FastAPI และการตั้งค่า
- จุดปลายทาง API แรกของคุณ
- พารามิเตอร์เส้นทางและคำค้น
- เอกสาร API แบบโต้ตอบด้วย Swagger UI