Consuming Anaplan Rest API 2.0 from Java

Java Spring Boot Framework Provides an easy way to interact with the API. It can call any application Base API and process the Response according to our needs. This article explains how we can invoke Anaplan Rest API using Java Spring Boot Framework.

To start with, we must install the below software and SDE to run the application:
STS: Spring Tool Suite
Java: Java 1.8 would be the preferred version.
Postman: This is a tool to test APIs.

Installation

Since we are using Spring framework, we need to install Spring Tool Suite as an IDE to develop Spring applications. It is an Eclipse-based development environment. It provides a ready-to-use environment to implement, run, deploy, and debug the application. It validates our application and provides quick fixes for the applications.

You can download STS using the below link. Extract the zip file and run sts.exe
http://download.springsource.com/release/STS/3.8.3.RELEASE/dist/e4.6/spring-tool-suite-3.8.3.RELEASE...

In a similar way, you can download Java 8 and Postman to test the API we have created.

Project Setup

Spring Boot is built on the top of the Spring Framework. It provides an easier and faster way to set up, configure, and run both simple and web-based applications

It is a Spring module that provides the Rapid Application Development (RAD) feature to the Spring Framework. It is used to create a stand-alone, Spring-based application that you can just run because it needs minimal Spring configuration. All the dependencies required for the project can be mentioned in the pom.xml file.

Spring provides a quick way to generate your project with the required configuration and dependencies and download as a zip file at start.spring.io.

karthik_kumar_0-1596524184744.png

Once the project has download, import the project as existing maven projects in STS.

  • Select file and import the project.

karthik_kumar_1-1596524184749.png

  • Import as existing maven projects.

karthik_kumar_2-1596524184754.png

  • Select the folder path where we have downloaded the project.

karthik_kumar_3-1596524184757.png

Code Explanation

Spring web dependency helps to develop a custom API. In our project, we are creating each custom API to perform the below operations and test them using Postman.

  • Upload file.
  • Run an import action.
  • Run a process.
  • Run an export action.
  • Download file.
  • Run a delete action.

For all the system API calls, Spring web dependency provides Rest Template Package. We can use this to make the HTTP call.

Unlike Anaplan 1.3 API, 2.0 works based on token authentication. For each system API call, we must provide a token that expires after 30 minutes. We can generate the refresh token using the previous token which will again expire after 30 min. We can also expire the current token once we no longer need the token.

To get the token for the first time, we have to make an authenticate HTTP call by providing a username and password.

We can save our username, password workspace id, and model id in the properties file.

karthik_kumar_4-1596524184759.png

Below is the code snippet to fetch the token and expire the token.

karthik_kumar_5-1596524184762.png

Set the username and password as HTTP Headers and make HTTP call using Rest Template. We will get the token as a response and mapped to Authentication Response Object & Return the token value.

karthik_kumar_6-1596524184767.png

To successfully logout, pass the current token as header and make a Post call.

We are making a token API call before calling Anaplan Operation System API (Upload, Import,…) and logout API call after that.

Now, we will define our custom API for most of the Anaplan operations.

AnaplanOperationsController class will be defining our custom API definition with the required parameters and call the service method.

AnaplanOperationsService class will have different methods to call Anaplan System API.

Below is the code explanation on how we can upload the file using our Custom API.

  • Create a method in the controller class which will have the field as the URL parameter and request body as the data we want to upload the file.

karthik_kumar_7-1596524184768.png

  • In the service layer class, provide your logic implementation.

karthik_kumar_8-1596524184774.png

Get the token value by calling the user token method where we are performing an HTTP call to get the user token. Once we get the token, add it to the HTTP headers along with content-type.

Then, perform the Anaplan System Upload HTTP call by passing upload URL, HTTP Method, File data to the RestTemplate as parameters.

We can get to know whether the upload operation is successful or failure by status code.

Once we get the status, invalidate the token that we have used, using the logout user.

Now we can test the code implementation by calling our custom API.

File Upload needs a file ID and the data we need to upload as an input. Make sure the delimiter we have chosen in Anaplan is the same for the data we are uploading, separated by comma or tab, depending on what we have chosen in Anaplan.

Use the below API to get the File ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_0-1596525506499.png

In a similar way, we can invoke other Anaplan System APIs. Code implementation is in the demo project file.

  • Run an import action.

Once we have uploaded the file, the data needs to be reflected in the module. To achieve this, we need to run an Import Action. To run an Import Action, we need the Import Id. Get the Import ID using the below API and use it in our custom API. In response, we will get the Success Message after the import action has completed.

Use the below API to get the Import ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_1-1596525552900.png

  • Run a process.

We can run a process adding Import Action to the process. To run a process, we require a Process ID. Get Process ID by using the below API and use it in our Custom API. In response, we will get the Success Message once the process action has completed.

Use the below API to get the Process ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_2-1596525568233.png

  • Run an export action.

To run an Export Action, we need an Export ID which we will get from Anaplan Export API. Once the Export Action has completed, it will send the success message. We can customize the code in a way to check whether Export has completed or not, by calling Action Status API.

Use the below API to get the Export ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_3-1596525581199.png

  • Download file.

Once the Export Action completed, we can run the Download Action to download the latest file content. To download the file we need to have the File ID, which we will get using the Anaplan Rest API.

Use the below API to get the File ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_4-1596525591894.png

  • Run a Delete action.

To perform Delete Action, we need Delete ID. Once Delete Action has performed, we will get a successful message.

Use the below API to get the Delete ID, making sure you enter your Workspace ID and Model ID in the appropriate places:
https://api.anaplan.com/2/0/workspaces/Your Workspace ID/models/Your Model ID

karthik_kumar_5-1596525605950.png

Here we have used Postman to test out custom API implementation. In a similar way that we have created for Anaplan, we can create endpoints and method logic for the source application (Salesforce, Any Database, NetSuite) and call those methods and transform the data to our needs, and we can send data into Anaplan using the above approach. We can use the above implementation as an alternative approach for an integration solution, instead of using an integration tool.

Note: All the above implementations are to show how to call the Anaplan Rest API using java Spring Boot Framework. We can make it more customized for every action, depending on our needs of the application.

The content in this article has not been evaluated for all Anaplan implementations and may not be recommended for your specific situation.
Please consult your internal administrators prior to applying any of the ideas or steps in this article.