Resources and Endpoints
# CHAPTER 5
Resources and Endpoints
1. Introduction
In REST, everything is a resource. A user is a resource, a blog post is a resource, and a comment is a resource. If HTTP is the language and REST is the architectural style, then Endpoints are the addresses where these resources live. In Chapter 5, we will focus on how to properly name your resources and structure your endpoint URLs. Excellent URL design is the hallmark of a professional API.2. Learning Objectives
By the end of this chapter, you will be able to:- Define what a "Resource" is in the context of REST.
- Differentiate between a URL, a URI, and an Endpoint.
- Understand standard RESTful naming conventions.
- Design logical, hierarchical endpoints for complex relationships.
- Avoid common anti-patterns in endpoint design.
3. Beginner-Friendly Explanation
Think of your API like a well-organized library.- The Library is the server.
- The Books, Authors, and Members are the Resources.
- The Aisle and Shelf numbers (e.g., Aisle 5, Shelf B) are the Endpoints.
If you want to find a book, you don't wander around aimlessly; you go to the specific location where books are kept. In REST, if you want a list of users, you go directly to the /users endpoint. A clean API allows developers to guess where things are without even looking at the manual, just like a well-labeled library.
4. Real-World Examples
-
GitHub API: To get a user's repositories, the endpoint is
https://api.github.com/users/octocat/repos. Notice the hierarchy: Users -> Specific User (octocat) -> Repositories.
-
Stripe API: To view a specific customer, the endpoint is
https://api.stripe.com/v1/customers/cus_123.
5. Detailed Code Examples
In a PHP application, routing handles matching a URL to a specific piece of code. Here is a conceptual example of how endpoints map to logic.6. Request/Response Examples
A properly designed endpoint should return exactly what it describes.Request:
Response:
7. HTTP Examples
The endpoint URL works in tandem with the HTTP Method to determine the action.-
GET /users(Fetch all users)
-
POST /users(Create a new user)
-
GET /users/5(Fetch user #5)
-
PUT /users/5(Update user #5)
-
DELETE /users/5(Delete user #5)
8. JSON Examples
When creating a resource via an endpoint, the JSON payload should match the expected resource structure.POST to /products:
9. Best Practices
-
Use Nouns, Not Verbs: Use
/users, not/getUsersor/createUsers. The HTTP method (GET, POST) provides the verb.
-
Use Plurals: Standardize on plural nouns. Use
/users,/products, and/ordersinstead of/useror/product.
-
Keep Hierarchy Logical: For nested resources, go down one level. E.g.,
/users/5/orders(Orders belonging to user 5). Avoid going too deep (like/users/5/orders/12/items/4); it becomes too complex.
10. Common Mistakes
-
Action-based URLs: Creating endpoints like
POST /users/5/delete. This breaks REST conventions. It should beDELETE /users/5.
-
Inconsistent Pluralization: Mixing
/userand/productsacross your API. Pick plurals and stick with them uniformly.
11. Mini Exercises
-
1.
Look at this URL:
POST /updateCustomerProfile?id=9. Rewrite it to be perfectly RESTful.
PUT /customers/9)*
- 2. Design the endpoint URL to fetch the list of reviews for a specific product with ID 42.
GET /products/42/reviews)*
12. Coding Challenges
Challenge 1: You are building an API for a zoo. Define the RESTful endpoints (HTTP Method + URL) for the following actions:- 1. View all animals.
- 2. Add a new animal.
- 3. View the diet plan for the animal with ID 8.
- 4. Remove the animal with ID 3 from the system.
13. MCQs with Answers
Which of the following is the most RESTful endpoint to get a list of books?
How should you design an endpoint to update employee number 104?
What is a "Resource" in REST?
14. Interview Questions
- Q: Explain the difference between using nouns and verbs in REST endpoint design.
-
Q: If an endpoint becomes too deeply nested (e.g.,
/authors/1/books/5/chapters/3/paragraphs), how would you refactor it to be cleaner?
-
Q: Why do we prefer plural nouns (
/users) over singular nouns (/user) in API design?
15. FAQs
Q: What if an action doesn't fit neatly into a CRUD operation (Create, Read, Update, Delete)? A: Sometimes you have an action like "calculate" or "search". While you should try to map things to resources, occasionally a verb endpoint is necessary in pragmatic APIs, such asPOST /search or POST /calculator/compute. However, keep these to an absolute minimum.
Q: Should endpoints be lowercase?
A: Yes. Use lowercase letters and hyphens (kebab-case) for multi-word endpoints. For example, /user-profiles is preferred over /userProfiles or /User_Profiles.