Upload

Upload files for processing

post

Upload files to be processed by Personaify.

Authorizations
AuthorizationstringRequired
Bearer authentication header of the form Bearer <token>.
Bodyobject[]
filestring · binaryOptional

The file to upload

Example: filename.txt
typestring · enumOptionalExample: invoicePossible values:
kb_idsstring | nullableOptionalExample: ["041becbaf34b41cdb1c3891ff8ce1ffa","d6cbe7203c494f6887e86d951439cc06"]
Responses
200Success
application/json
post
/api/upload
POST /api/upload HTTP/1.1
Host: app.personaify.ai
Authorization: Bearer YOUR_SECRET_TOKEN
Content-Type: multipart/form-data
Accept: */*
Content-Length: 173

[
  {
    "file": "filename.txt",
    "type": "invoice",
    "kb_ids": [
      "041becbaf34b41cdb1c3891ff8ce1ffa",
      "d6cbe7203c494f6887e86d951439cc06"
    ],
    "custom_fields": {
      "key1": "value1",
      "key2": "value2"
    }
  }
]
200Success
[]

The type node in the request body specifies the document type.

def upload_document(api_base_url, document_path, access_token, knowledgebase_ids, type=None, custom_fields=None):
    """
    Uploads a document to the API.  This will create the multipart form data and upload the document to the API.
    
    :param api_base_url: API base URL, eg: https://app.personaify.ai/api/
    :param document_path: Path to the document to upload
    :param access_token: Access token for the API
    :param knowledgebase_ids: [optional] List of knowledgebase IDs to associate the document with
    :param type: [optional] Document type - defaults to 'document'
    :param custom_fields: [optional] Custom fields to associate with the document
    """
    print(f"Uploading document: {document_path}")
    url = urljoin(api_base_url, 'upload')
    headers = {'Authorization': f'Bearer {access_token}'}

    fields = {
      'file': (Path(document_path).name, open(document_path, 'rb'), 'text/plain'),
      'type': type if type else 'document'
    }

    # Add 'kb_ids' if exists
    if knowledgebase_ids:
        fields['kb_ids'] = ','.join(knowledgebase_ids)
        
    # Add 'custom_fields' only if it's not None and not empty
    if custom_fields:
        fields['custom_fields'] = json.dumps(custom_fields)

    m = MultipartEncoder(
        fields=fields
    )
    try:
      print(f'Uploading document to {url}...')
      response = requests.post(url, data=m, headers={**headers, 'Content-Type': m.content_type})
      print(f'Response Status Code: {response.status_code}')
      print(f'Response Content: {response.content}')
    except Exception as e:
      print(f'Error uploading document: {e}')
      traceback.print_exc()
    response.raise_for_status()
    return response.json()
    

api_base_url = "https://app.personaify.ai/api/"
document_path = "/path/to/your/document.txt"
access_token = "your_access_token"
knowledgebase_ids = []
doc_type = "invoice"
custom_fields = {"field1": "value1", "field2": "value2"}

response = upload_document(api_base_url, document_path, access_token, knowledgebase_ids, doc_type, custom_fields)

print(response)

Last updated