Hello
I am attempting to utilize the transactional API in Python. Specifically trying to retrieve all line items in a model w/ID
I tried doing this in Anaplan Connect earlier, however, it did not work for me. I am now trying in Python. However, it's quite the challenge as 1 article does not have the steps, I am having to read 5+ articles to piece together the script, though because each author has slightly different naming variables, I am having a bit of difficulty piecing it all together.
Anyone have a template which can call the transactional API? I will ideally need a template that takes me from authentication all the way through retrieving all line items and then putting that data into a csv.
Tagging Python pros: @Kevin_Cho @jesse_wilson @christophe_keom @JaredDolich
Solved! Go to Solution.
Cheers @DaanishSoomar for picking that up, there is indeed a typo!
And yeah, you can consolidate it all into one script if you'd like. You can create a function like how Karan had done it, if you will be calling it multiple times.
E.g.
def auth_token_basic(email, password):
user_pw = b64encode(bytes(email+':'+password, 'utf-8'))
auth_headers = {}
auth_headers['Authorization'] = 'Basic ' + user_pw
auth_url = 'https://auth.anaplan.com/token/authenticate'
auth_json = requests.post(auth_url, headers=auth_headers).json()
try:
auth_token = auth_json['tokenInfo']['tokenValue']
return auth_token
except KeyError:
print('No Token Info found - check your credentials?')
You can then just call this function
my_email = '...'
my_password = '...'
token = auth_token_basic(my_email, my_password)
Can do the same for the CSV script I gave before too
Sorry Daanish! I haven't dug into the Transactional APIs yet primarily because they use V2 API. I have every scenario under the sun for bulk APIs using Python. The issue is the tokenization for me. I have to figure out how to get that working.
Yeah that’s also exactly where I’m struggling too. I’ve tried compiling research and applying it from other articles in community. I guess my execution is off.
@DaanishSoomar i was able to do it . What error are you getting ? may be share your python code i should be able to debug
My code ( function to get all line items for a given module)
def getAlllineitems(mID,t):
url = 'https://api.anaplan.com/2/0/models/'+mID+'/lineItems'
header = {'Authorization':'AnaplanAuthToken ' + t,'Accept':'application/json','Content-Type': 'application/json'}
response = requests.get(url, headers= header)
return response.json()
mID = model ID
t = Token value
Karan Kochhar
Hey @DaanishSoomar and @JaredDolich
Take a look at this (draft) article that I had written Python and Anaplan: Pt 2—Anaplan Model Registry Ca... - Anaplan Community It's the next part in my series, but just awaiting review before publication.
It has some sample code to generate an authentication token using your username and password, and also has a link to a great article by @scott.smith for generating the auth strings using a CA Certificate.
To specifically retrieve all line items in a model and then turn it into a CSV, you can follow the below:
import csv
import json
import requests
auth_token = ''
mdl_id = '' # insert your model ID here
lineitem_url = 'https://api.anaplan.com/2/0/models/{}/lineItems/'.format(mdl_id)
#.format() method replaces each set of curly braces with the variables listed
#define headers as a dict
request_headers = {}
request_headers['Authorization'] = 'AnaplanAuthToken ' + auth_token
#pass request headers through
lineitem_json = requests.get(lineitem_url, headers=request_headers).json()
#line item endpoint returns specific json schema, we want the object "items" which is a list of dict objects representing each individual line item
lineitem_dict = lineitem_json['items']
#converting to csv
keys = lineitem_dict[0].keys() #get column headers as the keys per object
file_name = 'lineitems.csv'
with open(file_name, 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(lineitem_dict)
You are amazing @kevin.cho!
Just to validate my understanding. Below is the script I should utilize for transactional api and retrieving all line items in a model.
However, before I also need another script that creates an auth token?
So the order of operations would be:
1. Run script that generates auth token in a separate script that is used only for generating a token.
2. Copy/paste that token into my desired script into the token variable line.
3. Run that desired script.
Can you please confirm if I have the steps correctly?
Also quick follow up, I copied and pasted the basic authentication script from the article that is still in review, I received this error message. I also made sure to swap in my username and password above (hid it for privacy)
Cheers @DaanishSoomar for picking that up, there is indeed a typo!
And yeah, you can consolidate it all into one script if you'd like. You can create a function like how Karan had done it, if you will be calling it multiple times.
E.g.
def auth_token_basic(email, password):
user_pw = b64encode(bytes(email+':'+password, 'utf-8'))
auth_headers = {}
auth_headers['Authorization'] = 'Basic ' + user_pw
auth_url = 'https://auth.anaplan.com/token/authenticate'
auth_json = requests.post(auth_url, headers=auth_headers).json()
try:
auth_token = auth_json['tokenInfo']['tokenValue']
return auth_token
except KeyError:
print('No Token Info found - check your credentials?')
You can then just call this function
my_email = '...'
my_password = '...'
token = auth_token_basic(my_email, my_password)
Can do the same for the CSV script I gave before too
Thanks Kevin!