title: AWS - API Gateway - Lambda handles callback
body: <p>I got &quot;Internal server error&quot; Status: 502 on AWS API Gateway. Searched solutions this post says need to handle callback with a callback function. Would anyone please advise how to incorporate the callback codes with my AWS-Lambda function below? Thank you so much.</p>
  <p><a href="https://stackoverflow.com/questions/47672377/message-internal-server-error-when-try-to-access-aws-gateway-api">message: &quot;Internal server error&quot; when try to access aws gateway api</a></p>
  <pre><code>callback(null, {
      statusCode: 200,
      body: JSON.stringify(message),
      headers: {'Content-Type': 'application/json'}
  });
  </code></pre>
  <p>My AWS-Lambda function</p>
  <pre><code>'use strict';
  const AWS = require('aws-sdk');
  
  exports.handler = async (event, context) =&gt; {
      const documentClient = new AWS.DynamoDB.documentClient();
  
      let responseBody = &quot;&quot;;
      let statusCode = 0;
  
      const {
          id,
          productname
      } = JSON.parse(event.body);
  
      const params = {
          TableName: &quot;Products&quot;,
          Item: {
              id: id,
              productname: productname
          }
      };
  
      try {
          const data = await documentClient.put(params).promise();
          responseBody = JSON.stringify(data);
          statusCode = 201;
      } catch (err) {
          responseBody = `Unable to put product: ${err}`;
          statusCode = 403;
      }
  
      const response = {
          statusCode: statusCode,
          headers: {
              &quot;Content-Type&quot;: &quot;application/json&quot;
          },
          body: responseBody
      };
      return response
  };
  </code></pre>