Having spent the better part of the last year working on one project that is completely reliant on a third-party API. As well as another project, that is heavily reliant on several third-party APIs, I have developed some opinions on the matter that, in my own journey to improve as a developer has inspired some of my own behaviour.
In particular, one of the mentioned third-party APIs has been somewhat difficult to work with due to the fact that:
As a result of the above, after some searching, I came across Swagger and the concept of API-First design and development. As I see them, the benefits of designing (and sharing) the Swagger document up front are as follows:
With these in mind, and with the goal of improving my own development skills and habits, I set about with the following goals when starting to build my example Itemly todo API:
I write my .yml in VS code (in order to better learn the structure)
While writing, I run the file through the swagger-cli in order to validate as I go.
npm i -g swagger-cli
swagger-cli validate swagger.yml
Once ready, or when completing endpoints, I will paste the spec into the online Swagger Editor for validation (the validation and error messages are pretty good here)
Looking at the API writing process above, admittedly, it is not particularly efficient. Some areas for improvement are:
swagger-cli validate
error messages, while useful, do not include line numbers - hence the pasting into the Swagger Editor. Another area I am currently looking into is how to effectively mock the API - either by using a tool such as WireMock or Mock Server, or by finding a tool to which I can pass the Swagger document and it will generate the mocks for me - something like Swagger Hub, PostMan or Stoplight might be candidates in this area. However, I will share more on this in a future post!