Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add RESTful API to API documentation #2680

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions apps/www/content/glossary/restful-api.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
---
title: "RESTful API: Design & Best Practices Guide"
description: Unlock RESTful API design power. Learn essentials from experts. Master design patterns and naming conventions.
h1: "RESTful API: Design Patterns & Best Practices"
term: RESTful API
categories: []
takeaways:
tldr: RESTful API is a set of principles for designing networked applications. It uses HTTP requests to access and manipulate data.
definitionAndStructure:
- key: REST Compliance
value: Adherence to REST principles
- key: Delphi Study Methodology
value: Expert opinion gathering
- key: Richardson Maturity Model
value: HTTP methods and status codes usage
- key: Quality Attributes
value: Usability and maintainability
- key: Rule Categorization
value: Design rules classification
historicalContext:
- key: Introduced
value: "2000"
- key: Origin
value: Web Services (RESTful API)
- key: Evolution
value: Standardized RESTful API
usageInAPIs:
tags:
- HTTP
- REST
- API
description: RESTful APIs are used to create, read, update, and delete (CRUD) resources on a server. They use standard HTTP methods like GET, POST, PUT, DELETE. They are stateless, meaning each request from client to server must contain all the information needed to understand and process the request.
bestPractices:
- "Use HTTP methods appropriately: GET for retrieving, POST for creating, PUT for updating, and DELETE for removing resources."
- Design APIs to be stateless, each request should contain all the information necessary to service the request.
- Use meaningful and clear URIs that represent resources.
recommendedReading:
- url: https://www.researchgate.net/publication/344417926_Which_RESTful_API_Design_Rules_Are_Important_and_How_Do_They_Improve_Software_Quality_A_Delphi_Study_with_Industry_Experts
title: Which RESTful API Design Rules Are Important and How Do They Improve Software Quality? A Delphi Study with Industry Experts
- url: http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
title: RESTful Web Services
Comment on lines +40 to +41
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Incorrect URL for "RESTful Web Services" reference.

The URL points to Fielding's dissertation but the title suggests it should link to a different resource about RESTful Web Services.

Consider updating to a more appropriate URL that matches the title, or update the title to match the current URL's content: "Architectural Styles and the Design of Network-based Software Architectures".

- url: https://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven
title: REST APIs must be hypertext-driven
didYouKnow: REST stands for Representational State Transfer. It was introduced in 2000 by Roy Fielding in his doctoral dissertation.
faq:
- answer: Writing good REST API documentation involves several key steps. First, plan your documentation structure carefully, ensuring it aligns with the API's functionality. Prioritize important sections such as API endpoints, request/response examples, and error codes. Maintain consistency in your language and format to make the documentation easy to follow. Keep your explanations simple and clear, avoiding unnecessary jargon. Add interactivity where possible, such as 'Try it out' features, to help users understand how the API works. Lastly, cater to all levels of technical expertise by providing detailed explanations for beginners and concise, technical details for experienced users.
question: How do you write a good REST API documentation?
- answer: Best practices for building a RESTful API include using clear, noun-based resource names for easy understanding. Implement security measures such as OAuth 2.0 and rate limiting to protect your API. Optimize performance by caching data and compressing responses. Make your API user-friendly by providing interactive documentation using tools like OpenAPI. Use semantic versioning to manage changes and updates. Ensure the quality of your API by conducting thorough unit, integration, and security testing. Lastly, monitor your API's health, usage, and performance to maintain its reliability and efficiency.
question: Which are examples of best practices for building a RESTful API?
- answer: "The three fundamental principles of a RESTful API are: 1) Uniform Interface: This principle ensures that the API has a consistent interface, making it easier for clients to interact with the server. 2) Statelessness: This means that each request from the client to the server must contain all the information needed to understand and process the request. The server should not store any context between requests. 3) Layered System: This allows an architecture to be composed of hierarchical layers by constraining component behavior. Other principles include Cacheability and Code on Demand."
question: What are the three principles for a RESTful API?
- answer: "A RESTful API consists of three major components: 1) Client: This is the application or software code that sends requests to the server for a resource. 2) Server: This is the application or software code that controls the resource. It processes the client's requests and sends responses. 3) Resource: This is the data or service that the client requests. It can be any information that can be named, such as a document or an image. The client interacts with a representation of the resource, rather than the resource itself."
question: What are the three components of a RESTful API?
updatedAt: 2024-11-25T19:00:24.000Z
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Future date detected in updatedAt field.

The updatedAt timestamp is set to November 25, 2024, which is in the future.

Update the timestamp to reflect the current date or remove it to let the system manage this automatically.

slug: restful-api
---

A **RESTful API**, or **Representational State Transfer API**, is a set of principles that provide developers with guidelines and best practices for creating scalable web services. REST APIs utilize standard HTTP methods such as GET, POST, PUT, DELETE, and PATCH to perform CRUD operations. This architecture leverages the existing web infrastructure, making it a natural choice for building APIs that are easy to understand and use.

## Understanding RESTful API Concepts

RESTful APIs are inherently **stateless**, meaning each request from a client to a server must contain all the information needed to understand and complete the request. The server does not store any state about the client session, which enhances scalability by reducing server memory requirements. Communication between client and server occurs using standard HTTP protocols, with data typically returned in **JSON** or **XML** format.

## REST API Design Patterns and Best Practices

When designing RESTful APIs, adhering to **REST API standards** is crucial for ensuring reliability, maintainability, and scalability. Here are some essential **RESTful API design patterns and best practices**:

- Use nouns instead of verbs in endpoint paths to represent resources.
- Implement idempotent operations where possible to improve reliability.
- Utilize HTTP status codes correctly to communicate the outcome of API requests.
- Leverage caching mechanisms to enhance performance.

## REST API URL Best Practices and Examples

A well-designed REST API URL should be intuitive and convey the resource hierarchy, making it understandable and predictable. Here are some **REST API URL best practices**:

- Use plural nouns for resources (e.g., `/users`).
- Keep URLs simple and concise.
- Use query parameters for filtering, sorting, and pagination.

**Example:**
- List of users: `GET /users`
- User details: `GET /users/{id}`

## REST API Documentation and Examples

Effective **RESTful API documentation** is crucial for the success of any API. It should include:

- A comprehensive overview of the API.
- Clear, executable examples of requests and responses.
- Authentication and authorization procedures.
- Error codes and messages.

**Example:**
```json
GET /users/123
Response:
{
"id": "123",
"name": "John Doe",
"email": "[email protected]"
}
```
Comment on lines +95 to +103
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance API example with headers and status code.

The API example would be more complete with HTTP headers and status code information.

Consider updating the example to include these details:

 GET /users/123
+
+HTTP/1.1 200 OK
+Content-Type: application/json
+
 Response:
 {
   "id": "123",
   "name": "John Doe",
   "email": "[email protected]"
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
```json
GET /users/123
Response:
{
"id": "123",
"name": "John Doe",
"email": "[email protected]"
}
```
```json
GET /users/123
HTTP/1.1 200 OK
Content-Type: application/json
Response:
{
"id": "123",
"name": "John Doe",
"email": "[email protected]"
}
```


## REST API Naming Conventions

Consistent naming conventions in REST API design enhance readability and usability. Common **REST API best practices for naming** include:

- Using camelCase or snake_case consistently across all endpoints.
- Pluralizing nouns to represent collections or lists.
- Keeping endpoint names concise and descriptive.

## REST API Design Example

Consider an API for a simple blog platform:

- **List all posts**: `GET /posts`
- **Create a new post**: `POST /posts`
- **Read a specific post**: `GET /posts/{id}`
- **Update a post**: `PUT /posts/{id}`
- **Delete a post**: `DELETE /posts/{id}`

Each endpoint clearly represents the actions that can be performed on the `posts` resource, adhering to REST principles and using HTTP methods appropriately.

By following these **REST API best practices**, developers can create robust, scalable, and user-friendly APIs that meet the needs of their applications. Whether you're new to API development or looking to refine your skills, understanding these concepts is essential for success in the field.
Loading