Welcome to Flask-REST-JSONAPI documentation !¶
Flask-REST-JSONAPI is a library to help you build REST apis. It is built around:
- jsonapi: a specification for building apis in json
- flask: a microframework for Python based on Werkzeug
- marshmallow-jsonapi: JSON API 1.0 formatting with marshmallow
- sqlalchemy: SQLAlchemy is the Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.
- mongodb: a free and open-source cross-platform document-oriented database program
I have created this library because I was looking for the best way to implement a REST api. The jsonapi specification is a very strong specification about interactions between the api and the caller and I think it is a very good one.
There is a lot of very good REST libraries based on Flask like Flask-RESTful or Flask-Restless but I would like to combine the flexibility of Flask-RESTful with the simplicity of Flask-Restless and the power of Marshmallow and SQLAlchemy around a strong and shareable communication protocol: jsonapi.
Moreover, most Flask frameworks only support SQLAlchemy so I would like to create an generic abstraction to communicate with any data provider: the data layer system. Current available data layers are:
- SQLAlchemy
- MongoDB
You can easily create and use your own data layer to communicate with the data provider of your choice. Read the data layer section to learn more.
Here is a quick example:
from flask import Flask
from flask_rest_jsonapi import Api, ResourceDetail
app = Flask(__name__)
api = Api(app)
class HelloWorld(ResourceDetail):
def get(self):
return "Hello world"
api.detail_route('index', '/', resource_cls=HelloWorld)
if __name__ == '__main__':
app.run(debug=True)
Save this file as api.py
Launch local server:
$ python api.py
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
Now you can try this:
$ curl "http://127.0.0.1:5000/" -H "Content-Type: application/vnd.api+json"\
-H "Accept: application/vnd.api+json"
"Hello world"
Note
All code examples in this tutorial are based on a classic blog example with topic and post.
Contents¶
- Installation
- Resource
- Data layer
- Sorting
- Fields restriction
- Filtering
- Pagination
- Routing
- Tutorial
- Initialize flask application, API and database
- Define and initialize models
- Define marshamllow-jsonapi schemas
- Register resources and routes
- List topics
- Create topic
- Update topic
- Delete topic
- Create author
- Create post with an author in a topic
- List posts of topic 1
- List posts of author 1 (John Smith)