Skip to main content
This example shows how to add a PDF to a sales order in Bonx from a Node.js script. The file will appear in the order’s Files tab, just as if it had been added from the interface. You need:
  • the sales order’s internal ID in Bonx;
  • a PDF file;
  • your Bonx GraphQL URL and API key.
Adding the file takes three calls:
1

Request an upload URL

Bonx returns a temporary URL to which you can send the PDF.
2

Upload the PDF

Send the file’s contents to this URL with a PUT request.
3

Link the PDF to the order

Create the file in Bonx with the salesOrders type and the order’s ID.

Complete example

This example uses fetch, which is available natively in Node.js 18 and later. Save the script as an .mjs file so that you can run it directly.

Things to check

  • salesOrderId is the order’s internal ID in Bonx.
  • The upload URL expires after 10 minutes. Request it just before sending the file.
  • Use application/pdf both when creating the URL and when making the PUT request.
  • For fileId, use the fileName returned by generatePresignedUrlUpload.
  • Running the script again adds another copy of the file. Avoid processing the same operation twice.
To attach the PDF to another record type, replace salesOrders with the relevant recordType. See Attaching a document for the available values.