Foundation for Success: When designing API projects, it makes sense to build upon the REST best practices and guidelines for web services already implemented by countless others. Rather than start anew, build upon this foundation of API guidelines from thousands of successful API companies.
Understanding RESTful APIs
REST (Representational State Transfer) is an architectural style for designing web services. While you don't technically need to use the HTTP protocol, REST and HTTP developed alongside each other, and almost every RESTful API relies upon HTTP. For that reason, it makes sense to structure your API around the built-in methods and status codes that are already well-defined in HTTP.
A RESTful API uses HTTP methods to express the primary purpose of each call rather than embedding actions in the URL. This creates a clean, predictable interface that developers can easily understand and implement.
HTTP Methods and Status Codes
Each HTTP request includes a method, sometimes called "HTTP verbs," that provides context for each call. Understanding these methods is crucial for designing effective REST APIs.
Common HTTP Methods
Here are the most common HTTP methods used in REST APIs:
- GET:read data from your API
- POST:add new data to your API
- PUT:update existing data with your API
- PATCH:updates a subset of existing data with your API
- DELETE:remove data (usually a single resource) from your API
As you design your API, rely on these methods to express the primary purpose of a call rather than adding the purpose to the URL. The methods should be the verbs, while your URLs provide the nouns (resources).
Common HTTP Status Codes
HTTP status codes help describe the response from server to client. Understanding the appropriate use of status codes is essential for clear API communication.
Success Codes (2xx)
200- Successful request (GET)201- Created (POST)204- No content (PUT/PATCH)
Error Codes (4xx/5xx)
400- Bad request401- Unauthorized404- Not found429- Too many requests500- Server error
Use Friendly and Common API Endpoint Names
A typical design pattern with REST APIs is to build your endpoints based on resources. These are the "nouns" to HTTP method verbs. Your API design will be much easier to understand if these names are descriptive and follow common conventions.
For example, if you're working on a cookbook API, you might use the following endpoint: /recipes/
As you add new recipes, you would POST them to the endpoint. To get a list, you use the GET method on the same endpoint. To retrieve a specific recipe, you could call it by its identifier: /recipes/42
One thing to specifically avoid is describing actions in the endpoint name. For example, a verb within the endpoint like /getRecipes/ would run counter to relying on the HTTP method to provide that context.
GET vs. POST: Choosing the Right Method
As you're designing RESTful APIs, rely on the HTTP methods to express the primary purpose of a call. Here are several factors that will influence your decision:
GET Request Characteristics:
- Can be cached
- Are idempotent (can be called multiple times with the same result)
- Should never be used for sensitive data
- Have length restrictions
- Should only retrieve data
POST Request Characteristics:
- Never cached (unless specified in headers)
- NOT idempotent
- Have no restrictions on data length
- Used to create new resources
CRUD Example with REST
Implementing basic CRUD functionality (Create, Read, Update, Delete) with proper HTTP methods:
[POST] /api/recipes - Creates a recipe [GET] /api/recipes - Lists recipes [GET] /api/recipes/:id - Gets a recipe by ID [PUT] /api/recipes/:id - Updates a recipe by ID [DELETE] /api/recipes/:id - Deletes a recipe by ID [DELETE] /api/recipes - Bulk delete recipes
In this example, you only have 2 actual routes: /api/recipes and /api/recipes/:id. The HTTP method determines the action, not additional endpoints.
Cache Data to Improve Performance
Caching partially eliminates unnecessary trips to the server. Returning data from local memory rather than sending a query for each new request can improve your app's performance significantly.
GET requests are cacheable by default, however, POST requests require you to specify the cache requirements in the header. Caching can lead to stale data, so it's important to handle this properly.
Cache-Control Headers
When using Cache-Control, you can set specific directives such as:
- max-age: Specifies the maximum amount of time a resource will be considered fresh
- must-revalidate: Forces the cache to revalidate before serving stale content
- no-transform: Prevents caching intermediaries from modifying the response
Plan for Future Use Cases with API Parameters
Naive or simplistic API design can follow all the guidelines above and still not support the use cases that developers will need. It's important to thoroughly understand how an API will be used and plan for extensibility.
There are three common types of parameters to consider for your API:
1. Filtering
Return only results that match specific criteria
GET /users?age=30
2. Pagination
Control the number of results returned
GET /users?page=3&per_page=20
3. Sorting
Order results by specific fields
GET /users?sort=name&order=asc
These three approaches can be used together to support very specific queries. Understanding your use cases will help determine the complexity of your parameters.
JSON as the API Data Format
While REST doesn't require JSON, it has become the de facto standard for RESTful APIs. JSON's lightweight nature, easy parsing, and language independence make it ideal for API responses.
When designing your API responses, use JSON consistently. Your JSON responses should be well-formatted, properly validated, and include clear error messages when things go wrong.
Example API Response
{
"status": "success",
"data": {
"recipes": [
{
"id": 1,
"name": "Chocolate Chip Cookies",
"prepTime": 15,
"cookTime": 10
}
]
},
"pagination": {
"page": 1,
"per_page": 20,
"total": 100
}
}Borrow From Existing Conventions
Very few of us are building completely unique APIs, so there is a lot to learn from others. Many API standards are built around REST APIs. When you implement authentication, for example, don't blaze a new trail—use established patterns like OAuth.
You'll find standards for API headers and data formats like JSON and XML. Your industry may have its own set of standards or conventions. Even if they aren't as strict as regulations, it's worth considering patterns with which developers will already be familiar.
Conclusion
REST API design patterns provide a solid foundation for building robust, scalable web services. By following these best practices—using proper HTTP methods, maintaining consistent naming conventions, implementing caching strategies, and planning for extensibility—you can create APIs that are both powerful and easy to use.
Remember that good API design is about making life easier for developers who will consume your API. Consistency, predictability, and clear documentation are key to API success.
Working with API JSON Responses?
Use our free JSON formatter to format, validate, and work with JSON data from REST APIs. Make your API development easier with our powerful formatting tools.
Try JSON Formatter