Understanding SOAP API and Implementing it with Node.js: A Practical Tutorial

Dev Balaji
5 min readJun 13


SOAP (Simple Object Access Protocol) is a protocol for exchanging tructured information in web services using XML (eXtensible Markup Language). It allows applications running on different platforms to communicate with each other over a network. SOAP is based on a request-response model, where the client sends a request to a server, and the server responds with the requested information.

To implement a SOAP API, you typically follow these steps:

  1. Define the SOAP service: This involves defining the operations or methods that the service provides, the input parameters for each operation, and the output or response format.
  2. Create a WSDL (Web Services Description Language) document: WSDL is an XML-based file that describes the SOAP web service. It specifies the location of the service, the operations it supports, the expected input parameters, and the format of the responses.
  3. Generate server-side code or use an existing framework: Depending on the programming language or framework you are using, you may generate server-side code based on the WSDL or utilize existing libraries or frameworks that handle the SOAP implementation details.
  4. Implement the server-side logic: Write the code to handle the incoming SOAP requests, process the requested operations, and generate the appropriate responses.
  5. Provide the WSDL to clients: Clients need the WSDL document to understand the service’s capabilities, the required input parameters, and the expected response format.
  6. Generate client-side code or use an existing library: Clients can generate client-side code based on the WSDL or use existing SOAP client libraries or frameworks that handle the SOAP communication and data parsing.
  7. Implement client-side logic: Use the generated or existing client-side code to send SOAP requests to the server and handle the responses.

We’ll use a fictional example of a weather service that provides weather forecasts based on location.

Step 1: Install the soap library

npm install soap

Step 2: Define the WSDL Create a file named weatherService.wsdl and define the WSDL for the SOAP service. The WSDL describes the operations and message formats. Here's an example:

<definitions name="WeatherService"

<xsd:element name="GetWeatherRequest">
<xsd:element name="location" type="xsd:string"/>
<xsd:element name="GetWeatherResponse">
<xsd:element name="temperature" type="xsd:string"/>
<xsd:element name="description" type="xsd:string"/>

<message name="GetWeatherRequestMessage">
<part name="request" element="tns:GetWeatherRequest"/>
<message name="GetWeatherResponseMessage">
<part name="response" element="tns:GetWeatherResponse"/>

<portType name="WeatherPortType">
<operation name="GetWeather">
<input message="tns:GetWeatherRequestMessage"/>
<output message="tns:GetWeatherResponseMessage"/>

<binding name="WeatherBinding" type="tns:WeatherPortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="GetWeather">
<soap:operation soapAction="http://example.com/weather/GetWeather"/>
<soap:body use="encoded" namespace="http://example.com/weather"
<soap:body use="encoded" namespace="http://example.com/weather"

<service name="WeatherService">
<port name="WeatherPort" binding="tns:WeatherBinding">
<soap:address location="http://example.com/weather"/>


In this example, we define the GetWeather operation that takes a location parameter and returns the temperature and description of the weather.

Step 3: Generate server-side code Create a file named server.js and implement the server-side code using the soap library. Here's an example:

const soap = require('soap');

// Define the service implementation
const service = {
WeatherService: {
WeatherPort: {
GetWeather: function(args, callback) {
// Perform the logic to get the weather based on the location
const location = args.location;
const temperature = '25°C';
const description = 'Sunny';

// Return the response
const result = {
temperature: temperature,
description: description
callback(null, result);

// Create the SOAP server
const xml = require('fs').readFileSync('weatherService.wsdl', 'utf8');
const server = soap.listen({ path: '/weather', xml: xml }, function() {
console.log('SOAP server running at http://localhost:8000/weather?wsdl');

// Attach the service implementation to the SOAP server
server.addService(xml, service, { suppressStack: true });

n this example, we define the GetWeather method as the server implementation. It receives the location parameter, performs the logic to get the weather data, and returns the result using the provided callback.

Step 4: Start the SOAP server Run the server script by executing node server.js. The SOAP server will start listening on http://localhost:8000/weather and provide the WSDL at http://localhost:8000/weather?wsdl.

Step 5: Generate client-side code Create a file named client.js to implement the client-side code using the soap library. Here's an example:

const soap = require('soap');

// Create the SOAP client
const url = 'http://localhost:8000/weather?wsdl';
soap.createClient(url, function(err, client) {
if (err) {
console.error('Error creating SOAP client:', err);

// Make a SOAP request
const args = { location: 'New York' };
client.GetWeather(args, function(err, result) {
if (err) {
console.error('Error making SOAP request:', err);

// Handle the SOAP response
console.log('Temperature:', result.temperature);
console.log('Description:', result.description);

In this example, we create a SOAP client using the provided WSDL URL. We then call the GetWeather method on the client, passing the location parameter. The response is handled in the callback function.

Step 6: Run the SOAP client Execute node client.js to run the client script. It will send a SOAP request to the server, and the response will be logged to the console.

That’s it! You have now implemented a SOAP API using Node.js and the soap library.

What is important.. ??

When writing a SOAP API, consider the following parameters:

  1. Endpoint URL: The URL where the SOAP service is hosted.
  2. SOAP headers: Some SOAP APIs require additional headers to be included in the SOAP request, such as authentication tokens or session IDs.
  3. Operation name: The name of the operation you want to invoke on the SOAP service.
  4. Input parameters: The input data required for the operation, provided as an object or XML document.
  5. Callback function: A function to handle the response or any errors that occur during the SOAP request.

Note that the exact implementation details may vary depending on the specific SOAP library or framework you are using.

How different is SOAP API’s from other API’s …!!

SOAP APIs have some unique characteristics compared to other APIs:

  1. XML-based messaging: SOAP messages are sent as XML, which provides a structured format for the data being exchanged.
  2. Formal contract: The WSDL document defines the contract between the service and its clients, making it explicit and providing a standardized way to interact with the API.
  3. Protocol independence: SOAP can be used over various protocols, including HTTP, SMTP, and more, allowing flexibility in communication.


Popular libraries and frameworks to work with SOAP API in node

There are several SOAP libraries and frameworks available for Node.js that can help you implement SOAP APIs. Here are some popular options:

  1. soap: The soap package is a widely used library for working with SOAP in Node.js. It provides a simple API for creating SOAP clients and servers, handling SOAP requests and responses, and parsing XML. You can find it on npm: https://www.npmjs.com/package/soap
  2. strong-soap: strong-soap is a powerful SOAP library for Node.js. It supports both client and server implementations, provides extensive features for working with SOAP headers, attachments, and security, and offers WSDL parsing capabilities. You can find it on npm: https://www.npmjs.com/package/strong-soap
  3. node-soap: node-soap is another popular SOAP library for Node.js. It offers a simple API for creating SOAP clients and servers, handling SOAP requests and responses, and working with WSDL files. You can find it on npm: https://www.npmjs.com/package/soap
  4. easy-soap-request: easy-soap-request is a lightweight SOAP client library for Node.js. It focuses on simplicity and ease of use, allowing you to make SOAP requests without requiring a WSDL file. You can find it on npm: https://www.npmjs.com/package/easy-soap-request

These libraries provide different features and levels of abstraction, so you can choose the one that best suits your requirements and preferences. Make sure to check their documentation and examples to understand how to use them effectively in your Node.js applications.



Dev Balaji

#Passionate learner #Mastering Js and Framework #I talk about hidden tips and tricks on technologies #Aspiring Blogger