Skip to content

Commit 4ec6523

Browse files
Merge pull request #2 from Advanced-Infrastructure/setup/api-guideline
Setup/api guideline
2 parents f717c4b + 6d1c4fd commit 4ec6523

2 files changed

Lines changed: 151 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Our comprehensive documentation is available at:
2525

2626
- [Branch Protection Guidelines](./branch_protection.md)
2727
- [Pull Request Guidelines](./pr_guideline.md)
28+
- [API documentation Guidelines](./api_documentation.md)
2829

2930
## 💻 Getting Started
3031

@@ -46,6 +47,6 @@ We welcome contributions! If you have suggestions for improving these guidelines
4647

4748
This project is licensed under the MIT License - see the LICENSE file for details.
4849

49-
---
5050

51-
<p align="center">Made with ❤️ for developers who care about code quality</p>
51+
52+
<p align="center">Made with ❤️ for developers who care about code quality</p>

api_documentation.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# Best Practices for OpenAPI Documentation
2+
3+
Writing clear and comprehensive OpenAPI documentation is critical for developer adoption and API longevity. Below are best practices for versioning, model management, and endpoint documentation.
4+
5+
---
6+
7+
## 1. Model Management
8+
9+
Models (schemas) define the structure of request/response data. Consistency here reduces confusion.
10+
11+
### Best Practices:
12+
- **Leverage JSON Schema**: Use OpenAPI's schema syntax to enforce validation.
13+
14+
```yaml
15+
components:
16+
schemas:
17+
User:
18+
type: object
19+
required:
20+
- id
21+
- name
22+
properties:
23+
id:
24+
type: integer
25+
format: int64
26+
name:
27+
type: string
28+
example: "Jane Doe"
29+
```
30+
- **Reuse Components**: Define reusable schemas, parameters, and responses under `components` to avoid duplication.
31+
- **Provide Examples**: Include sample data for clarity.
32+
33+
```yaml
34+
examples:
35+
UserExample:
36+
value:
37+
id: 1
38+
name: "Jane Doe"
39+
```
40+
- **Document Enums and Constraints**: Specify allowed values, formats, and limits.
41+
42+
```yaml
43+
status:
44+
type: string
45+
enum: [active, inactive, pending]
46+
minLength: 4
47+
maxLength: 8
48+
```
49+
- **Version Models**: Introduce new schema versions (e.g., `UserV2`) for breaking changes.
50+
51+
---
52+
53+
## 2. Endpoint Documentation
54+
55+
Clear endpoint docs help developers integrate quickly.
56+
57+
### Best Practices:
58+
- **Summarize and Describe**:
59+
60+
```yaml
61+
/users/{id}:
62+
get:
63+
summary: Get a user by ID
64+
description: Retrieves a single user's details, including name and contact info.
65+
```
66+
- **Document All Parameters**:
67+
- Specify path, query, header, and body parameters.
68+
- Include examples and required flags.
69+
70+
```yaml
71+
parameters:
72+
- in: path
73+
name: id
74+
required: true
75+
schema:
76+
type: integer
77+
example: 42
78+
```
79+
- **Cover All Responses**:
80+
- Define HTTP status codes, especially errors (4xx/5xx).
81+
- Add examples for success and failure cases.
82+
83+
```yaml
84+
responses:
85+
200:
86+
content:
87+
application/json:
88+
schema:
89+
$ref: "#/components/schemas/User"
90+
404:
91+
description: User not found
92+
```
93+
- **Use Tags for Grouping**: Organize endpoints by functionality (e.g., `Users`, `Auth`).
94+
95+
```yaml
96+
tags:
97+
- name: Users
98+
description: Manage user accounts
99+
```
100+
- **Show Request/Response Examples**:
101+
102+
```yaml
103+
requestBody:
104+
content:
105+
application/json:
106+
schema:
107+
$ref: "#/components/schemas/User"
108+
examples:
109+
UserExample:
110+
value:
111+
name: "Jane Doe"
112+
```
113+
- **Standardize Error Formats**:
114+
115+
```yaml
116+
components:
117+
schemas:
118+
Error:
119+
type: object
120+
properties:
121+
code:
122+
type: integer
123+
message:
124+
type: string
125+
```
126+
127+
---
128+
129+
## Where do I add documentation?
130+
Good question, cowboy. We use serverless.yml to manage API declarations and deployments to APIGateway. So, it made sense to add a block for endpoint documentation.
131+
132+
All documentation is added in the `docs` folder. The yaml file follows the OpenAPI standards. Read about OpenAPI spec here [openAPI](https://github.com/OAI/OpenAPI-Specification)
133+
134+
Next, edit serverless open `serverless.yml` in `be-api-test` [serverless](https://github.com/Advanced-Infrastructure/be-api-test/blob/dev/serverless.yml)
135+
Under functions, see the documentation block `documentation: ${file(./docs/api/documentation.yml):endpoints.api-endpoint}`
136+
137+
Make sure you declare the documentation block for every endpoint.
138+
139+
140+
---
141+
142+
143+
## Final Tips
144+
- **Validate Your Spec**: Use tools like Swagger Editor or Spectral to lint your OpenAPI file.
145+
- **Automate Documentation**: Generate interactive docs with Swagger UI or Redoc.
146+
- **Keep It Consistent**: Follow the same structure, naming conventions, and style across all endpoints.
147+
148+
Well-documented APIs reduce support overhead and empower developers to build faster. 📘🚀

0 commit comments

Comments
 (0)