Connecting Xano App to a SOAP API Using JavaScript with Lambdas

I've been having some challenges connecting my Xano app to a SOAP API that I need to use. After some research and considering various third-party services like Make, I realized that this can be achieved using JavaScript within an AWS Lambda function.

Here’s a simple example of how you can set up a Lambda function to interact with a SOAP API. In this case, I’m using the SOAP API provided by Postman:

Lambda Function Code:

async function fetchNumberToWords() {
  const url = "https://www.dataaccess.com/webservicesserver/NumberConversion.wso";
  const soapRequest = `<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
      <NumberToWords xmlns="http://www.dataaccess.com/webservicesserver/">
        <ubiNum>500</ubiNum>
      </NumberToWords>
    </soap:Body>
  </soap:Envelope>`;

  try {
    const response = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "text/xml",
        "SOAPAction": "http://www.dataaccess.com/webservicesserver/NumberToWords",
      },
      body: soapRequest,
    });

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }

    const data = await response.text();
    console.log("Response:", data);
    return data; // return the result after the fetch completes
  } catch (error) {
    console.error("Error:", error);
    return "Error occurred";
  }
}

// Usage example
return await fetchNumberToWords();

Explanation:

  1. SOAP Request Setup: We create a SOAP request payload to send to the API.

  2. Fetch API Call: The fetch function sends a POST request to the SOAP API with the necessary headers and body.

  3. Error Handling: Checks if the response is OK and handles any errors that might occur.

Response Example:

The lambda function will return a response similar to this:

<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <m:NumberToWordsResponse xmlns:m="http://www.dataaccess.com/webservicesserver/"> <m:NumberToWordsResult>five hundred</m:NumberToWordsResult> </m:NumberToWordsResponse> </soap:Body> </soap:Envelope>

Notes:

  • Ensure you configure your Lambda function to handle the parameters and responses appropriately for your API integration.

  • While this example uses fetch, you can achieve the same result using Axios if you prefer.

Hope this helps!

Working with APIs
1
5 replies