Flask-jsonapi
JSONAPI 1.0 server implementation for Flask.
Installation
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].
Documentation
Full documentation is available at: https://flask-jsonapi.readthedocs.io/.
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.
Configuration
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)
Base.metadata.create_all(db_engine)
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/')
app.run(host='127.0.0.1', port=5000)
Usage
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
Result:
{
"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
Result:
{
"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
pytest
Credits
Some parts of this project were written based on Flask-REST-JSONAPI.
Metadata
Release files for flask-jsonapi 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| flask_jsonapi-1.4.1.tar.gz | 20.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_jsonapi-1.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 58.3 kB
Release files / flask_jsonapi-1.4.1.tar.gz
| Download URL | flask_jsonapi-1.4.1.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c78a7550f846d77b511706f204e949acdba48cfaa1039f9e23db76c81a9fdf8f
|
|
BLAKE2b-256 checksum How to use checksums |
d16719cdeb70b8f79b77bce798ef5cd9302e592689ef3a9c950da21ed4fb5184
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Release files / flask_jsonapi-1.4.1-py3-none-any.whl
| Download URL | flask_jsonapi-1.4.1-py3-none-any.whl |
|---|---|
| Size | 37.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f636ab88ff2e63688bd6a292d0452a4a8216700ddee148b46389c2214056a226
|
|
BLAKE2b-256 checksum How to use checksums |
95a9d61678fab5f8db1d0413cc7467646dbe0dd573f44748746b99cd2a2b8f6f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.2
|
Security for flask-jsonapi 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.