API Fundamentals
/Beginner
Resources & Endpoints
Definition
In REST, a 'Resource' is any piece of data or object the API exposes (like Users, Posts, Comments). An 'Endpoint' is the specific URL URL where that resource can be accessed.
Explain Like I'm New
Think of a Resource as a noun (a Book). Think of an Endpoint as the physical address of the library bookshelf where that book lives (`api.library.com/books/123`).
Real World Example
In a Twitter clone API, the Resources are `users`, `tweets`, and `likes`. The Endpoints are `api.com/users`, `api.com/tweets`, and `api.com/tweets/5/likes`.
Common Use Cases
- ā¢API Routing
- ā¢Database modeling
Terminal Output
bash / terminal
/*
Resource Naming Conventions in REST
*/
// Rule 1: Use plural nouns
ā
GET /tickets
ā GET /ticket
// Rule 2: Do not use verbs in the URL (The HTTP method IS the verb)
ā
POST /users
ā POST /createUser
// Rule 3: Use nesting to show relationships (Sub-resources)
// "Get all reviews for product #55"
ā
GET /products/55/reviews
// Rule 4: Don't nest too deeply (Keep it flat if possible)
ā GET /users/1/orders/5/items/32/tracking
ā
GET /items/32/tracking
Interview Questions
basic
- In REST API design, should URLs be based on Nouns or Verbs?
intermediate
- How do you represent a relationship between two resources in an endpoint URL (e.g., the comments belonging to a specific article)?