什麼是 API?
Server 能做的事的菜單
⏱ 預估 ~4 分鐘
01 · 讀一讀
API(Application Programming Interface,應用程式介面)是一組規則,定義 server 會接受哪些請求、會回傳什麼回應。
當工程師做一台 server,他不會讓人隨便丟任何請求進來。他會做一個 API — 一份清楚的清單,列出可以呼叫的 endpoint(URL)、要送什麼資料、會拿到什麼回來。
你用的每個 app 都靠 API。氣象 app 顯示預報時,它在呼叫天氣 API。你登入網站時,它在呼叫驗證 API。你滑社群動態時,它在呼叫 API 撈下一批貼文。
💡 想像一下API 就像餐廳的菜單。菜單告訴你可以點什麼、每道菜裡有什麼、多少錢。你不能走進廚房自己亂做 — 你從菜單裡選。API 就是這樣:告訴你的 app 它可以跟 server 要什麼。
重點整理
- API = Application Programming Interface
- 它定義了 server 接受的請求「菜單」
- 菜單上的每一項叫做一個 endpoint(特定的 URL + method)
- API 讓不同的軟體系統可以互相溝通,不用知道對方內部的細節
02 · 看程式碼
下面是一個部落格典型的 API 長什麼樣子。每個 endpoint 是 HTTP method + URL 路徑的組合。
Blog API Endpoints
GET /api/posts → Get all blog posts
GET /api/posts/15 → Get post #15
POST /api/posts → Create a new post
PUT /api/posts/15 → Update post #15
DELETE /api/posts/15 → Delete post #15
User API Endpoints
GET /api/users → Get all users
GET /api/users/42 → Get user #42's profile
POST /api/users → Create a new user (sign up)
PUT /api/users/42 → Update user #42's profile
DELETE /api/users/42 → Delete user #42's account
注意模式:URL 告訴你你在操作什麼(/posts 或 /users),數字告訴你是哪一筆,HTTP method 告訴你你想做什麼。這種一致的模式讓 API 變得好猜、好用。
03 · 選擇題
一支手機氣象 app 顯示今天的預報。它怎麼拿到那筆資料?
- 預報資料寫死在 app 裡面
- App 從手機的感應器讀資料
- App 呼叫一個天氣 API,拿最新資料
- App 去爬一個天氣網站
04 · 填空
API 定義了 server 會接受哪些 _____。
⚠ 完整互動體驗需要 JavaScript。請啟用 JavaScript 後重新整理。
※ 本站為獨立繁中教學專案,非 Anthropic 官方產品。Claude™ 為 Anthropic, PBC 商標。