The script
send-message.ts
Run it
Install the SDK, andtypescript and @types/node as development dependencies. Set "type": "module" in the package.json of the project, then save the script as send-message.ts with the tsconfig.json of the overview, and set ISNAP_API_KEY in the environment. The arguments are the id of one of your lines, the phone number of the recipient, and the text.
What it prints
A call that resolves means the API accepted the message. It does not mean the message was sent. A first call prints one of two results:
A retry with the same key prints the status the message has now, which can be any message status.
To follow the message after the send, read it with
client.messages.get(id). Held messages explains the holds.
Errors it handles
Errors and retries describes the classes, and
POST /v1/messages lists the codes of the operation.
Retry with the same key
The script passes its ownidempotencyKey, so that you can retry after an unknown outcome. Run it again with the same line, the same recipient, the same text, and the key it printed as a fourth argument. The API then returns the message already created, in its current state, and sends no second message. With the same key and a different text, the call throws ConflictError with code idempotency_conflict.
Send retries one after the other. Idempotency says how long a key stays valid.