How to develop and design a good API interface?
Clear function
At the beginning of design, it is necessary to sort out the detailed functions of the API, divide it by business function points or modules, and make clear what functions the API needs to provide.
The code logic is clear.
Keep the code clean and tidy, add necessary comments, and ensure the single function of the interface. If an interface needs complex business logic, it is suggested to split it into multiple interfaces or package the functions into public methods independently to avoid too much code in the interfaces, which is not conducive to post-maintenance and post-iteration.
Necessary safety inspection mechanism
At present, Web applications are prone to data theft, tampering, illegal submission, repeated requests and other security problems, and the security verification mechanism of API is essential. The common solution is to add a signature to each HTTP request in the form of digital signature, and the server side verifies the legality of the signature to ensure the legality of the request.
Log record
In order to locate the problem in time, the log is essential.
Reduce coupling degree
A good API should be as simple as possible. If the business coupling between APIs is too high, it is easy to make the related APIs unavailable because of a code exception, and try to avoid the complicated calling relationship between APIs.
Returns a meaningful status code
The data returned by API should carry status code data, such as 200 for normal request and 500 for internal server error. Returning the common status code is helpful for problem location. For example, you can refer to the following status codes:
Development document
Because the API is provided for third parties or internal use, it is essential to develop documentation, otherwise others will not know how to call it. A good API development document should include the following elements:
1, explanation of current API architecture mode, development tools and versions, system laziness and other environmental information;
2. What functions does the current API provide?
3. Inertia relationship between 3.API modules;
4. Call rules and precautions;
5, deployment considerations, etc.
A good API must be easy to use, easy to understand, easy to expand, not easy to misuse, high security and powerful. It is not easy to do the above, but we should follow the above principles and design the API according to the reasonable division of the business itself.