Skip to main content

JSONAPI 1.0 implementation for Flask.

Project description


Build Status Documentation Status Coverage Status Latest Version Supported Python versions Wheel Status License

JSONAPI 1.0 server implementation for Flask.


This package requires at least python 3.7. To install run: pip install flask-jsonapi. You can install SQLAlchemy support with: pip install flask-jsonapi[sqlalchemy].


Full documentation is available at:

Simple example

Let’s create a working example of a minimal Flask application. It will expose a single resource Article as a REST endpoint with fetch/create/update/delete operations. For persistence layer, it will use an in-memory SQLite database with SQLAlchemy for storage.


import flask
import sqlalchemy
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from marshmallow_jsonapi import Schema, fields

import flask_jsonapi
from flask_jsonapi.resource_repositories import sqlalchemy_repositories

db_engine = sqlalchemy.create_engine('sqlite:///')
session = scoped_session(sessionmaker(bind=db_engine))
Base = declarative_base()
Base.query = session.query_property()

class Article(Base):
    __tablename__ = 'articles'
    id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
    title = sqlalchemy.Column(sqlalchemy.String)


class ArticleRepository(sqlalchemy_repositories.SqlAlchemyModelRepository):
    model = Article
    instance_name = 'articles'
    session = session

class ArticleSchema(Schema):
    id = fields.Int()
    title = fields.Str()

    class Meta:
        type_ = 'articles'
        strict = True

class ArticleRepositoryViewSet(flask_jsonapi.resource_repository_views.ResourceRepositoryViewSet):
    schema = ArticleSchema
    repository = ArticleRepository()

app = flask.Flask(__name__)
api = flask_jsonapi.Api(app)
api.repository(ArticleRepositoryViewSet(), 'articles', '/articles/')'', port=5000)


Create a new Article with title “First article”:

curl -H 'Content-Type: application/vnd.api+json' \
    -H 'Accept: application/vnd.api+json' \
    http://localhost:5000/articles/ \
    --data '{"data": {"attributes": {"title": "First article"}, "type": "articles"}}' \
    2>/dev/null | python -m json.tool


    "data": {
        "type": "articles",
        "id": 1,
        "attributes": {
            "title": "First article"
    "jsonapi": {
        "version": "1.0"

Get the list of Articles:

curl -H 'Accept: application/vnd.api+json' \
    http://localhost:5000/articles/ \
    2>/dev/null | python -m json.tool


    "data": [
            "type": "articles",
            "id": 1,
            "attributes": {
                "title": "First article"
    "jsonapi": {
        "version": "1.0"
    "meta": {
        "count": 1

Running tests

virtualenv -p python3.7 ~/flask-jsonapi-virtualenv
. ~/flask-jsonapi-virtualenv/bin/activate
pip install -r base_requirements.txt
pip install -U pytest==3.0.5


Some parts of this project were written based on Flask-REST-JSONAPI.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for flask-jsonapi, version 1.2.0
Filename, size File type Python version Upload date Hashes
Filename, size flask_jsonapi-1.2.0-py3-none-any.whl (34.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size flask-jsonapi-1.2.0.tar.gz (20.1 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page