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

class pylambdarest.Request(event: dict)

Request objects created in @route handlers, and accessible from within the handler as the “request” argument. It contains parsed information about the API Gateway event.

Parameters:event (dict) – The API Gateway event.
body

Return the raw body field of the API Gateway event.

Examples

>>> event = {"body": '{"name": "John Doe"}'}
>>> Request(event).body
'{"name": "John Doe"}'
headers

Return the request’s headers.

Examples

>>> event = {"headers": {"accept": "*/*"}}
>>> Request(event).headers
{'accept': '*/*'}
json

Return a dict parsed from the body field of the API Gateway event.

Examples

>>> event = {"body": '{"name": "John Doe"}'}
>>> Request(event).json["name"]
'John Doe'
method

Return the request’s HTTP method.

Examples

>>> event = {"httpMethod": "GET"}
>>> Request(event).method
'GET'
path_params

Return the pathParameters field of the API Gateway event.

Examples

>>> event = {"pathParameters": {"user_id": 123}}
>>> Request(event).path_params
{'user_id': 123}
query_params

Return the queryStringParameters field of the API Gateway event.

Examples

>>> event = {"queryStringParameters": {"page": "3"}}
>>> Request(event).query_params
{'page': '3'}

Response

class pylambdarest.Response(code: int, body: Optional[Any] = None, headers: Optional[dict] = None)

Internal Response object created in a @route handler.

Response objects are responsible for the formatting of the response to the expected Lambda response format.

Parameters:
  • code (int) – Response HTTP status code.
  • body (Any) – Response body. Must be JSON serializable.
  • headers (dict) – Response headers.

Examples

>>> response = Response(200, {"message": "Hello From pylamndarest!"})
>>> response.format()
{'statusCode': 200, 'body': '{"message": "Hello John Doe !"}'}
format() → Dict[str, Any]

Format the Response instance to the expected Lambda response format.

Examples

>>> res = Response(200, {"message": "Hello from pylambdarest !"})
>>> res.format()
{'statusCode': 200, 'body': '{"message": "Hello from pylambdarest !"}'}