Skip to main content

The library for writing RESTful APIs with sanic

Project description


PyPI version CodeFactor codebeat badge Codacy Badge Coverage Status

Simple library for creating RESTful APIs with sanic


  • simplicity
  • schematics integration
  • exceptions handling
  • unified response format
  • 100% coverage


python3 -m pip install sanic-restful-resources


users = [{'name': 'Michael'}, {'name': 'Ivan'}]

class UserPostSchema(Model):
    name = StringType(required=True)

class Users:
    async def get(self, request):
        return users

    async def post(self, request, user_data):
        return '', 204

More examples in this folder.


  • resource(uri='') - return decorator for classes that will be considered RESTful resources. This decorator automatically extends HTTPMethodView (refer to sanic documentation for details). You can provide resource URI through decorator or by class attribute uri. You can provide decorators for all the methods through class attribute decorators.

    Examples of possible return values for handlers:

    return "data", 200, {"X-Custom-Header": "Value"}
    return "data", 200
    return "data"
    return {"arg": "val"}
    return ["val1", "val2"]
    return "", 201
    return sanic.response.json # .text, .html, e.t.c.
  • Api(name='API', url_prefix) - class for aggregating resources and registrating them in the sanic application. Internally it uses blueprints. Basic workflow:

    from sanic import Sanic
    from resources import User, Users
    app = Sanic()
    api = Api(url_prefix='/api/v1')
    # ...
  • validate(**models) - decorator for methods, that will validate incoming data with provided models from Schematics library. Successfully validated and parsed models will be passed as keyword arguments to the handler method. If any model fails to validate - handler will not be called.

  • error(description=None, details=None, status=400, **kwargs - method for returning errors from handlers.

  • collect_args(request) - method for getting data from all possible sources of data in the request.

Project details

Download files

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

Source Distribution

sanic_restful_resources-0.2.0.tar.gz (5.1 kB view hashes)

Uploaded source

Built Distribution

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page