Skip to content

Sending an Attachment (bizzStream.getAttachmentBase64String)

The majority of external APIs (including LLMs) accept attachments in the form of a base64-encoded string. You can use bizzStream.getAttachmentBase64String to get the base64 string of an attachment.

Syntax

bizzStream.getAttachmentBase64String(attachmentInfo);

Parameters

The getAttachmentBase64String function takes a single 'attachmentInfo' object with the following properties:

Parameter Type Required Description
ddName String No The document definition name of the source document. Default is the DD of the serverDocument.
_id String Yes (if externalId is not provided) The ID of the document that contains the attachment.
externalId String Yes (if _id is not provided) The external ID of the document that contains the attachment.
valuePath String Yes The path within the target document pointing to the attachment field.
fileName String Yes (if fileId is not provided) Specific file name within the source field.
fileId String Yes (if fileName is not provided) Specific file name within the source field.

Return value

The base64 string of the attachment. This can then be sent in the body of a REST call, in whichever way the external API requires.

Example

The following example shows how to send an attachment to OpenAI's GPT for getting a summary:

const OpenAiKey = actionInfo.settings.openaiApiKey;
const headers = {
    'Authorization': `Bearer ${OpenAiKey}`,
    'Content-Type': 'application/json'
}
const lineFieldFileId = serverDocument.ingredients[1].picture[1]._id;

const pictureString = bizzStream.getAttachmentBase64String({
    //ddName: "menuItem", <-- Only needed if the dd is different from the serverDocument's dd
    _id: serverDocument._id,
    valuePath: "ingredients.1.picture",
    fileId: lineFieldFileId
});
const body = {
    model: 'gpt-4o',
    messages: [
        {
            role: 'user',
            content: [
                {
                    type: 'text',
                    text: 'Read through the attachment, and summarize the contents'
                },
                {
                    type: 'image_url',
                    image_url: {
                        //Attachment type can be found in the document object under the attachment field
                        url: `data:image/jpeg;base64,${pictureString}`
                    }
                }
            ]
        }
    ]
};
//This particular API only accepts image attachments. The function that is documented here works on any attachment type.
const response = REST.call('https://api.openai.com/v1/chat/completions', 'POST', headers, body)
const gptResponse = response?.choices?.[0]?.message?.content;
notification.info("GPT says:", gptResponse)