Building Your First API Call
Now that you've set up your staging account access, you're ready to make your first call to the stage API!
Our example code is in Python because it's easy to understand and commonly used, but you can use any language that can make Web calls. If you haven't set up a Python environment, download Python and configure it to your liking.
The Python examples require the requests and requests_toolbelt modules, so install the modules from the command line with:
python -m pip install requests
python -m pip install requests_toolbelt
The following Python code will make the GET call to the /credit endpoint to pull the awesomeuser user's credit balance.
import requests
url = "https://stage-rest.click2mail.com/molpro/credit"
# Define credentials
myusername = 'awesomeuser'
mypassword = 'gReAt/PaSsWoRd123
headers = {"Accept": "application/xml"}
response = requests.get(url, headers=headers, auth=(myusername, mypassword))
print(response.text)
Run the file:
py my_first_call.py
and if you did it right, you should get the following XML back:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<credit>
<balance>0.0</balance>
<allowNegative>false</allowNegative>
</credit>
Note that your XML will not contain the line breaks
Congratulations! You have taken your first step into a larger world!
For a line-by-line explanation of the above code, see our recipe:
Updated 8 months ago