SlideShare a Scribd company logo
1 of 24
Download to read offline
APIDOCInline Documentation for RESTful web APIs
Introduction with a small example.
http://apidocjs.com
What does apiDoc ?
apiDoc
… creates a Documentation HTML-Page
… use API-Descriptions from your source code
For who ?
apiDoc can be used with any programming language, that allow
block documentation:
/**
* This is a comment.
*/
Where i find apiDoc ?
apiDoc is Open Source and distributed over NPM and github
Visit: http://apidocjs.com
Special functions
apiDoc
… support own created Templates
… includes History
… compare old and new API-Method Versions
… allows frontend Developer to see what changed
… support Inherit
… reuse of Documentation parts
… extendable
… you can create your own Documentation Methods
Ok, let us begin...
apiDoc is a node module, so install Node.js first:
http://nodejs.org
Node.js includes „npm“, a package manager for node.
Install apiDoc
[sudo] npm install -g apidoc
We install apiDoc global, so you can access it from everywhere.
EXAMPLE PROJECT
We use a simple static JavaScript example.
We ignore routing, validation or database operations.
This are only small parts of apiDocs functions, for all visit http://apidocjs.com
There is also a complex example: http://apidocjs.com/#example-full
Create a new directory „my_project“.
Within that dir create a file „example.js“.
example.js
var currentUser = {
name: 'Mary'
};
function getUser() {
return { code: 200, data: currentUser };
}
function setName(name) {
if(name.length === 0) {
return { code: 404, message: 'NameEmptyError' };
}
currentUser.name = name;
return { code: 204 };
}
A basic doc
Above „function getUser“ we place a basic documentation block
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
*/
function getUser() {
return { code: 200, data: currentUser };
}
This 3 parameters are required!
@api describes the path of the Method that clients call.
@apiName is a unique Name, you can name as you want, for simplicity i prefer
„Type of Method“ + „Name of Method“ e.g. {get} and „/user“ => GetUser.
@apiGroup is the name of the group to which this method belongs. The group will be
used for Navigation.
By default apiDoc search for all „.jjs“ files in current directory and sub directories.
You can change the includeFilter, call „apidoc -h“ for details.
apiDoc created a new HTML-Page in directory „doc“.
From command line run within „my_project“ directory:
apidoc
Open „doc/index.html“ in your Browser
We see the rudimentary Webpage, with Navigation left and Content right.
We have a group „User“ and within that group an API-Method „GET /user“
Describe the API-Answer
Now we add success return parameters
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
*
* @apiSuccess {String} name The users name.
*/
function getUser() {
return { code: 200, data: currentUser };
}
apidoc
Run apiDoc again, it will overwrite the „doc“ Directory
Now we have a little more Information and see what „GET /user“ returns on a
successful reply „Success 200“.
Add a Version and example Code
We add a Version (Format “major.minor.patch”, see http://semver.org for details)
and we add an example code, that shows what will be return in case of success
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
* @apiVersion 0.1.0
*
* @apiSuccess {String} name The users name.
*
* @apiSuccessExample Example data on success:
* {
* name: 'Paul'
* }
*/
function getUser() {
return { code: 200, data: currentUser };
}
In the right dropdown box we see now the Version „0.1.0“ and under „Success 200“
our integrated example.
VERSIONING
If you work in a Team and some Programmer work an the API backend and some on
Frontend Client, it is important to know what changed in a new Release.
Especially during developing an Application, many things can change.
An API Method change
Create a new file „_apidoc.js“.
Put only the documentation Block from „getUser“ in that file:
_apidoc.js
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
* @apiVersion 0.1.0
*
* @apiSuccess {String} name The users name.
*
* @apiSuccessExample Example data on success:
* {
* name: 'Paul'
* }
*/
„_apidoc.js“ will be our history file and contains old documentation blocks.
Now we change the documentation block in „example.js“
example.js
/**
* @api {get} /user Request User information
* @apiName GetUser
* @apiGroup User
* @apiVersion 0.2.0
*
* @apiSuccess {String} name The users name.
* @apiSuccess {Number} age Calculated age from Birthday
*
* @apiSuccessExample Example data on success:
* {
* name: 'Paul',
* age: 27
* }
*/
function getUser() {
return { code: 200, data: currentUser };
}
After call „apidoc“ and open „doc/index.html“ you see now Version „0.2.0“ with the
Field „age“ and the changed example.
And now the magic...
Select from Dropdown „0.2.0“ the Version „0.1.0“.
You see now a comparison from the 2 Versions. Green marked content indicates the
Fields that are added to Version „0.2.0“.
In the future, when you made more changes to an API-Method, simply copy and add
the complete documentation block to the history file „_apidoc.js“.
Leave the old blocks as they are.
Then you have a Documentation where everybody can see any change of your API.
PARAMS AND ERRORS
We take now a quick look at how to add Parameters, that an API-Method need and
how we return errors.
We add now a documentation to our second function „setName“.
example.js
/**
* @api {put} /user Change User
* @apiName PutUser
* @apiGroup User
* @apiVersion 0.1.0
*
* @apiParam {String} name New name of the user
*
* @apiError NameEmptyError The name was empty. Minimum of <code>1</code> character is required.
*/
function setName(name) {
if(name.length === 0) {
return { code: 404, message: 'NameEmptyError' };
}
currentUser.name = name;
return { code: 204 };
}
You will see now the new „Change User“ entry in navigation and the content for the
API-Method.
Thank you and have a productive use !
For more Information visit http://apidocjs.com
Author: Peter Rottmann
peter@apidocjs.com

More Related Content

What's hot

Buck outside Valley
Buck outside ValleyBuck outside Valley
Buck outside Valley
Bruno Rocha
 
An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager
WSO2
 
Customizing workflows in WSO2 API Manager
Customizing workflows in WSO2 API ManagerCustomizing workflows in WSO2 API Manager
Customizing workflows in WSO2 API Manager
WSO2
 
Alm Specialist Toolkit Team System 2008 Deep Dive
Alm Specialist Toolkit   Team System 2008 Deep DiveAlm Specialist Toolkit   Team System 2008 Deep Dive
Alm Specialist Toolkit Team System 2008 Deep Dive
Christian Thilmany
 
Customizing the API Store & Publisher in WSO2 API Manager
Customizing the API Store & Publisher in WSO2 API ManagerCustomizing the API Store & Publisher in WSO2 API Manager
Customizing the API Store & Publisher in WSO2 API Manager
WSO2
 
Running the-next-generation-of-cloud-native-applications-using-open-applicati...
Running the-next-generation-of-cloud-native-applications-using-open-applicati...Running the-next-generation-of-cloud-native-applications-using-open-applicati...
Running the-next-generation-of-cloud-native-applications-using-open-applicati...
NaveedAhmad239
 

What's hot (20)

API designing with WSO2 API Manager
API designing with WSO2 API ManagerAPI designing with WSO2 API Manager
API designing with WSO2 API Manager
 
Hccjp kong 210409
Hccjp kong 210409Hccjp kong 210409
Hccjp kong 210409
 
apidays LIVE New York 2021 - API tool chain for low budget programs by Paul K...
apidays LIVE New York 2021 - API tool chain for low budget programs by Paul K...apidays LIVE New York 2021 - API tool chain for low budget programs by Paul K...
apidays LIVE New York 2021 - API tool chain for low budget programs by Paul K...
 
Buck outside Valley
Buck outside ValleyBuck outside Valley
Buck outside Valley
 
Google Cloud Enpoints
Google Cloud EnpointsGoogle Cloud Enpoints
Google Cloud Enpoints
 
2.3.anypoint exchange
2.3.anypoint exchange2.3.anypoint exchange
2.3.anypoint exchange
 
An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager An Introduction to the WSO2 API Manager
An Introduction to the WSO2 API Manager
 
Customizing workflows in WSO2 API Manager
Customizing workflows in WSO2 API ManagerCustomizing workflows in WSO2 API Manager
Customizing workflows in WSO2 API Manager
 
Pure APIs: Development workflows for successful API integrations
Pure APIs: Development workflows for successful API integrationsPure APIs: Development workflows for successful API integrations
Pure APIs: Development workflows for successful API integrations
 
Alm Specialist Toolkit Team System 2008 Deep Dive
Alm Specialist Toolkit   Team System 2008 Deep DiveAlm Specialist Toolkit   Team System 2008 Deep Dive
Alm Specialist Toolkit Team System 2008 Deep Dive
 
Design Driven API Development
Design Driven API DevelopmentDesign Driven API Development
Design Driven API Development
 
API workshop by AWS and 3scale
API workshop by AWS and 3scaleAPI workshop by AWS and 3scale
API workshop by AWS and 3scale
 
OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)
OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)
OpenAPI Spec at Google (Open API Initiative Meetup on 2016-09-15)
 
Customizing the API Store & Publisher in WSO2 API Manager
Customizing the API Store & Publisher in WSO2 API ManagerCustomizing the API Store & Publisher in WSO2 API Manager
Customizing the API Store & Publisher in WSO2 API Manager
 
Running the-next-generation-of-cloud-native-applications-using-open-applicati...
Running the-next-generation-of-cloud-native-applications-using-open-applicati...Running the-next-generation-of-cloud-native-applications-using-open-applicati...
Running the-next-generation-of-cloud-native-applications-using-open-applicati...
 
Cloud-first SharePoint JavaScript Add-ins - Collab 365
Cloud-first SharePoint JavaScript Add-ins - Collab 365Cloud-first SharePoint JavaScript Add-ins - Collab 365
Cloud-first SharePoint JavaScript Add-ins - Collab 365
 
Build and Manage Serverless APIs (APIDays Nordic, May 19th 2016)
Build and Manage Serverless APIs (APIDays Nordic, May 19th 2016)Build and Manage Serverless APIs (APIDays Nordic, May 19th 2016)
Build and Manage Serverless APIs (APIDays Nordic, May 19th 2016)
 
Global Azure BootCamp: Azure Logic Apps
Global Azure BootCamp: Azure Logic AppsGlobal Azure BootCamp: Azure Logic Apps
Global Azure BootCamp: Azure Logic Apps
 
WSO2 API Manager : Going beyond the just API Management
WSO2 API Manager : Going beyond the just API ManagementWSO2 API Manager : Going beyond the just API Management
WSO2 API Manager : Going beyond the just API Management
 
[API the Docs Paris 2018] Architecting DX
[API the Docs Paris 2018] Architecting DX[API the Docs Paris 2018] Architecting DX
[API the Docs Paris 2018] Architecting DX
 

Viewers also liked

(API) Docs for Developers
(API) Docs for Developers(API) Docs for Developers
(API) Docs for Developers
Brandon West
 
Learnings @WalmartLabs - Agile journey
Learnings @WalmartLabs  - Agile journeyLearnings @WalmartLabs  - Agile journey
Learnings @WalmartLabs - Agile journey
Abinav Munshi
 

Viewers also liked (8)

(API) Docs for Developers
(API) Docs for Developers(API) Docs for Developers
(API) Docs for Developers
 
Learnings @WalmartLabs - Agile journey
Learnings @WalmartLabs  - Agile journeyLearnings @WalmartLabs  - Agile journey
Learnings @WalmartLabs - Agile journey
 
Distribution, redundancy and high availability using OpenSIPS
Distribution, redundancy and high availability using OpenSIPSDistribution, redundancy and high availability using OpenSIPS
Distribution, redundancy and high availability using OpenSIPS
 
AWS October Webinar Series - Introducing Amazon Elasticsearch Service
AWS October Webinar Series - Introducing Amazon Elasticsearch ServiceAWS October Webinar Series - Introducing Amazon Elasticsearch Service
AWS October Webinar Series - Introducing Amazon Elasticsearch Service
 
2015 Future of Open Source Survey Results
2015 Future of Open Source Survey Results2015 Future of Open Source Survey Results
2015 Future of Open Source Survey Results
 
OPEN SOURCE SEMINAR PRESENTATION
OPEN SOURCE SEMINAR PRESENTATIONOPEN SOURCE SEMINAR PRESENTATION
OPEN SOURCE SEMINAR PRESENTATION
 
(BDT209) Launch: Amazon Elasticsearch For Real-Time Data Analytics
(BDT209) Launch: Amazon Elasticsearch For Real-Time Data Analytics(BDT209) Launch: Amazon Elasticsearch For Real-Time Data Analytics
(BDT209) Launch: Amazon Elasticsearch For Real-Time Data Analytics
 
Open Source Creativity
Open Source CreativityOpen Source Creativity
Open Source Creativity
 

Similar to apiDoc Introduction

OpenSocial Intro
OpenSocial IntroOpenSocial Intro
OpenSocial Intro
Pamela Fox
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
Akhil Mittal
 

Similar to apiDoc Introduction (20)

Creating web api and consuming- part 1
Creating web api and consuming- part 1Creating web api and consuming- part 1
Creating web api and consuming- part 1
 
Google cloud endpoints
Google cloud endpointsGoogle cloud endpoints
Google cloud endpoints
 
Google Cloud Endpoints - Soft Uni 19.06.2014
Google Cloud Endpoints - Soft Uni 19.06.2014Google Cloud Endpoints - Soft Uni 19.06.2014
Google Cloud Endpoints - Soft Uni 19.06.2014
 
OpenSocial Intro
OpenSocial IntroOpenSocial Intro
OpenSocial Intro
 
Introduction to Hydra
Introduction to HydraIntroduction to Hydra
Introduction to Hydra
 
Django 1.10.3 Getting started
Django 1.10.3 Getting startedDjango 1.10.3 Getting started
Django 1.10.3 Getting started
 
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API frameworkSFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
SFScon 2020 - Nikola Milisavljevic - BASE - Python REST API framework
 
How to build crud application using vue js, graphql, and hasura
How to build crud application using vue js, graphql, and hasuraHow to build crud application using vue js, graphql, and hasura
How to build crud application using vue js, graphql, and hasura
 
Lab 5-Android
Lab 5-AndroidLab 5-Android
Lab 5-Android
 
-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx-Kotlin_Camp_Unit2.pptx
-Kotlin_Camp_Unit2.pptx
 
-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx-Kotlin Camp Unit2.pptx
-Kotlin Camp Unit2.pptx
 
Android application architecture
Android application architectureAndroid application architecture
Android application architecture
 
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part OneAppcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
Appcelerator Titanium Alloy + Kinvey Collection Databinding - Part One
 
Schema-First API Design
Schema-First API DesignSchema-First API Design
Schema-First API Design
 
Cloud native programming model comparison
Cloud native programming model comparisonCloud native programming model comparison
Cloud native programming model comparison
 
Paper trail gem
Paper trail gemPaper trail gem
Paper trail gem
 
Angularjs2 presentation
Angularjs2 presentationAngularjs2 presentation
Angularjs2 presentation
 
iOS Swift application architecture
iOS Swift application architectureiOS Swift application architecture
iOS Swift application architecture
 
LearningMVCWithLINQToSQL
LearningMVCWithLINQToSQLLearningMVCWithLINQToSQL
LearningMVCWithLINQToSQL
 
Basic iOS Training with SWIFT - Part 4
Basic iOS Training with SWIFT - Part 4Basic iOS Training with SWIFT - Part 4
Basic iOS Training with SWIFT - Part 4
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

apiDoc Introduction

  • 1. APIDOCInline Documentation for RESTful web APIs Introduction with a small example. http://apidocjs.com
  • 2. What does apiDoc ? apiDoc … creates a Documentation HTML-Page … use API-Descriptions from your source code For who ? apiDoc can be used with any programming language, that allow block documentation: /** * This is a comment. */
  • 3. Where i find apiDoc ? apiDoc is Open Source and distributed over NPM and github Visit: http://apidocjs.com Special functions apiDoc … support own created Templates … includes History … compare old and new API-Method Versions … allows frontend Developer to see what changed … support Inherit … reuse of Documentation parts … extendable … you can create your own Documentation Methods
  • 4. Ok, let us begin... apiDoc is a node module, so install Node.js first: http://nodejs.org Node.js includes „npm“, a package manager for node. Install apiDoc [sudo] npm install -g apidoc We install apiDoc global, so you can access it from everywhere.
  • 5. EXAMPLE PROJECT We use a simple static JavaScript example. We ignore routing, validation or database operations. This are only small parts of apiDocs functions, for all visit http://apidocjs.com There is also a complex example: http://apidocjs.com/#example-full
  • 6. Create a new directory „my_project“. Within that dir create a file „example.js“. example.js var currentUser = { name: 'Mary' }; function getUser() { return { code: 200, data: currentUser }; } function setName(name) { if(name.length === 0) { return { code: 404, message: 'NameEmptyError' }; } currentUser.name = name; return { code: 204 }; }
  • 7. A basic doc Above „function getUser“ we place a basic documentation block /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User */ function getUser() { return { code: 200, data: currentUser }; } This 3 parameters are required! @api describes the path of the Method that clients call. @apiName is a unique Name, you can name as you want, for simplicity i prefer „Type of Method“ + „Name of Method“ e.g. {get} and „/user“ => GetUser. @apiGroup is the name of the group to which this method belongs. The group will be used for Navigation.
  • 8. By default apiDoc search for all „.jjs“ files in current directory and sub directories. You can change the includeFilter, call „apidoc -h“ for details. apiDoc created a new HTML-Page in directory „doc“. From command line run within „my_project“ directory: apidoc
  • 9. Open „doc/index.html“ in your Browser We see the rudimentary Webpage, with Navigation left and Content right. We have a group „User“ and within that group an API-Method „GET /user“
  • 10. Describe the API-Answer Now we add success return parameters /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User * * @apiSuccess {String} name The users name. */ function getUser() { return { code: 200, data: currentUser }; } apidoc Run apiDoc again, it will overwrite the „doc“ Directory
  • 11. Now we have a little more Information and see what „GET /user“ returns on a successful reply „Success 200“.
  • 12. Add a Version and example Code We add a Version (Format “major.minor.patch”, see http://semver.org for details) and we add an example code, that shows what will be return in case of success /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User * @apiVersion 0.1.0 * * @apiSuccess {String} name The users name. * * @apiSuccessExample Example data on success: * { * name: 'Paul' * } */ function getUser() { return { code: 200, data: currentUser }; }
  • 13. In the right dropdown box we see now the Version „0.1.0“ and under „Success 200“ our integrated example.
  • 14. VERSIONING If you work in a Team and some Programmer work an the API backend and some on Frontend Client, it is important to know what changed in a new Release. Especially during developing an Application, many things can change.
  • 15. An API Method change Create a new file „_apidoc.js“. Put only the documentation Block from „getUser“ in that file: _apidoc.js /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User * @apiVersion 0.1.0 * * @apiSuccess {String} name The users name. * * @apiSuccessExample Example data on success: * { * name: 'Paul' * } */ „_apidoc.js“ will be our history file and contains old documentation blocks.
  • 16. Now we change the documentation block in „example.js“ example.js /** * @api {get} /user Request User information * @apiName GetUser * @apiGroup User * @apiVersion 0.2.0 * * @apiSuccess {String} name The users name. * @apiSuccess {Number} age Calculated age from Birthday * * @apiSuccessExample Example data on success: * { * name: 'Paul', * age: 27 * } */ function getUser() { return { code: 200, data: currentUser }; }
  • 17. After call „apidoc“ and open „doc/index.html“ you see now Version „0.2.0“ with the Field „age“ and the changed example.
  • 18. And now the magic...
  • 19. Select from Dropdown „0.2.0“ the Version „0.1.0“. You see now a comparison from the 2 Versions. Green marked content indicates the Fields that are added to Version „0.2.0“.
  • 20. In the future, when you made more changes to an API-Method, simply copy and add the complete documentation block to the history file „_apidoc.js“. Leave the old blocks as they are. Then you have a Documentation where everybody can see any change of your API.
  • 21. PARAMS AND ERRORS We take now a quick look at how to add Parameters, that an API-Method need and how we return errors.
  • 22. We add now a documentation to our second function „setName“. example.js /** * @api {put} /user Change User * @apiName PutUser * @apiGroup User * @apiVersion 0.1.0 * * @apiParam {String} name New name of the user * * @apiError NameEmptyError The name was empty. Minimum of <code>1</code> character is required. */ function setName(name) { if(name.length === 0) { return { code: 404, message: 'NameEmptyError' }; } currentUser.name = name; return { code: 204 }; }
  • 23. You will see now the new „Change User“ entry in navigation and the content for the API-Method.
  • 24. Thank you and have a productive use ! For more Information visit http://apidocjs.com Author: Peter Rottmann peter@apidocjs.com