Today we’re going to utilize Twilio’s Fax API and SparkPost to get an incoming fax and forward it as an outbound e-mail. We’ll utilize a telephone number we buy with Twilio and host our reasoning with the serverless Twilio Functions and TwiML Bins.
Register for – Or Sign Into – A Twilio Account
You’ll be utilizing a couple of Twilio items today to finish your mission: Twilio Programmable Fax for getting an inbound fax and Twilio Functions and TwiML Bins to host your reasoning.
Either develop a brand-new Twilio account (register for a totally free Twilio trial), or sign into an existing Twilio account. When you are visited, open a tab with the Functions Console. Then – leave it alone, we’ll return to it later on.
Purchase a Twilio Telephone Number
Similar to with telephone call and text you’ll require a telephone number to accept inbound faxes. If you have not yet bought a fax capable number, head over to the Purchase a Contact number area of the console.
Making certain to inspect the ‘Fax’ box in the abilities area, acquire a contact number of your option:
You will return to this number later on to direct inbound faxes. Next, let’s figure out the outbound e-mail bit by going to SparkPost.
SparkPost Email API Secret Generation Time!
We’re going to utilize SparkPost to deal with the information of sending e-mails including a link to a PDF of our inbound faxes. To begin things off with SparkPost, you’ll require to create a brand-new API secret. If you have not yet, sign up a brand-new account with SparkPost. In any case, login to your account with a mind to establishing some secrets.
After that, go on and head to the SparkPost designer console and produce one there. Bear in mind that SparkPost API secrets vanish as soon as they’re created, so proceed and conserve that in a safe location in the meantime.
Open up your Twilio Console and head on over to the back to the functions configuration section of the console once you’re produced that key. This is where you develop environment variables to abstract a few of the worths you’ll require in your code from the reasoning itself – ideal for an API secret like this.
Develop a brand-new environment variable called SPARKPOST_KEY and paste because API secret that you conserved.
Now that we have actually gotten your SparkPost secret set up, we’ll require to make certain that you’ll have the ability to utilize SparkPost’s Node.js SDK within Twilio Functions. On the functions setup page you were you, include a brand-new dependence and fill in SparkPost for the name. We have actually set the variation to 2.1.2, which, sometimes of publication, is the newest.
Send an Email with SparkPost and Twilio
We’ll require to establish a set of Functions and a TwiML Bin to manage getting the incoming fax and forwarding a link out to that fax. While forwarding the fax PDFs themselves is a choice, we will not be connecting the real PDF to the e-mail given that it can grow quite big.
Reroute to a Fresh Media URI
To begin things off right, let’s head to the Manage Functions page and produce a brand name brand-new function. Proceed and pick the ‘Blank’ design template. The very first function we are going to compose will reroute you to a link to the PDF of your fax pdf.
We need to reroute you to a fresh link since the openly available links for media on Programmable Fax have a 2 hour time to live. (I personally do not constantly inspect my e-mail every 2 hours!)
Go on and call this function Redirect to Fax Media and set the course to/ redirectToFaxMedia. You will require to ensure that “Examine for legitimate Twilio signature” is unattended – having it examined will avoid you from straight accessing that endpoint.
When you have this established, we’ll require to compose some code that will take a faxSid as a URL specification and reroute a user to the PDF for that fax.
In this piece of code, we utilize event.faxSid to get the faxSid from the querystring (e.g. your-uri-here. twil.io/ redirectToFaxMedia?faxSid=FXabcd). We bring that specific Fax, which consists of a newly produced mediaUrl.
Next, to reroute the user, we need to construct a Twilio.Response things. We set the statusCode on the reaction item to be a 303 and set the Area header to be the mediaUrl that was returned on the resource. We return this as part of the callback function upon success. This winds up rerouting the user to our newly produced mediaUrl!
Email a Link to your Inbound Fax
Now that you have actually the redirect established, we’ll require to compose another function to in fact send the link to your inbound fax. Return to the Manage Functions page and produce a brand-new function. You will wish to choose the ‘Blank’ design template once again. Go on and call this function Fax to Set the course and email to be/ faxToEmail.
This function is a bit longer than the previous one. You will require to change the YOUR_EMAIL_ADDRESS and SENDER_EMAIL_ADDRESS placeholders with your e-mail address and the e-mail address that you want to have actually the e-mail sent out from – or do not hesitate to reword the code to select up environment variables from your functions setup rather. Remember that the domain for that e-mail will require to be a validated sending out domain with SparkPost. In your own inbox, do not forget to enable the SENDER_EMAIL_ADDRESS that you define above so your fax e-mails do not get sent out to spam!
Twilio Functions has an unique criterion as part of its context item called DOMAIN_NAME – this deals with to your Functions URI. We utilize this to craft the base URI for the link that you will click to access the PDF of your fax. We utilize event.FaxSid to get the FaxSid from the inbound Fax action demand to finish out the URI. We then utilize the Transmissions API from SparkPost to send an e-mail consisting of the link you formerly produced. We’re setting the open_tracking and click_tracking choices to incorrect so that your URI does not get overwritten by SparkPost’s useful click tracking function.
After you strike conserve, ensure to copy the complete course for the Function. You will require this in a 2nd to set up the TwimlBin you established!
Set Up A TwiML Bin to Accept Inbound Faxes
When Twilio gets an incoming fax to a number you have actually established with us, we’ll require your application to inform us how to manage it. In this case, we’re going to utilize a TwiML Bin to return the action. TwiML Bins are serverless ‘bins’ that can keep the XML that comprises an action to Twilio.
Produce a brand-new TwiML Bin with the following piece of TwiML. Make certain to alter the YOUR_REDIRECT_TO_FAX_MEDIA_URL_HERE placeholder to be the URL that you copied when you developed your/ faxToEmail function. Proceed and call this Fax to Email Bin.
Seriously, that’s all you require to do! TwiML Bins are a simple, serverless method to host basic reasoning that does not require the overhead of a complete programs language. Next, let’s point the fax firehose (gardenhose?) at the Bin and finish the chain.
Link a TwiML Bin to Your Contact Number
Now that you have your Functions and Bin composed, you will require to head on over to the Manage Numbers area of the console and set up the number you have actually acquired. You will require to alter the number to get inbound Faxes.
Remember that Twilio numbers can just either get voice calls or get faxes. For the “A Fax is available in” area, You will desire to choose the TwiML bin that you simply produced in the previous action. When you struck the conserve button, you’re done and prepared!
Bring Faxes Into the 20th Century with Email Forwarding
Now, all that’s left for you to do is attempt it!
Utilize your option of Fax device or other-Twilio-code pretending to be a fax device to fax the contact number. You must rapidly get an e-mail with the brand-new link created by your Twilio Function, moderated by the 2nd Function and activated by the TwiML Bin.
If that works, have self-confidence the setup will continue to run day and night forwarding those day-to-day faxes you’re getting to your inbox. You’ll likewise no longer need to fret about your feline assaulting the faxes as they print.
There you go – a cloud fax maker constructed with a couple of Twilio services and products and assistance from our pals at SparkPost (unique thanks to Avi Goldman for the assistance!). Now it’s time for you to construct one – we can’t wait to see what you fax!
Editor: Wish to finish the circle? Our pals at Sparkpost published a tutorial from the other instructions, transforming e-mail to fax. Go examine it out!
Patrick Kolencherry is a Contact Center Professional at Twilio based in warm Austin, Texas. According to Facebook’s advertisement targeting platform, his interests consist of “Water Treatment, Filtering, and Wave” (we’re still puzzled by that a person). You can connect with him on Twitter at @kolencherry.