AI Hints
Home›System Design Tutorial›Designing APIs
System Design · Lesson 17 of 120

Good API Design

Names, versions, errors.

⏱ 2 min read▶ Video at 19:18🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

An API is a promise. Once other people build on it, changing it can break their apps. So it's worth designing it carefully, the first time.

Good API Design
From the video: Good API Design
  • An API is a promise
  • Changes break others
  • Design it carefully

Habits of good APIs

Good APIs share a few habits. Use plural nouns, like slash products, not verbs, like slash get products. Put a version in the path, like slash v one, so you can change things later without breaking old clients. Return clear errors, with a status code and a message. And be consistent everywhere.

Plural nouns/products, not /getProducts
Versioned/v1/products
Clear errorscode + message
Consistentsame style everywhere

A helpful error

Compare two errors. The first just says error, which helps nobody. The second gives a status, a machine readable code, and a message a human can act on. Run it. A developer seeing the second one knows exactly what to fix.

errors.pypython
import json

bad = {"error": "error"}

good = {
    "status": 400,
    "code": "INVALID_QUANTITY",
    "message": "quantity must be between 1 and 99",
}
print(json.dumps(bad))
print(json.dumps(good, indent=2))
Output
{"error": "error"}
{
  "status": 400,
  "code": "INVALID_QUANTITY",
  "message": "quantity must be between 1 and 99"
}

Summary

Good APIs use nouns, versions, clear errors, and the same style everywhere. Next, how to return a long list without sending everything at once.

Key takeaways
  • Nouns, not verbs
  • Version from day one
  • Errors people can act on
Next up: Pagination — one page at a time