Skip to content

Instantly share code, notes, and snippets.

@BunHouth
Last active August 17, 2024 04:42
Show Gist options
  • Save BunHouth/2564453f60d0220dc52ee87a9eed5353 to your computer and use it in GitHub Desktop.
Save BunHouth/2564453f60d0220dc52ee87a9eed5353 to your computer and use it in GitHub Desktop.

API

The URL pattern for an API is generally structured to make it easy to understand the purpose and hierarchy of the resources being accessed. Here are some common best practices for designing API URL patterns:

1. Resource-Based URLs:

  • Use nouns to represent resources.
  • Example: /api/v1/users, /api/v1/orders, /api/v1/products.

2. HTTP Methods for Actions:

  • Use HTTP methods to specify actions (GET, POST, PUT, DELETE).
  • Example:
    • GET /api/v1/users - Retrieve a list of users.
    • POST /api/v1/users - Create a new user.
    • GET /api/v1/users/{id} - Retrieve a specific user.
    • PUT /api/v1/users/{id} - Update a specific user.
    • DELETE /api/v1/users/{id} - Delete a specific user.

3. Hierarchical Relationships:

  • Represent hierarchical relationships using nested resources.
  • Example: /api/v1/users/{user_id}/orders/{order_id} - Retrieve a specific order for a specific user.

4. Query Parameters for Filtering, Sorting, and Pagination:

  • Use query parameters for filtering, sorting, and pagination.
  • Example:
    • GET /api/v1/products?category=electronics&sort=price&order=asc
    • GET /api/v1/users?page=2&limit=20

5. Plural vs. Singular:

  • Typically, resource names are plural.
  • Example: /api/v1/users instead of /api/v1/user.

6. Versioning:

  • Include versioning in the URL to manage API changes.
  • Example: /api/v1/, /api/v2/.

7. Status Codes and Error Handling:

  • Return appropriate HTTP status codes (e.g., 200 for success, 404 for not found, 500 for server error).

Example:

GET /api/v1/drivers/{driver_id}/trips?date=2024-08-01

In the context of your personal-driver application, an example URL pattern might be:

  • GET /api/v1/drivers/{driver_id}/trips - Get trips for a specific driver.
  • POST /api/v1/rides - Create a new ride request.
  • GET /api/v1/users/{user_id}/ride-history - Retrieve ride history for a specific user.

These patterns help keep the API structured and intuitive for developers.

When dealing with multiple levels of nested resources in your API, it's important to design URL patterns that maintain clarity and avoid unnecessary complexity. Here are some best practices for handling multiple nested resources:

1. Limit Depth of Nesting:

  • Avoid deep nesting (more than 2-3 levels) as it can become cumbersome and hard to manage.
  • Example: /api/v1/users/{user_id}/orders/{order_id}/items/{item_id}.
  • Instead, consider flattening the structure where appropriate:
    • /api/v1/orders/{order_id}/items/{item_id}.

2. Consider Using Parent Identifiers:

  • When deeper nesting is needed, use parent resource identifiers in the query or path to maintain clarity.
  • Example: /api/v1/orders/{order_id}/items?user_id={user_id}.

3. Use Query Parameters for Complex Relationships:

  • Instead of deep nesting, use query parameters to manage relationships between resources.
  • Example:
    • /api/v1/items?order_id={order_id}&user_id={user_id} - to filter items by order and user.

4. Consistency Across API:

  • Ensure consistent use of patterns across the API.
  • Example:
    • If you use /api/v1/companies/{company_id}/departments/{department_id}/employees/{employee_id}, be consistent across similar endpoints.

5. Resource-Specific Actions:

  • For actions that don’t fit neatly into RESTful conventions, consider using custom action endpoints.
  • Example: /api/v1/orders/{order_id}/items/{item_id}/ship - for a custom action like shipping an item.

6. Avoid Redundant Information:

  • Don’t repeat information that is already provided in the path.
  • Example: /api/v1/orders/{order_id}/items/{item_id} instead of /api/v1/users/{user_id}/orders/{order_id}/items/{item_id} if the user_id is not needed.

Example of Well-Structured Nested URLs:

GET /api/v1/companies/{company_id}/departments/{department_id}/employees/{employee_id}
POST /api/v1/orders/{order_id}/items
GET /api/v1/users/{user_id}/addresses/{address_id}/orders

Flattening When Possible:

Instead of deep nesting, consider this:

GET /api/v1/employees?department_id={department_id}&company_id={company_id}
GET /api/v1/orders?user_id={user_id}&status=pending

These practices help maintain a clean, easy-to-understand API design, making it easier for developers to use and extend the API.

Frontend/Dashboard

When designing URL patterns for a frontend application with nested routes, the goal is to reflect the structure of your application while keeping URLs readable and intuitive. Here are some best practices for managing nested routes in a frontend context:

1. Reflect Application Hierarchy:

  • The URL structure should mirror the hierarchy of your application's pages or components.
  • Example:
    • /dashboard/users for a users list under the dashboard.
    • /dashboard/users/{user_id} for a specific user's detail page.

2. Use Meaningful and Consistent Paths:

  • Ensure that each segment of the URL is meaningful and consistent.
  • Example:
    • /products/{product_id}/reviews/{review_id} instead of /products/1234/r/5678.

3. Limit Depth of Nesting:

  • Avoid deep nesting to keep URLs manageable.
  • Example:
    • /projects/{project_id}/tasks/{task_id} is preferable to /companies/{company_id}/projects/{project_id}/tasks/{task_id}.

4. Optional Parameters and Queries:

  • Use optional parameters or query strings for less critical information.
  • Example:
    • /users/{user_id}/orders?status=completed.

5. Dynamic Segments for IDs:

  • Use dynamic segments in the URL for IDs or other unique identifiers.
  • Example:
    • /users/{user_id} for a user profile.
    • /products/{product_id}/edit for editing a specific product.

6. Breadcrumb-Friendly URLs:

  • Design URLs that are breadcrumb-friendly, allowing easy navigation back to parent routes.
  • Example:
    • /courses/{course_id}/lessons/{lesson_id}/content can easily backtrack to /courses/{course_id}/lessons or /courses/{course_id}.

7. Use Slashes for Hierarchy:

  • Slashes (/) should indicate a hierarchical relationship between resources.
  • Example:
    • /categories/{category_id}/products/{product_id} indicates that products belong to a specific category.

8. Avoid Redundant Nesting:

  • Don’t repeat segments unnecessarily.
  • Example:
    • /users/{user_id}/orders is better than /users/{user_id}/user-orders.

Example Patterns:

  1. Admin Dashboard:

    /admin/dashboard
    /admin/users
    /admin/users/{user_id}/edit
    /admin/settings
    
  2. E-commerce Application:

    /shop
    /shop/products
    /shop/products/{product_id}
    /shop/cart
    /shop/checkout
    
  3. Project Management Tool:

    /projects
    /projects/{project_id}
    /projects/{project_id}/tasks
    /projects/{project_id}/tasks/{task_id}/edit
    

Consideration for Single Page Applications (SPA):

  • Route Guarding: Use route guards to protect nested routes that require authentication or specific permissions.
  • Lazy Loading: Implement lazy loading for deeply nested or less frequently accessed routes to improve performance.

These practices ensure that your frontend URLs are intuitive, maintainable, and user-friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment