team-telnyx / telnyx-node

Node SDK for the Telnyx API
https://developers.telnyx.com/docs/api/v2/overview
MIT License
51 stars 20 forks source link

Using "contents" key with telnyx.faxes.send #150

Closed nabramow closed 6 months ago

nabramow commented 1 year ago

I'm trying to figure out how to send a fax using the contents key rather than media_url. In the docs under send fax it allows me to select "multi-part/form-data" but in the Node SDK bit it still shows media-url.

https://developers.telnyx.com/openapi/programmablefax/tag/Programmable-Fax-Commands/#tag/Programmable-Fax-Commands/operation/SendFax

How can I use the contents key to send a fax?

This is what I have:

      const result = await telnyx.faxes.send({
        connection_id: TELNYX_CONNECTION_ID,
        contents: fax.contents, // should be binary string
        from: '+123',
        to: '+123,
      });

This is getting me the following error: error (Status 422) (Request 33035a3d-e6bd-4149-8d52-7fa00210dc83) 'contents' is invalid.

I can't figure out if the docs are wrong or if it's a bug. I also don't see an option to add a multi-part/form-data Content-Typeheader.

Any chance y'all have an example?

nabramow commented 1 year ago

Okay, I've solved this now. Here was the issue:

For the formData, it needs a third argument "filename".

Appending the binary file string to the formData "contents" key should look like this:

 formData.append('contents', binaryPdfString, `myPdfName.pdf`);

In your docs it just shows:

 formData.append('contents', binaryPdfString);

So you might want to fix that in the docs here: https://developers.telnyx.com/openapi/programmablefax/tag/Programmable-Fax-Commands/#tag/Programmable-Fax-Commands/operation/SendFax

My second issue I think was related to the package we're using to fetch in node, we're using "isomorphic fetch" package.

For some reason it didn't like me manually inserting the "Content-Type" in the headers, I had to spread it from the formData instead like this:

const headers = {
    Authorization: `Bearer API_KEY`,
    ...formData.getHeaders(), 
 };

I figured that out from this post, in case anyone has the same issue: https://stackoverflow.com/questions/43500514/how-to-post-with-multipart-form-data-header-and-formdata-using-fetch

nabramow commented 1 year ago

Gonna leave this open just because you probably want to update the docs?

https://developers.telnyx.com/openapi/programmablefax/tag/Programmable-Fax-Commands/#tag/Programmable-Fax-Commands/operation/SendFax

Also it would be great if there was support for this in Node SDK! Or at least make it clear in the docs it's not possible to send multipart/form-data when using Node SDK as it took me ages to figure out as it still shows it as a possibility in the docs.

ADandyGuyInSpace commented 11 months ago

Thanks for bringing this up! I'll take a look and get back to you on this. There should be plans to support this at the SDK, I'll try to document the best case approach of sending this kind of media data and not just linking the urls.

ADandyGuyInSpace commented 6 months ago

This should be resolved with the latest update, will be adding full fledged form support to when TS gets implemented to make it more straightforward in attaching it

fszolno commented 2 months ago

Hi @ADandyGuyInSpace is there any example code on how to use this to send the file binary content with the multipart/form-data content-type?