Skip to main content
These scripts upload a file as an attachment. The first one uses attachments.upload, waits until the attachment is ready, and sends it in a message. The second one runs the three steps itself, so that the attachment id is known from the first step. Attachments gives the limits and the retention of a file, and The attachments helper describes the helper.
The scripts act on your real account. The first one sends a real message, with the file, to the recipient you give, and the message counts against the sending limits of the line. An isnap_test_ account key is not a separate test environment: it acts on the same account, lines and billing as an isnap_live_ key.

Upload with the helper, then send

upload-attachment.ts
The client passes maxRetries: 2, the rule of this site. Besides that, the script changes two defaults, and states maxPolls at its default so that the polling budget reads in one place: attachments.upload resolves only when the attachment is ready: the script then sends it. The send has no body, which a message with an attachment allows. The SDK adds an idempotency key to the registration and to the send.

Errors it handles

Keep the attachment id

When the helper throws, it gives you no attachment id. To keep the id, run the three steps yourself: register, upload, poll.
upload-attachment-steps.ts
Wait for status: ready before you send an attachment. Pass the id in the attachments array of messages.send, as the first script does. The script throws the errors of the SDK without catching them: an APIError of attachments.create or attachments.get, or an APIConnectionError. It exits with code 1 when the attachment is not ready at the end: its status is then failed or expired, or the 5 minutes are spent.

Run them

Install the SDK, and typescript and @types/node as development dependencies. Set "type": "module" in the package.json of the project, then save the scripts as upload-attachment.ts and upload-attachment-steps.ts with the tsconfig.json of the overview, and set ISNAP_API_KEY in the environment. The arguments of the first script are the path of the file, its content type, the id of one of your lines, and the phone number of the recipient.
The line id and the phone number above are placeholders: replace them with yours.