Documenting a REST API is crucial for both internal development and external API consumption. Good documentation makes it easier for developers to understand and use the API effectively. Documentation should include comprehensive details about the API’s capabilities, endpoints, request/response formats, authentication methods, and examples of usage:
One example of well-made API docs: Stripe API Reference
npm install apidocpackage.json, for example: "apidoc": "apidoc -i src/ -o docs/"Add some apidoc configuration to package.json, for example:
...
"apidoc": {
"title": "My API docs",
"name": "My API docs",
"url": "https://example.com/api/v1",
"order": [
"Authentication",
"User"
]
},
...
or create a separate config file: apidoc.json, example of a more detailed configuration, where you can define the order of the endpoints, custom header and footer (in this case using separate markdown files) and other options:
{
"name": "My apidoc example",
"version": "0.3.0",
"description": "My apiDoc example project",
"title": "Custom apiDoc browser title",
"url" : "https://api.example.com/v1",
"header": {
"title": "My own header title",
"filename": "header.md"
},
"footer": {
"title": "My own footer title",
"filename": "footer.md"
},
"order": [
"GetUser",
"PostUser"
],
"template": {
"withCompare": true,
"withGenerator": true
}
}
Add api annotations to your code comments in routers (or to a separate js-file), for example:
/**
* @api {get} /api/resource/:id Request Resource information
* @apiName GetResource
* @apiGroup Resource
*
* @apiParam {Number} id Resource unique ID.
*
* @apiSuccess {String} firstname Firstname of the Resource.
* @apiSuccess {String} lastname Lastname of the Resource.
*/
npm run apidocdocs/ folder using express.static middlewaredocs/ folder to .gitignore filehttp://localhost:3000/docs/ to see the generated documentationExample annotations for auth-router.js:
const authRouter = express.Router();
/**
* @apiDefine all No authentication needed.
*/
/**
* @apiDefine token Logged in user access only
* Valid authentication token must be provided within request.
*/
/**
* @apiDefine UnauthorizedError
* @apiError UnauthorizedError User name or password invalid.
* @apiErrorExample Error-Response:
* HTTP/1.1 403 Forbidden
* {
* "error": {
* "message": "username/password invalid",
* "status": 401
* }
* }
*/
/**
* @api {post} /login Login
* @apiVersion 1.0.0
* @apiName PostLogin
* @apiGroup Authentication
* @apiPermission all
*
* @apiDescription Sign in and get an authentication token for the user.
*
* @apiBody {String} username Username of the user.
* @apiBody {String} password Password of the user.
*
* @apiParamExample {json} Request-Example:
* {
* "username": "johnd",
* "password": "examplepass"
* }
*
* @apiSuccess {String} token Token for the user authentication.
* @apiSuccess {Object} user User info.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "message": "Logged in successfully",
* "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoyMSwid
* XNlcm5hbWUiOiJ1dXNpMSIsImVtYWlsIjoidXVzaTFAZXhhbXBsZS5jb20
* iLCJ1c2VyX2xldmVsX2lkIjoyLCJpYXQiOjE3MDEyNzkzMjJ9.3TbVTcXS
* dryTDm_huuXC_U1Lg4rL0SOFyn_WAsC6W0Y"
* "user": {
* "user_id": 21,
* "username": "johnd",
* "email": "johnd@example.com",
* "user_level": "regular"
* }
* }
*
* @apiUse UnauthorizedError
*/
authRouter.route('/login').post(postLogin);
/**
* @api {get} /auth/me Request information about current user
* @apiVersion 1.0.0
* @apiName GetMe
* @apiGroup Authentication
* @apiPermission token
* @apiHeader {String} Authorization Bearer token.
*
* @apiSuccess {Object} user User info.
* @apiSuccess {Number} user.user_id Id of the User.
* @apiSuccess {String} user.username Username of the User.
* @apiSuccess {String} user.email email of the User.
* @apiSuccess {Number} user.user_level_id User level id of the User.
* @apiSuccess {Number} user.iat Token creation timestamp.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "user_id": 21,
* "username": "johnd",
* "email": "johnd@example.com",
* "user_level_id": 2,
* "iat": 1701279021
* }
*
* @apiError InvalidToken Authentication token was invalid.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 403 Forbidden
* {
* "message": "invalid token"
* }
*/
authRouter.route('/me').get(authenticateToken, getMe);
docs (see teachers’s lecture example)docs folder using npm script)express.static middleware for serving the docs folder)Returning: No specific return is needed. The documentation should be available for your individual project.