[Automation] Making a SOAP API Call in Python, Using a Session Key

Level of Difficulty: Beginner – Senior.

Let’s say you’re working on a system that needs to query a legacy SOAP API. Although SOAP APIs are becoming more scarce as REST and Open Data APIs are taking the world by storm, the reality is that SOAP APIs still exist and are still used. In most SOAP implementations, accessed is managed using sessions, accessible through obtaining a session key. For this post, we will be using the ‘zeep‘ library to make SOAP requests using session keys. We are then going to write the output of the SOAP request to a dataframe using ‘pandas‘.

Install Libraries

To make SOAP requests in Python, you’ll need to install zeep. To write to a dataframe, you’ll need to install pandas if you don’t already have it installed.

pip install zeep
pip install pandas

Import Libraries

There are a few libraries that we need to import before we can do anything meaningful.

# we need this to auth
from requests import Session
from requests.auth import HTTPBasicAuth

# we need this for soap stuff
from zeep import Client
from zeep.transports import Transport

# we need some of this stuff for soap stuff, and some of it jus coz!
import pandas as pd
import datetime

import zeep

Assign ‘Config’ Variables

We need three variables in order to ‘configure’ the request specific to what we are trying to access and what we are trying to access it as:

username = "<name>"
password = "<password which should be secured better than this>"
wsdl = "<your wsdl endpoint>"

The only thing truly different above is ‘wsdl‘. If you were using REST, that would be your HTTP endpoint. The URL, if you’d like.

Setup the Session

Before we can make a SOAP request, we need to setup the session we will use to make the request. It is important to close a session once it has been opened to avoid being locked out which could happen in some instances:

# create the session and auth
session = Session()
session.auth = HTTPBasicAuth(username, password)
client = Client(wsdl, transport=Transport(session=session))

# get the session key!
SessionKey = client.service.SessionStart(Input=SessionStartInputHeader)
key = SessionKey.Result.DataRows.DataRow[0].SessionKey

This is probably the point where it’d be a good idea to get a try catch finally block going.

Build the Request

Okay… Now comes the meaty part of the solution. Let’s try make the request in the ‘try‘ block, print the exception in the ‘catch‘ block and close the session in the ‘finally‘ block:


    # read the API docs - that's where you'll find out what service session data is expected from you
    servicesessiondata = {
        'Key': key

    # assign header and get ready to get the key
    servicesessioninputheader = {"Parameters": servicesessiondata}
    service_key = client.service.ServiceSessionStart(Input=servicesessioninputheader)
    service_key = service_key.Result.DataRows.DataRow[0].ServiceSessionKey

    # read the API docs - that's where you'll find out what request data is expected from you
    request_data = {
        "Parameters": {
        "Header": {'ServiceSessionKey': service_key}}

    # get the SOAP response by calling the function
    response = client.service.FunctionName(Input=request_data)

    # manipulating response
    result = zeep.helpers.serialize_object(response)

    # add response to the dataframe
    df = pd.DataFrame(result['Result']['DataRows']['DataRow'])
    df['LoadDate'] = datetime.now()
    df['LoadSource'] = "Python Script"

except Exception as e:

    # print exception


    # terminate the session so that you don't get locked out!
    terminate_key = {"Header": key}

Now, hopefully, you have your data from the SOAP request you just made.

Any chance this didn’t work for you? Feel free to reach out or leave a comment below. The full solution is available on GitHub.

Published by Jacqui Muller

I am an application architect and part time lecturer by current professions who enjoys dabbling in software development, RPA, IOT, advanced analytics, data engineering and business intelligence. I am aspiring to complete a PhD degree in Computer Science within the next three years. My competencies include a high level of computer literacy as well as programming in various languages. I am passionate about my field of study and occupation as I believe it has the ability and potential to impact lives - both drastically and positively. I come packaged with an ambition to succeed and make the world a better place.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: