Call a Web API in Appian

You can call a web API in Appian during the execution of a robotic process. A web API can help pass information back to Appian at certain times and on a regular basis, if desired. You can create web APIs in Appian using templates for common operations.

A common use case for calling a web API in Appian is to upload a document. For example, a robotic process can retrieve or process documents and upload it to Appian using the corresponding web API.

To set up a robotic process to call an Appian web API, you'll need to define some environmental variables, add the variables to a custom Java module or method, create credentials, and associate the code with an action in the workflow. This page walks through the steps to set up calling a web API through Appian RPA, referencing a common example to upload documents in Appian.

Prerequisites

To get started, you'll need to set up a few pieces in Appian and Appian RPA:

Create an endpoint variable

Before you create the method that will call your web API, we recommend that you add an environmental variable to point to the API endpoint URL. Using a variable to reference the endpoint URL makes the code easier to maintain when it changes. For example, the URL will change when you deploy the robotic process to a different environment so you'd only need to update the variable to reflect this change, instead of throughout your code.

rpa-example-endpoint.png

Appian recommends using credentials to store API keys for use in robotic processes.

Create a custom method

Next, you'll create the code that takes a document and passes to Appian via the web API. In the example below, we create a custom method to upload attachments to Appian.

The main set of code refers to previously defined classes and functions, so let's review those first:

  • The RestHelper class converts a Java object to JSON. The file can be found in the robotic process's source code (src > main > java).
  • The IUsernamePassword method is part of the Client module and retrieves the credentials associated with an application, as defined in the RPA Console.
  • The uploadFile function references the endpoint so when the robotic process recognizes a document, it's sent to Appian:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    private String uploadFile(File file) throws Exception {
        String result;

        // Create a FileEntity for the file
        HttpEntity reqEntity = EntityBuilder.create().setFile(file).build();

        // Create the HTTP POST client and request
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost request = new HttpPost(endpointUpload);
        
        // Get first credentials for Appian
        IUsernamePassword appianCredentials = server.getCredentials("APPIAN").get(0);
        
        // Retrieve credentials for the designated entry
        request.addHeader("Appian-API-Key", appianCredentials.getPassword());
        request.addHeader("Appian-Document-Name", file.getName());
        request.setHeader("Accept", "application/json");
        request.setEntity(reqEntity);

        // Execute the request
        try (CloseableHttpResponse response = client.execute(request)) {
            result = EntityUtils.toString(response.getEntity());
        }

        server.info(result);
        client.close();

        return result;
    }

Enter the following code to create the custom method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public void uploadAttachmentsToAppian() throws Exception {
    server.debug("Looking for files in: " + attachmentsDir.getAbsolutePath());
    File[] filesToUpload = Objects.requireNonNull(attachmentsDir.listFiles());
    server.setNumberOfItems(filesToUpload.length);
    for (final File fileEntry : filesToUpload) {
        server.debug("Found: " + fileEntry.getName());
        String result = uploadFile(fileEntry);
        Map<String, Object> resultMap = (Map<String, Object>) RestHelper.fromJson(result);
        server.debug("result: " + result);
        if ((int) resultMap.get("documentId") > 0) {
            // Capture the documentId to use elsewhere, such as the Start Process Action
            setWorkflowVariables(resultMap.get("documentId").toString());
        } else {
            throw new Exception("Issue uploading document (" + fileEntry.getName() + ") to Appian.");
        }
        server.debug(workflowVariables.get("documentId"));
    }
    try {
        if (attachmentsDir.exists()) {
            FileUtils.forceDelete(attachmentsDir);
        }
        FileUtils.forceMkdir(attachmentsDir);
    } catch (IOException e) {
        server.warn(String.format("%s could not be cleaned"), e);
    }
}

Next, identify the action in the workflow to use this method. Open the action and choose the method so the robotic process references this code at this point in the workflow.

rpa-upload-action.png

When the robotic process executes, it will upload the document to Appian using the API you created. You can then use the document ID in the robotic process or another process model.

Open in Github

On This Page

FEEDBACK