OneSpan Sign How To: Making REST Calls To OneSpan Sign's API In Python
Python is one of the most popular general-purpose programming languages available. It is used in many application domains, such as: web applications, software development, desktop GUIs, etc. It’s popularity amongst developers is due to three distinct reasons:
- Readability
- Libraries
- Community
Python emphasizes on the ease of which a user can understand and write code. As a result, its syntax allows developer to write the equivalent concepts in fewer lines of code than Java or C++. Moreover, Python has been around for over two decades. Hence, it has a large standard library proving tools suited to many applications. Last but not least, Python has also a vast community, where you can easily find support. In this blog, I will show you how to make REST calls to OneSpan Sign’s API in Python.
The Code
For this blog, I will be using Python 2.7. If you don’t already have it installed, you can download it from their official website, here. You can also get the complete example code from the Developer Community Code Share. The first thing you’ll want to do is install the ‘requests’ module. This will allow you to make HTTP requests to OneSpan Sign’s API. Open your command prompt and enter the following line:
$ pip install requests
Now, in your favorite text editor, create a new file named "createAndSendPackage.py" and save it in a location of your choice. Go ahead and copy the code below. I will go over it in more detail further down.
import requests import json url = "https://sandbox.esignlive.com/api/packages" payload = json.dumps({"documents":[{"id":"sample-contract","name":"Test Document"}],"status":"SENT","type":"PACKAGE","roles":[{"type":"SIGNER","id":"Signer1","signers":[{"email":"[email protected]","firstName":"John","lastName":"Smith","id":"Signer1"}],"name":"Signer1"}],"name":"Example Package"}) file = open('doc1.pdf', 'rb') files = { 'payload': payload, 'file': file } headers = { 'authorization': "Basic your_api_key", 'accept': "application/json" } response = requests.post(url, files=files, headers=headers) print(response.text)
Now, let’s go over the code in greater detail. The first couple of lines imports the modules needed in order to make REST calls.
import requests import json
The next line is the endpoint url where you will send your POST request to.
url = https://sandbox.esignlive.com/api/packages
Then, the JSON string that defines your package is created. You would typically build your JSON string dynamically versus having a big string like the one below. I chose to do so for simplicity.
payload = json.dumps({"documents":[{"id":"sample-contract","name":"Test Document"}],"status":"DRAFT","type":"PACKAGE","roles":[{"type":"SIGNER","id":"Signer1","signers":[{"email":"[email protected]","firstName":"John","lastName":"Smith","id":"Signer1"}],"name":"Signer1"}],"name":"Example Package"})
Next, the document binaries are read and the appropriate request payload constructed.
file = open('doc1.pdf', 'rb') files = { 'payload': payload, 'file': file }
Finally, the headers are defined and the POST request is made to create and send your package. Make sure to replace the api key placeholder with your own value.
headers = { 'authorization': "Basic your_api_key", 'accept': "application/json" } response = requests.post(url, files=files, headers=headers) print(response.text)
Running Your Code
Open your command prompt and change the current directory to the location where you saved your "createAndSendPackage.py" file. Then, enter the following line:
python createAndSendPackage.py
OneSpan Sign will then return a package id as a response, which will be printed to the command prompt window.
If you have questions regarding this blog or anything else concerning integrating OneSpan Sign into your application, visit the developer community forums: https://community.onespan.com. That's it from me. Thank you for reading! If you found this post helpful, please share it on Facebook, Twitter, or LinkedIn.
Haris Haidary
Junior Technical Evangelist
LinkedIn | Twitter