Highlighted
Regular Contributor

Anaplan API Python Library

Note: While all of these scripts have been tested and found to be fully functional, due to the vast amount of potential use cases, Anaplan does not explicitly support custom scripts built by our customers. This article is for information only and does not suggest any future product direction. This library is a work in progress, and will be updated with new features once they have been tested.

 

Getting Started

The attached Python library serves as a wrapper for interacting with the Anaplan API. This article will explain how you can use the library automate many of the requests that are available in our Apiary, which can be found at https://anaplan.docs.apiary.io/#.

 

This article assumes you have the requests and pyOpenSSL modules installed as well as the Python 3 version 3.7. Please make sure you are installing these modules with Python 3, and not for an older version of Python. For more information on these modules, please see their respective websites:

  • Python (If you are using a Python version older or newer than 3.7 we cannot guarantee validity of the article) 
  • Requests 
  • pyOpenSSL

Note: Please read the comments at the top of every script before use, as they more thoroughly detail the assumptions that each script makes.

 

Gathering the Necessary Information

In order to use this library, the following information is required:

  • Anaplan model ID
  • Anaplan workspace ID
  • Anaplan action ID
  • Anaplan certificate, or username and password

There are two ways to obtain the model and workspace IDs:

  1. While the model is open, go Help>About: Help Menu.png
  2. Select the workspace and model IDs from the URL: URL.png

 

Authentication

Every API request is required to supply valid authentication. There are two (2) ways to authenticate:

  1. Certificate Authentication
  2. Basic Authentication

Certificates are created in your Anaplan profile, under the certificates tab. Once generated, simply point to the file path for the certificate to use.

 

Basic authentication uses your Anaplan username and password.

 

To create a connection with this library, define the authentication type and details, and the Anaplan workspace and model IDs:

 

conn = AnaplanConnection(anaplan.generate_authorization("Certificate","<path to Anaplan Certificate>"), "<workspace ID>", "<model ID>")

or

conn = AnaplanConnection(anaplan.generate_authorization("Basic","<Anaplan username>", "<Anaplan password>"), "<workspace ID>", "<model ID>")

 

Getting Anaplan Resource Information

You can use this library to get the necessary file or action IDs. This library builds a Python key-value dictionary, which you can search to obtain the desired information:

ex.

list_of_files = anaplan.get_list(conn, "files")
files_dict = anaplan_resource_dictionary.build_id_dict(list_of_files)

This code will build a dictionary, with the file name as the key. The following code will return the ID of the file:

users_file_id = anaplan_resource_dictionary.get_id(files_dict, "file name")
print(users_file_id)

To build a dictionary of other resources, replace "files" with the desired resource: actions, exports, imports, processes.

 

Uploads

Note: This library is currently designed to work only with flat files, later versions will include "streamed" data.

You can upload a file of any size, and define a chunk size up to 50mb. The library loops through the file, reading chunks of the specified size and uploading to the Anaplan model.

  

upload = anaplan.file_upload(conn, "<file ID>", <chunkSize (1-50)>, "<path to file>")

 

Executing Actions

You can run any Anaplan action with this script, and define a number of times to retry the request if there's a problem. In order to execute an Anaplan action the ID is required. To execute, all that is required is the following:

run_job = execute_action(conn, "<action ID>", "<retryCount>")
print(run_job)

This will run the desired action, loop until complete, then print the results to the screen.

 

Downloading a File

If the above code is used to execute an export action, the fill will not be downloaded automatically. To get this file, use the following:

download = get_file(conn, "<file ID>", "<path to local file>")
print(download)

This will save the file to a desired location on the local machine (or mounted network share folder) and alert you once the download is complete, or warn you if there is an error. 


Edit: Attached library updated with better comments and improved functionality.



Jesse Wilson
Solution Architect - Data Integrations
Singapore
2 REPLIES 2
New Contributor

Re: Anaplan API Python Library

Thanks for sharing this Jesse!
Certified Master Anaplanner

Re: Anaplan API Python Library

Nice work--will Anaplan be providing alternative languages in the future (C#, Golang, etc)?