A story about using Node.js community-built tools effectively in order to speed up the prototyping phase of your API project. In the whole article, Open API specification (which recently reached a RC on version 3) refers to version 2, also known as Swagger specification.
In this article I’ll briefly demonstrate how you can to make use of few community-driven tools which I came across recently and I definitely recommend: Typicode’s json-server and Rebilly’s Open API generator. By using a combination of them, you can quickly achieve a mock API server with really nice and useful documentation based in short deadlines.
I think it’s better to start with the specification as it answers many questions, even before making any real or fake data for the API.
The official documentation is sufficient to get started. There’s really no need for detailed tutorial here on how to use the tool in order to make a working project — just follow the steps. (And maybe push an empty commit on the gh-pages
branch before first deploy just to make sure)
More importantly, you have to know why this generator is good and why I recommend it 😌
When you follow the steps correctly, you’ll end up with a simplified version of RebillyAPI repository. That’s the final outcome that’ll motivate you to try the generator now, if you haven’t already done so 😊
Probably you might find other similar tools such as spectacle, however this tool worked really well for me and it covers everything I think one might need during a prototyping phase of an API project.
Thus, you have a tool to develop an Open API specification and deploy its documentation to github pages, making it nice and accessible for the public. With this Open API specification, you can later generate server/client code with Codegen, or just deploy the spec to services such as Cloud Endpoints or any other that will soon come with similar integration.
Basically, there are many opportunities available for you when you have the specification. Next step is to generate a server with mock data which matches the specification’s “contract” with the documentation or just any front-end app meant to work with the specification.
The JSON server tool is not really a generator per se, however it’s easy to integrate it as such in your toolchain. What I mean is, the tool is based on express.js and its popular middleware. You won’t need the express generator as you focus on the server. The endpoints are automatically discovered out of JSON files and you do not need express’ sophisticated templates views, routes, etc.
A simple function in a script run by a npm task can easily do for having a generator for the server part. Also, faker plays really well with this “generator” function. (no word play intended here) Check an example.
The main ideas you should understand with JSON server:
Also, another benefit is that the server can be easily deployed. For example, if you decide to deploy on heroku, you’ll only need to update your server to use process.env.IP
and process.env.PORT
. The documentation points to the jsonplaceholder example, but surely hook.io or a similar cloud service will work just fine. Just keep it simple and working — the main idea of using this tool is to move fast and effectively.
Another option for generating a server on top of an existing specification is the swagger-server package which also integrates with express.js, however does not necessarily provide the ready filters, sorts, etc. that json-server does.
I personally separated both tools in 2 repositories for the following reasons:
That was a brief article sharing some love about open source projects which deserve an applause for being simple and effective helpers for developers.
If this article gets any positive feedback, I will make a follow-up story to this one showing few tips to improve the workflow of editing and publishing specification to documentation to a working API.
At any case, the search of more effective and well-integrated API prototyping, documentation and deployment continues.