How do I build an API connection in Python?

This article shows an example of an API connection in Python.

PyPi package

We have a PyPi package available that provides functionality for authentication, authorization, and requesting resources. This package can be installed using pip.

pip install mopinion

We provide additional and more in-depth information on

Create a Python connection without the PyPi package

In the below code replace the following with your own credentials.

public_key = '8e8e8e8e8e88e'
token = '9d9galnojvavlkre455%lreagfglkfgslkng'

import requests import base64 import hmac, hashlib import simplejson as json import pprint pp = pprint.PrettyPrinter(indent=1) def callAPI(base_url, public_key, token, endpoint='/account', method='GET', body='', query=''): signature_token = bytes(token.encode('utf-8')) uri_and_body = '{}|{}'.format(endpoint,body).encode('utf-8') uri_and_body_hmac_sha256 =, msg=uri_and_body, digestmod = hashlib.sha256).hexdigest() xtoken = base64.b64encode(bytes('{}:{}'.format(public_key, uri_and_body_hmac_sha256), 'utf-8')) url = '{}{}{}'.format(base_url, endpoint, query) headers = { 'X-Auth-Token': xtoken, 'version':'1.18.14', 'verbosity':'full' } response = requests.request(method, url, data=body, headers=headers, params=query) return response, xtoken baseurl = '' endpoint = '/account' #public key and signature token public_key = '8e8e8e8e8e88e' token = '9d9galnojvavlkre455%lreagfglkfgslkng' query = '' call = callAPI(baseurl,public_key,token,endpoint,query=query) pp.pprint(call[0].json()) print(call[1])