Hello! This article presents a simple API starter that might help beginners to understand better the API concept. The codebase can be downloaded from Github and used for eLearning activities or production. The framework that powers the API is Flask, a leading software library actively supported and versioned by many
open-source enthusiasts. Thanks for reading!
✨ Starter Features
- 👉 Simple API over a minimal
SQLite Persistencemanaged by an elegant ORM (SqlAlchemy)
Powerful APIcore provided by Flask-RestX
Strong Input validation
||GET||return all items||✔️|
|POST||create a new item||✔️|
||GET||return one item||✔️|
✨ API Coding & Implementation Rules
- Simple Interface
- Consistent, intuitive actions
- Strong Input Validation
✨ Codebase Structure
All relevant files are listed below. Other files like
docker-compose.yml, README, LICENSE are omitted.
The information managed by the API is saved using a simple table defined with three fields:
date_created. Here is the source code:
The source code provides a few helpers that make our life, as a developer, easier:
update_data- update the
save- save & commit the updates of the current object
toJSON- returns the JSON representation
Each method is kept as simple as possible but at the same time, provides robust validation and elegant SQL access.
For instance, the route that manages the update operation for an
Let's iterate over the relevant lines:
@rest_api.route('/api/datas/<int:id>')defines the route
Flask will route the request to this section when users access
/api/datas/1 for instance.
This decorator triggers a validation previously defined as bellow:
data field has a size over 255, the request is rejected. For us, as developers, the coding effort is minimal.
✨ Where to go from here
This simple API will be extended with more features soon:
- Add more fields to
- Implement authentication
updateactions to authenticated users.
For more resources, feel free to access: