Search code examples
circuit-sdk

Attach a file (picture) to a conversation


I would like to be able to attach a file to a conversation, using the REST API. Is it possible? There is a «attachments» to /conversations/{convId}/messages/{itemId} but is that usable? How? The description of that field is not available.


Solution

  • The file API of Circuit supports the upload of attachments. As soon as you received your access token you can POST a message with the byte data. The following example wold upload a file with name test.jpg

    POST /rest/v2/fileapi HTTP/1.1
    Host: local.circuit.com
    Authorization: Bearer <access token>
    Content-Length: 100
    Content-Disposition: attachment; filename="test.jpg"
    Cache-Control: no-cache
    
    <your content in binary form here>
    

    Usually I am using Postman for my tests since it is very easy to use and supports OAuth 2.0 token generation (https://www.getpostman.com/)

    You will receive an result that looks like

    {"fileId":"fb211fd6-df53-4b82-824d-986dac47b3e7","attachmentId":"ZmIyMT..."}
    

    If you want to validate your upload you can check it via

    GET /rest/v2/fileapi?fileid=fb211fd6-df53-4b82-824d-986dac47b3e7 HTTP/1.1
    Host: local.circuit.com
    Authorization: Bearer <access token>
    Cache-Control: no-cache
    

    Well that was the easy part, now that you have uploaded the file to the backend you must attach it to a conversation item. Today we do not support UPDATE, i.e. you need to create a new one.

    POST /rest/v2/conversations/<conv ID>/messages HTTP/1.1
    Host: local.circuit.com
    Authorization: Bearer <access token>
    Content-Type: application/x-www-form-urlencoded
    Cache-Control: no-cache
    
    content=New+Text+Message&attachments=ZmIyMT...
    

    You have to pass the generated attachment ID. After the execution of this requests the file is attached to the conversation.

    If you skip the second step the file will not be linked to any conversation, is only accessible by the user who initiates the upload and will be deleted within the next 24 - 48h automatically.

    Hope this helps, let me know if you have additional questions.