Skip to content

Commit 1d94077

Browse files
committed
feat: new api docs using scalar
1 parent a27afb1 commit 1d94077

File tree

5 files changed

+2385
-2
lines changed

5 files changed

+2385
-2
lines changed

APISPEC.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# API Documentation
2+
3+
## Overview
4+
5+
Forward Email provides a comprehensive API for managing email forwarding, domains, aliases, and more. The API documentation is generated from an OpenAPI specification and rendered using [Scalar](https://github.com/scalar/scalar).
6+
7+
## API Specification Process
8+
9+
The API specification is maintained as a structured OpenAPI v3 JSON file:
10+
11+
- **File Location**: The specification is stored as `api-spec.json` in the `assets/` folder
12+
- **Build Process**: During the build process, the specification is copied to the build folder
13+
- **Web Routes**: The API documentation is served via the `/api` and `/email-api` web routes
14+
- **Renderer**: The documentation is rendered using Scalar API tool, configured in `app/views/email-api.pug`
15+
16+
## Maintaining the API Specification
17+
18+
When updating the API, follow these steps to maintain the specification:
19+
20+
1. **Edit the Specification**: Update `assets/api-spec.json` with new endpoints, schemas, or descriptions
21+
2. **Use Markdown**: Leverage rich markdown in descriptions for better rendering in Scalar
22+
3. **Follow Best Practices**:
23+
- Keep the main API description concise and high-level
24+
- Place error details in the Error schema component
25+
- Add specific examples to individual endpoints
26+
- Use tables for parameter descriptions
27+
- Include code examples with syntax highlighting
28+
29+
## Markdown Enhancement Guidelines
30+
31+
The API specification uses markdown for rich documentation in Scalar:
32+
33+
### Schema Descriptions
34+
35+
```json
36+
{
37+
"schemas": {
38+
"Example": {
39+
"description": "## Example Object\n\n### Properties Overview\n\n| Property | Type | Description |\n|----------|------|-------------|\n| id | string | Unique identifier |\n| name | string | The name of the example |\n\n### Example JSON\n\n```json\n{\n \"id\": \"123\",\n \"name\": \"Example Name\"\n}\n```"
40+
}
41+
}
42+
}
43+
```
44+
45+
### Endpoint Descriptions
46+
47+
```json
48+
{
49+
"paths": {
50+
"/v1/example": {
51+
"get": {
52+
"description": "**Get Example**\n\n### Parameters\n\n| Parameter | Type | Required | Description |\n|-----------|------|----------|-------------|\n| id | string | Yes | Example ID |\n\n### Example Request\n\n```bash\ncurl https://api.forwardemail.net/v1/example \\\n -u API_TOKEN:\n```\n\n### Example Response\n\n```json\n{\n \"id\": \"123\",\n \"name\": \"Example Name\"\n}\n```"
53+
}
54+
}
55+
}
56+
}
57+
```
58+
59+
## Testing Documentation Changes
60+
61+
After updating the API specification:
62+
63+
1. Run the build process to copy the updated specification to the build folder
64+
2. Navigate to `/api` or `/email-api` in your development environment
65+
3. Verify that the documentation renders correctly in Scalar
66+
4. Check that all markdown formatting appears as expected
67+
5. Test the "Try It" functionality if available
68+
69+
## Scalar Configuration
70+
71+
The Scalar API documentation tool is configured in `app/views/email-api.pug`. If you need to customize the Scalar rendering:
72+
73+
1. Edit the configuration in this file
74+
2. Refer to the [Scalar documentation](https://github.com/scalar/scalar) for available options
75+
3. Test changes in the development environment before deploying
76+
77+
## Deployment
78+
79+
When deploying API documentation changes:
80+
81+
1. Ensure the updated `api-spec.json` is included in the deployment
82+
2. Verify that the build process correctly copies the specification
83+
3. Check the live documentation after deployment to confirm changes are visible

app/views/email-api.pug

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
block body
2+
#app
3+
4+
// Load the Script
5+
script(src="https://cdn.jsdelivr.net/npm/@scalar/api-reference")
6+
7+
// Initialize the Scalar API Reference
8+
script.
9+
window.WEB_URL = "#{ config.WEB_URL }";
10+
window.API_TOKEN = "#{user ? user[config.userFields.apiToken] : ""}";
11+
Scalar.createApiReference("#app", {
12+
// The URL of the OpenAPI/Swagger document
13+
url: `${window.WEB_URL}/api-spec.json`,
14+
authentication: {
15+
preferredSecurityScheme: "ApiKeyAuth",
16+
securitySchemes: {
17+
ApiKeyAuth: {
18+
username: window.API_TOKEN,
19+
password: "",
20+
},
21+
AliasAuth: {
22+
name: "Authorization",
23+
in: "header",
24+
value: window.API_TOKEN,
25+
},
26+
},
27+
},
28+
});

0 commit comments

Comments
 (0)