Course contentsShow
← Software Engineering
Lesson 781 of 1,93324. REST API DesignFree lesson

Handling Actions That Don't Map to CRUD

Explore patterns for non-CRUD operations: controller resources, status transitions, and action endpoints.

Handling Actions That Don't Map to CRUD

What you'll learn: How to design REST API endpoints for operations that don't fit the standard create, read, update, delete model.

Not every real-world action maps neatly to CRUD operations on resources. What happens when you need to "send an email," "cancel an order," or "approve a request"? These are actions or state transitions, not simple data manipulations.

Three Patterns for Non-CRUD Operations

1. Controller Resources
Treat the action itself as a resource. Instead of trying to force "send email" into a PUT or POST on /emails, create an endpoint like POST /email-sends or POST /notifications. The action becomes a noun—a thing you create a record of.

2. Status Transitions
Model state changes as updates to a status field. Instead of POST /orders/123/cancel, use PATCH /orders/123 with {"status": "cancelled"}. This keeps your API RESTful by updating a resource property, though it requires the client to know valid state transitions.

3. Action Endpoints
For operations that truly don't fit elsewhere, use explicit action endpoints as sub-resources: POST /orders/123/cancel or POST /accounts/456/activate. While purists debate whether this violates REST principles, pragmatism often wins—clarity matters more than dogma.

Choosing Your Approach

Controller resources work best when the action has meaningful side effects you might query later. Status transitions shine when operations are really state changes. Action endpoints excel when the operation is complex, contextual, or doesn't map to any resource attribute.

Key Takeaway: When CRUD doesn't fit, choose between controller resources (actions as nouns), status transitions (state updates), or pragmatic action endpoints—prioritizing clarity and consistency over strict REST ideology.