API Reference

route

class pylambdarest.route(body_schema: Optional[dict] = None, query_params_schema: Optional[dict] = None)

Lambda handler decorator.

The @route decorator automatically format the response from an handler into the expected API Gateway + Lambda format. It also parses API Gateway’s events into a Request object available as a “request” argument, and optionally provides body and query string parameters schema validation.

Parameters:
  • body_schema (dict) – Optional jsonschema for the request’s body. If the body does not match the provided body_schema, the request will result in a 400 code. For jsonschema specification, please see here.
  • query_params_schema (dict) – Optional jsonschema for the query string parameters. If the query string parameters does not match the provided query_params_schema, the request will result in a 400 code.

Examples

Define a ‘hello’ handler with body schema. A request argument (Request object) can be used in the handler.

>>> user_schema = {
...     "type": "object",
...     "properties": {
...         "name": {"type": "string"}
...     },
...     "required": ["name"],
...     "additionalProperties": False
... }
>>> @route(body_schema=user_schema)
... def hello(request):
...     name = request.json["name"]
...     return 200, {"message" : f"Hello {name} !"}
>>> hello({"body": '{"name": "John Doe"}'}, {})
{'statusCode': 200, 'body': '{"message": "Hello John Doe !"}'}

If the request’s body does not match the schema, a 400 will be sent.

>>> hello({"body": '{}'}, {})
{'statusCode': 400, 'body': '"'name' is a required property"'}

You can define a query_params_schema in the same way as body_schema for query string parameters validation.

Request

Response