Sending Documents via API

This documentation provides a detailed guide on how to send documents from Team Assistant (TAS) via the API. The process involves retrieving document IDs from the Team Assistant storage system, exporting the documents, encoding them if necessary, and then sending them to an external system using an API request.

  • Depending on the API integration, the document can be retrieved as a physical file or base64-encoded content.

Key Functions in the Process

1. Retrieving Documents

To send a document, the first step is to extract the document from the Team Assistant storage system. This involves fetching the document ID and retrieving its content in a format suitable for transmission.

  • The document ID can be retrieved using a query to the Team Assistant DMS.
  • Depending on the API integration, the document can be retrieved as a physical file or base64-encoded content.
  • If required, the file is temporarily stored in a designated location before being processed.

2. Preparing the Document for Transfer

Once retrieved, the document may require additional processing:

  • Base64 Encoding: If the API expects a base64 string, the document content is encoded accordingly.
  • File Name Assignment: Ensuring each document is properly named before sending.
  • Multiple File Handling: If multiple documents need to be sent, they are stored in an array before being uploaded.

3. Sending Documents via API

After processing, the document is sent to the external system via an API request. This involves:

  • Constructing the API endpoint URL.
  • Configuring HTTP request headers, such as authentication tokens and content type.
  • Attaching the document (either as a file or as a base64 string) to the request body.
  • Sending the request using a method such as HTTP POST.
  • Handling the API response to confirm a successful transfer.

4. Error Handling and Logging

To ensure smooth operation, error handling mechanisms should be in place:

  • Validating that the document ID exists before attempting to retrieve content.
  • Checking that the API response returns a successful status code.
  • Logging errors and warnings if the document upload fails.
  • Implementing retries in case of temporary failures.

Code examples

Physical file

/**
* Sends an attached document related to a specific case.
*
* @param {string} documentToSend - The actual name of the document attached to the case.
* @param {string} docNameTarget - Name of the document that you want receiving software to see.
*/
function sendDocument(documentToSend, docNameTarget) {
const dmsEntity = storage.getDmsEntity(documentToSend);
const {
NAME: fileName,
DMSF_ID: doc_id
} = dmsEntity;
if (fileName.toLowerCase().indexOf('.pdf') == -1) debug.error('pdf file only');
const finalFileName = `${docNameTarget}.pdf`;
const tmp = storage.getTmpLibrary();
const {
name
} = tmp.fileSync();

const filePath = `${name}_${finalFileName}`;
proc.warn(`tmp file path: ${filePath}`);
lib.exportAttachment(documentToSend, filePath);

curl.start();
curl.setOpt('URL', 'https://your.url/api/document/add');
curl.setOpt('CUSTOMREQUEST', 'POST');
curl.setOpt('FOLLOWLOCATION', true);
curl.setOpt('FAILONERROR', false);
curl.setOpt('SSL_VERIFYPEER', false);
curl.setOpt('SSL_VERIFYHOST', false);
curl.setOpt('TIMEOUT', 300);
curl.setOpt('HTTPHEADER', [
`Authorization: Bearer xxx`,
`accept: application/json`,
`Content-Type: multipart/form-data`
]);
curl.setOpt('HTTPPOST', [{
name: 'Files',
file: filePath,
type: 'application/pdf'
}]);

const response = curl.perform();
return response;
}

Base64-encoded content

var fileId = lib.getDMSFileIds('*', ',') 
var files = [];

// Create a new Array, with name and content of each file
fileId = fileId.split(',');
for (var i = 0; i < fileId.length; i++) {
var fileContent = storage.getDmsContent(fileId[i], 'base64');
var fileName = variableFileNames[i];
if (fileName) {
files.push({ fileName, fileContent }); } }
var base64String = '';
var namesString = '';
for (let i = 0; i < files.length; i++) {
if (i > 100) {
break;
}
if (i === 0) {
if (typeof files[i]['fileContent'] == 'object') {
base64String = files[i]['fileContent'].toString('base64');
} else {
base64String = files[i]['fileContent']; }

Anna Gernát Updated by Anna Gernát

AI for contracts in Team assistant

Contact

Syca (opens in a new tab)

Powered by HelpDocs (opens in a new tab)