is a tiny helper to create a response object for AWS Lambda with Proxy integration

Usage | Annotated source | License

NPM version No deps JavaScript Style Guide KLP


In some AWS Lambda, triggered by an API Gateway endpoint and configured with Lambda Proxy integration, suppose you have a dummy endpoint which returns JSON { "ok": true }, then the following code will be enough

const response = require('aws-lambda-res')

function handler (event, context, callback) {
  callback(null, response(200)({ ok: true }))

exports.handler = handler

You can pass headers as second argument, body can be null: for example to logout and redirect to homepage you can use something like

function handler (event, context, callback) {
  const Expires = 'Sat, 01 Jan 2000 00:00:00 GMT' // Some day in the past.

  callback(null, response(302)(null, {
    'Location': '',
    'set-cookie': `user_authenticated=;; Expires=${Expires}`

exports.handler = handler

Annotated source

// This code is generated by command: npm run markdown2code

Lambda Proxy integration needs a response with the following properties:

 * Create a response helper.
 * @params {Number} statusCode
 * @returns {Function} awsLambdaResponse helper

function response (statusCode) {

The exported response function, requires statusCode argument and returns an helper function which accepts parameters:

  1. body: can be any data, even null
  2. headers, defaults to { 'Content-type': 'application/json' }
   * AWS Lambda response helper.
   * @params {Object|null} body
   * @params {Object} [headers]
   * @returns {Object} responseObj required by AWS Lambda Proxy integration

  function awsLambdaResponse (body, headers) {
    body = body === null ? null : JSON.stringify(body)
    headers = typeof headers === 'object' ? headers : { 'Content-Type': 'application/json' }

    const responseObj = {
      isBase64Encoded: false,

    return responseObj

Return helper and export function

  return awsLambdaResponse

module.exports = response