How to display and upload attachments in a headless support portal
Attachments in a headless portal built on Plain's GraphQL API need two mutations: createAttachmentDownloadUrl to display existing attachments and createAttachmentUploadUrl to upload new files.
Display attachments from the timeline
When you query timelineEntries, message entries such as ChatEntry, EmailEntry, SlackMessageEntry and NoteEntry include an attachments field. Each attachment has an id, fileName, fileExtension, fileMimeType and fileSize.
To get a renderable URL, call createAttachmentDownloadUrl.
- Input:
{ attachmentId: ID! } - Returns:
attachmentDownloadUrl.downloadUrl, a signed URL that expires after 3 minutes. Fetch it on demand and do not cache it. - Required API key permission:
attachment:downloadThe response also includes
attachmentVirusScanResult. If it isINFECTEDorFAILED, do not render the file.nullmeans virus scanning is not configured for the workspace.For images, use the
downloadUrlas thesrc. For other file types, render a download link.
Upload files from customers
createAttachmentUploadUrl needs the attachment:create API key permission.
- Call
createAttachmentUploadUrlto receive anuploadFormUrland a list ofuploadFormDatakey-value pairs. - POST the file as
multipart/form-datato theuploadFormUrl. Add alluploadFormDatapairs as form fields first, then the file as the last field. - A successful upload returns HTTP 204.
- Use the returned
attachment.idin yourreplyToThreadorcreateThreadmutation.Upload URLs are valid for 2 hours. Attachments uploaded but never referenced in a message are deleted after 24 hours.