How to Send a Fax Using Twilio Fax API

twilio_fax

Last week Twilio launched a new API that allows to send faxes. From the very start Appery.io platform made it very simple to connect to any external REST API. We love Twilio API and have an example and video on how to use the SMS API. The Fax API is as elegant and simple to use. In this blog post you will learn how to send a fax using the Twilio Fax API from a Server Code script.

Create a new Server Code script and copy the following script:

var url = "https://fax.twilio.com/v1/Faxes";
var accountSid = "AC3526fbeed7...............";
var token = "8703513246d3f445e............";
var to = "To number";
var from = "Your Twilio number"
var mediaUrl = "https://www.twilio.com/docs/documents/25/justthefaxmaam.pdf";

var XHRResponse = XHR2.send("POST", url, {
   "body": "To=" + encodeURIComponent(to) + "&From=" + encodeURIComponent(from) + "&MediaUrl="+mediaUrl,
   "headers": {
      Authorization: "Basic " + encodeBase64(accountSid + ":" + token),
      "Content-Type": "application/x-www-form-urlencoded"
   }
});

Apperyio.response.success(XHRResponse.body, "application/json");

The script is simple and does a POST request to the Twilio Fax API.

Line 1 is the Twilio Fax API endpoint.

Line 2 and 3 are Twilio Account SID and Auth Token. You can get both from the console dashboard.

Line 4 is where to send the fax (number).

Line 5 is your Twilio number.

Line 6 is a link to a document you want to fax. You can set it to any PDF file accessible via a direct link (or use the sample one provided by Twilio).

Line 8 is where the POST request is invoked with all the information.

Twilio Fax API uses Basic Authentication so the Account SID and Auth Token need to be encoded as Base64. There is an out-of-the-box plug-in that you can add to your account. The plug-in has a function to encode a string as Base64.

  1. Open the dropdown list (on the left) and select Back to list.
  2. Open the Plugins tab.
  3. Find the EncodeBase64 plugin and click insert to add it to your account. The plugin will be listed in the Library section. You can now use it in any other script.
  4. One more step is to add this library as a dependency for your script. Open that script you created. Go to Dependencies tab and click on EncodeBase64_Library.

You are ready to test the script.

Go back to the Script tab and open the Run tab (on the right). Click on Save and run button to invoke the API and send the fax.

The response should look like this:

{
   "date_updated": "2017-04-06T13:13:40Z",
   "date_created": "2017-04-06T13:13:40Z",
   "num_pages": null,
   "api_version": "v1",
   "media_url": null,
   "url": "https://fax.twilio.com/v1/Faxes/FX225bab5034cb8a335c45...............",
   "sid": "FX225bab5034cb8a335c45...............",
   "quality": "fine",
   "duration": null,
   "price_unit": null,
   "price": null,
   "account_sid": "AC3526fbeed7...............",
   "from": "+1415xxxxxxx",
   "to": "+1925xxxxxxx",
   "status": "queued",
   "direction": "outbound"
}

You can navigate to the URL set in the url attribute – there you will be able to see the status of the sent fax (the browser will ask you to login, use the Account SID and Auth Token values).

It can take a few minutes for the fax to be delivered, keep that in mind when testing.

To learn more about Server Code check out our YouTube playlist. Happy faxing.