Skip to main content

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.

Release files for webargs-sanic 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for webargs-sanic 3.0.0
File Size Uploaded
webargs_sanic-3.0.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for webargs-sanic 3.0.0
File Interpreter ABI Platform
webargs_sanic-3.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.5 kB

Release files / webargs_sanic-3.0.0.tar.gz

Download URL webargs_sanic-3.0.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
465d4c100cde9498ab79fa430245805d4996c6a4dc77c124d4fc00023e80f3e7
BLAKE2b-256 checksum
How to use checksums
0179bf94cfa955f67c8b41cac8f16cc3172c3dd19677c72d4bcbffa646ff2915
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release files / webargs_sanic-3.0.0-py3-none-any.whl

Download URL webargs_sanic-3.0.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
44e29cf67a2d1cb8a87d755473e7a91833935c318f3b757420a4b890d05986da
BLAKE2b-256 checksum
How to use checksums
38b019a8e5d7e3fb97eacd479c9f8c9104a7486022a9fa5f8c2015c3637e0b2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.3

Release history Release notifications | RSS feed

This release

3.0.0 This release

2 release files

2.3.5

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.1

1 release file

2.2.0

2 release files

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.5.0

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

0.1.1

1 release file

0.1.0

1 release file

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.5

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page