RESTful API development: Provide lightweight JSON interface for App/mini-program
📂 Stage: Stage 5 - Advanced Advancement (Performance and Architecture) 🔗 Related chapters: Flask 上下文深挖 · 数据验证
0. Why choose REST + Flask?
Today's front-end and back-end separation architecture has become mainstream: whether it is a mobile app, a WeChat applet, or a Vue/React single-page application, a set of unified, stateless, and resource-centered back-end interfaces is needed. The RESTful style just meets these requirements, and the lightweight and flexible nature of Flask allows us to quickly build a standardized interface prototype and then smoothly iterate to production use.
This article will take you step by step through a complete "Article Management API" case to master the core skills of developing RESTful JSON interfaces with Flask.
1. RESTful design core: resources + status code
1.1 URL can only be "resource noun"
The only task of the URL is to identify the resource, and there should be no action words such as "get, create, delete" in it - these operations are all expressed by HTTP methods.
💡 A few practical details:
- For unified use
/apiPrefix to easily distinguish back-end interfaces and front-end routes. - Use plural form for resource names (
articlesinstead ofarticle), because a list is a collection of resources. - No more than 3 layers (e.g.
/api/articles/42/commentsNo problem, but/api/users/1/categories/5/articlesIt is too complicated, then you can use query parameters or reverse correlation interface instead).
1.2 Accurate use of HTTP status codes
Status code is a response mark that can be read by both humans and machines. Never return it in all situations.200or500. Here are some of the most commonly used ones in RESTful development:
2. Use Flask to implement basic article management API
It is assumed here that the project already has the foundation of user authentication (Flask-Login), database (SQLAlchemy) and form validation (Flask-WTF or Marshmallow). This article only focuses on the core code of the API part.
2.1 First create the API blueprint
Using blueprints to separate interfaces from page routing and templates is more in line with modular development habits:
2.2 Complete article CRUD interface
Below is the full implementation of the article management API, including paginated lists, details, creation, update and deletion.
3. Unified API error handling
In the above code, we directly useabort()and manual return status codes. But Flask's default error response is in HTML format, which is very unfriendly to the API. We need to register a specialized JSON error handler with the API blueprint.
💡 Don’t forget toapp/api/__init__.pyImport the error handling module here, otherwise these processors will not take effect:
4. Quick check on core points
4.1 Commonly used Flask API shortcut methods
4.2 Guide to avoid pitfalls in RESTful design
- You can add the version number as a prefix: For example
/api/v1/articles, which will not affect the old version of the client when upgrading the interface. - Don’t just use GET and POST: choose the appropriate HTTP method (GET/POST/PUT/DELETE/PATCH) based on the operation.
- Never return HTML to the API: All responses should be JSON.
- Uniform Time Format: It is recommended to use ISO8601 (for example
2024-05-20T12:34:56Zor local time with time zone). - Paging, sorting, and filtering are all placed in query parameters: For example
?page=1&sort=created_at。 - 204 must be returned after successful deletion, and the response body cannot be included.
5. Extension direction
If you want to upgrade this API into a truly production-ready service, you can continue to learn the following content:
- Interface document automatically generated: use
Flask-RESTXorFlask-APISpecGenerate Swagger documentation directly. - JWT token authentication: Flask-Login relies on Session and is suitable for the Web, while mobile Apps and small programs are more suitable for use.
Flask-JWT-ExtendedImplement stateless authentication. - Interface current limit: passed
Flask-LimiterPrevent malicious brush requests. - Data validation optimization: use
MarshmallowReplaces Flask-WTF, which is more focused on serialization/deserialization of APIs and does not require CSRF protection. - API Cache: use
Flask-CachingCache high-frequency queries (such as article lists) to reduce database pressure.
🔗 Extended reading

