Python > Working with External Resources > APIs (Application Programming Interfaces) > SOAP APIs
Accessing a SOAP API with Suds
This snippet demonstrates how to interact with a SOAP API using the suds
library in Python. SOAP (Simple Object Access Protocol) is a messaging protocol that allows applications to communicate over a network. This example will guide you through installing the necessary library, making a request to a SOAP API, and handling the response. Keep in mind that suds
is an older library and might not be compatible with all SOAP services. Consider using zeep
for more modern SOAP implementations.
Prerequisites
Before running this code, you need to install the suds
library. You can install it using pip:
pip install suds-jurko
Note: We are using suds-jurko
because the original suds
is no longer maintained.
Code Implementation
This code snippet first imports the Client
class from the suds.client
module. Then, it creates a Client
object using the URL of the WSDL file. The WSDL file describes the services provided by the SOAP API. We then call the Add
method of the service, passing in two integers as arguments. Finally, the result is printed to the console.
Error handling is included within a try...except
block to catch potential exceptions that might occur during the API call (e.g., network errors, invalid WSDL format, incorrect method parameters).
from suds.client import Client
# The URL of the SOAP WSDL (Web Services Description Language) file.
# Replace this with the actual URL of the WSDL for the SOAP API you want to use.
wsdl_url = 'http://www.dneonline.com/calculator.asmx?WSDL'
try:
# Create a Suds client instance.
client = Client(wsdl_url)
# Print the methods available in the SOAP service. (Optional).
# print(client)
# Call the 'Add' method of the SOAP service.
# Replace 5 and 3 with the actual numbers you want to add.
result = client.service.Add(intA=5, intB=3)
# Print the result.
print(f'The result of 5 + 3 is: {result}')
except Exception as e:
print(f'An error occurred: {e}')
Explanation of Key Components
suds.client.Client
class. It handles the communication with the SOAP service.client.service.Add
) triggers the execution of the corresponding operation on the SOAP service. The arguments are passed as keyword arguments (e.g., intA=5, intB=3
).
Real-Life Use Case
Imagine you are building an e-commerce application and need to integrate with a third-party payment gateway that exposes a SOAP API. You could use suds
(or a more modern alternative like zeep
) to make requests to the payment gateway's API, passing in transaction details such as the amount, currency, and credit card information. The API would then process the payment and return a response indicating whether the transaction was successful.
Best Practices
suds
client from creating a connection to the service.zeep
library instead of suds
, as it is actively maintained and offers better support for modern SOAP features.
When to use SOAP APIs
SOAP APIs are often used in enterprise environments where strict standards and security are paramount. They are suitable for applications requiring strong data typing, transaction management, and security features like WS-Security.
Alternatives
requests
can be used to interact with REST APIs.
Pros and Cons of Using SOAP
Pros:
Cons:
FAQ
-
What is a WSDL file?
A WSDL (Web Services Description Language) file is an XML document that describes the services offered by a SOAP API. It defines the methods, parameters, and data types that can be used to interact with the API. -
Why am I getting an error when running the code?
Possible causes include an invalid WSDL URL, network connectivity issues, incorrect method parameters, or an unsupported SOAP version. Check the WSDL URL, ensure you have an internet connection, and verify that the method parameters are correct. Also, make sure yoursuds
version is compatible with the SOAP service you are trying to access. -
Is suds still maintained?
The originalsuds
library is no longer actively maintained. It is recommended to usesuds-jurko
, a maintained fork, or consider usingzeep
for newer projects.