{"library":"middy","title":"Middy.js Middleware Engine for AWS Lambda","description":"Middy is a Node.js middleware engine specifically designed for AWS Lambda functions, bringing an Express.js-like middleware pattern to serverless environments. It allows developers to encapsulate common logic such as input parsing, validation, authentication, and error handling into reusable middleware functions, enhancing code modularity and maintainability for Lambda handlers. The current stable version series is 7.x, with recent releases like 7.3.1 focusing on improvements, security fixes, and compatibility. Previously, there was a significant breaking change from the 0.x series to 1.x, and subsequently to the 7.x series, requiring migration for older projects. Middy provides a rich ecosystem of official and third-party middlewares for common Lambda patterns, differentiating itself by its focus on simplicity, performance, and a clear execution lifecycle for `before`, `after`, and `onError` hooks.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install middy"],"cli":null},"imports":["import middy from '@middy/core'","import jsonBodyParser from '@middy/http-json-body-parser'","import validator from '@middy/validator'","import httpErrorHandler from '@middy/http-error-handler'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import middy from '@middy/core';\nimport jsonBodyParser from '@middy/http-json-body-parser';\nimport httpErrorHandler from '@middy/http-error-handler';\nimport validator from '@middy/validator';\n\n// Define your AWS Lambda handler function\nconst paymentHandler = async (event, context) => {\n  // Assume event.body is already parsed by jsonBodyParser\n  const { amount, currency, token } = event.body;\n\n  if (!amount || !currency || !token) {\n    throw new Error('Missing required payment details.');\n  }\n\n  // Simulate payment processing\n  console.log(`Processing payment for ${amount} ${currency} with token ${token}`);\n  const paymentId = `pay_${Date.now()}`;\n\n  return {\n    statusCode: 200,\n    headers: { 'Content-Type': 'application/json' },\n    body: JSON.stringify({ message: 'Payment processed successfully', paymentId })\n  };\n};\n\nconst inputSchema = {\n  type: 'object',\n  properties: {\n    body: {\n      type: 'object',\n      properties: {\n        amount: { type: 'number', minimum: 0.01 },\n        currency: { type: 'string', pattern: '^[A-Z]{3}$' },\n        token: { type: 'string' }\n      },\n      required: ['amount', 'currency', 'token']\n    }\n  },\n  required: ['body']\n};\n\n// Wrap your handler with middy and apply middlewares\nexport const handler = middy(paymentHandler)\n  .use(jsonBodyParser())\n  .use(validator({ inputSchema }))\n  .use(httpErrorHandler()); // Must be the last middleware for error catching","lang":"typescript","description":"This quickstart demonstrates how to set up an AWS Lambda handler with Middy.js, applying `jsonBodyParser` for automatic JSON parsing, `validator` for schema validation, and `httpErrorHandler` for structured error responses. It showcases the ESM import style for Middy 7.x and basic middleware chaining.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}