Skip to main content

webargs-sanic provides integration of webargs with Sanic applications.

Project description

webargs-sanic

webargs-sanic integrates webargs with Sanic so you can parse and validate query params, JSON bodies, forms, headers, cookies, files, and route params in async handlers.

Compatibility

Current runtime requirements:

  • Python >=3.10
  • sanic>=25.12,<26
  • webargs>=8.7,<9
  • marshmallow>=4.2,<5

Validated in this repository on:

  • Python 3.13.9
  • Python 3.14.3
  • Sanic 25.12.0
  • webargs 8.7.1
  • marshmallow 4.2.3

Installation

From PyPI:

python -m pip install webargs-sanic

From source:

git clone https://github.com/EndurantDevs/webargs-sanic.git
cd webargs-sanic
python -m pip install -e .

Optional extras:

  • .[test] installs the test tooling
  • .[dev] installs build and lint tooling
  • .[examples] installs the example app dependencies

Quickstart

from sanic import Sanic
from sanic.response import text

from webargs import fields, validate
from webargs_sanic.sanicparser import use_args


app = Sanic("demo")

hello_args = {
    "name": fields.Str(required=True, validate=validate.Length(min=3)),
}


@app.get("/")
@use_args(hello_args, location="query")
async def index(request, args):
    return text(f"Hello {args['name']}")

Class-Based Views

from sanic import Sanic
from sanic.response import json
from sanic.views import HTTPMethodView

from webargs import fields
from webargs_sanic.sanicparser import use_args, use_kwargs


app = Sanic("demo")


class EchoMethodViewUseArgs(HTTPMethodView):
    @use_args({"val": fields.Int(required=True)}, location="query")
    async def post(self, request, args):
        return json(args)


class EchoMethodViewUseKwargs(HTTPMethodView):
    @use_kwargs({"val": fields.Int(required=True)}, location="query")
    async def post(self, request, val):
        return json({"val": val})


app.add_route(EchoMethodViewUseArgs.as_view(), "/echo_method_view_use_args")
app.add_route(EchoMethodViewUseKwargs.as_view(), "/echo_method_view_use_kwargs")

Manual Parsing And JSON Errors

from sanic import Sanic
from sanic.response import json

from webargs import fields, validate
from webargs_sanic.sanicparser import HandleValidationError, parser


app = Sanic("demo")


@app.get("/echo_view_args_validated/<value>")
async def echo_use_args_validated(request, value):
    parsed = await parser.parse(
        {"value": fields.Int(required=True, validate=validate.Range(min=43))},
        request,
        location="view_args",
    )
    return json(parsed)


@app.exception(HandleValidationError)
async def handle_validation_error(request, err):
    return json(err.exc.message, status=err.status_code)

Example App

The sample CRUD app lives in examples/user_simple_storage.

Install its dependencies with:

python -m pip install -e ".[examples]"

or with:

python -m pip install -r examples/user_simple_storage/requirements.txt

Testing

This repository uses Sanic's test client and validates the modern dependency stack.

Create dedicated virtualenvs and run the suite:

python3.13 -m venv .venv313
. .venv313/bin/activate
python -m pip install --upgrade pip
python -m pip install -e ".[test]"
pytest -q
deactivate
python3.14 -m venv .venv314
. .venv314/bin/activate
python -m pip install --upgrade pip
python -m pip install -e ".[test]"
pytest -q
deactivate

License

This project is licensed under the MIT License. See LICENSE.

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

webargs_sanic-3.0.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

webargs_sanic-3.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file webargs_sanic-3.0.0.tar.gz.

File metadata

  • Download URL: webargs_sanic-3.0.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for webargs_sanic-3.0.0.tar.gz
Algorithm Hash digest
SHA256 465d4c100cde9498ab79fa430245805d4996c6a4dc77c124d4fc00023e80f3e7
MD5 e13963e50320883a093705b5db32d5eb
BLAKE2b-256 0179bf94cfa955f67c8b41cac8f16cc3172c3dd19677c72d4bcbffa646ff2915

See more details on using hashes here.

File details

Details for the file webargs_sanic-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: webargs_sanic-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for webargs_sanic-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 44e29cf67a2d1cb8a87d755473e7a91833935c318f3b757420a4b890d05986da
MD5 2e0bfa3b5e22c7ff3ca71651341da0c9
BLAKE2b-256 38b019a8e5d7e3fb97eacd479c9f8c9104a7486022a9fa5f8c2015c3637e0b2b

See more details on using hashes here.

Supported by

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