Skip to main content

How to process documents using SenseTask API

To authorize, use your API Key:

import os
import requests
import glob

API_KEY = <the api key>
SERVICE_URI = 'https://api.sensetask.com/api/v1'
FOLDER_PATH = <the folder to upload>

def upload_folder():
files = list(glob.iglob('{}/*'.format(FOLDER_PATH)))

documents = []
for file_path in files:

# get the metadata
file_name, file_extension = os.path.splitext(os.path.basename(file_path))
file_size = os.path.getsize(file_path)

file_extension = file_extension.lower()

if file_extension == '.pdf':
file_type = 'application/pdf'
elif file_extension == '.png':
file_type = 'image/png'
elif file_extension in ['.jpeg', '.jpg']:
file_type = 'image/jpg'
else:
# the file type is not supported so it should not be uploaded
continue

# create the files in the sense filesystem
# first get a valid file signature

file_signature = requests.post(
'{}/files/'.format(SERVICE_URI),
json={
"fileName": file_name,
"fileType": file_type,
"fileSize": file_size
},
headers={'ApiKey': API_KEY}).json()


# then put the file

requests.put(file_signature['signedUrl'], data=open(file_path, 'rb'))
print('uploaded {}'.format(file_name))

# prepare the document batch payload
documents.append({
"fileName": file_name,
"fileType": file_type,
"fileSize": file_size,
"fileUri": file_signature['fileUri']
})

# create the job
job = requests.post(
'{}/jobs/'.format(SERVICE_URI),
json={ "documentCount": len(documents), "parentNode": <the folder id> },
headers={'ApiKey': API_KEY}).json()

# create a batch document create in the job

requests.post(
'{}/documents/batch/{}/'.format(SERVICE_URI, job['_id']),
json={ "data": documents },
headers={'ApiKey': API_KEY}).json()


if __name__ == "__main__":
upload_folder()

Make sure to replace <the api key> with your API key.

SenseTask uses API keys to allow access to the API.

SenseTask expects for the API key to be included in all API requests to the server in a header that looks like the following:

ApiKey: <the api key>

You can view and manage your API keys in the Sensetask Settings > Developers > Api Keys. Please keep your api key secure. Don't share it in public repositories or client side applications.

This tutorial will demonstrate how to process all the eligible documents in a folder.

First, it will securely upload the files in your account's Sense storage. After that, it will create a Job object using the Jobs API that will hold all the documents in this processing batch. Finally, the document objects will be created using the Documents Batch API.

Once this is complete, the processing job has already started. The processing status for the queue via the SenseTask web app. If webhooks endpoints have been added using the Webhook Endpoints API, your server should receive notifications for the configured events.