Skip to main content

Parses query args in sanic using type annotations

Project description

Build Status Latest Version Python Support


Parses query args in Sanic using type annotations.


Please fill out this survey if you are using Sanicargs, we are gathering feedback :-)


Install with pip

$ pip install sanicargs


Use the parse_query_args decorator to parse query args and type cast query args and path params with Sanic’s routes or blueprints like in the example below:

import datetime
from sanic import Sanic, response
from sanicargs import parse_query_args

app = Sanic("test_sanic_app")

@app.route("/me/<id>/birthdate", methods=['GET'])
async def test_datetime(req, id: str, birthdate: datetime.datetime):
    return response.json({
        'id': id,
        'birthdate': birthdate.isoformat()

if __name__ == "__main__":"", port=8080, access_log=False, debug=False)

Test it running with

$ curl ''


  • str : ex: ?message=hello world
  • int : ex: ?age=100
  • bool : ex: ?missing=false
  • datetime.datetime : ex: ?currentdate=2017-10-30T10:10:30 or 2017-10-30
  • : ex: ?birthdate=2017-10-30
  • List[str] : ex: ?words=you,me,them,we

Note about datetimes

Dates and datetimes are parsed without timezone information giving you a “naive datetime” object. See the note on datetime.timestamp() about handling timezones if you require epoch format timestamps.

Important notice about decorators

The sequence of decorators is, as usual, important in Python.

You need to apply the parse_query_args decorator as the first one executed which means closest to the def.

request is mandatory!

You should always have request as the first argument in your function in order to use parse_query_args.

Note that request arg can be renamed and even type-annotated as long as it is the first arg.


0.0.1 (2017-01-09)

  • git init


  • added test suite
  • added path_param type casting


  • request can be renamed and even type-annotated

1.2.0 (2018-03-01)

  • added support for boolean parameters

1.3.0 (2019-01-08)

  • @wraps the inner function to keep the args signature

1.4.0 (2019-03-04)

  • Update deps to allow latest version of sanic

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 sanicargs, version 1.4.0
Filename, size File type Python version Upload date Hashes
Filename, size sanicargs-1.4.0.tar.gz (5.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page