Skip to main content
Functions

Build faster with serverless Functions

Functions is a serverless environment on Twilio where you can quickly create event-driven microservices, integrate with 3rd party endpoints, and extend Twilio Studio flows with custom logic.

illo-hero-functions

From concept to first edition
in fewer steps

Json code being connected to javascript code that will lead to different functions such as messaging or calls and finally to user.

Build event-driven microservices with Node.js

Create scalable, production-grade, event-driven Twilio applications using Node.js. Innovate without worrying about stability. Iterate quickly. Use custom logic for Studio applications and IVR systems.

Node js code connected to a database integrating different 3rth party services.

Integrate with 3rd-party endpoints

Integrate with any 3rd-party system by transforming and filtering your data using the power of Node.js and the npm ecosystem. Make external API requests with ease and set up webhooks to ingest data from Twilio products or external systems.

Studio being connected to Functions that will lead to different actions such as messaging and calls.

Your code on Twilio

Bring ideas to life quickly, whether you’re building powerful APIs, complex integrations, or adding custom logic to your IVRs. Let Twilio Functions handle the operational overhead so you can get to market faster.

Twilio Code helping grow metrics all around the world.

Automatic scaling and transparent pricing

Serverless on Twilio automatically scales from zero to peak demands and features a pay-as-you-go business model to minimize costs for your organization.

Functions use cases

Deploy with Functions in less
than 5 minutes

Send an SMS from Twilio Functions
  • Node.js

                    <div class="highlight"><span class="code-line"><span class="nx">exports</span><span class="p">.</span><span class="nx">handler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span><span class="w"> </span><span class="nx">event</span><span class="p">,</span><span class="w"> </span><span class="nx">callback</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">client</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">getTwilioClient</span><span class="p">();</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">message</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">client</span><span class="p">.</span><span class="nx">messages</span><span class="p">.</span><span class="nx">create</span><span class="p">({</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">to</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;+15558675310&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="kr">from</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;+15017122661&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">body</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;Hi there!&#39;</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="p">});</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="nx">message</span><span class="p">.</span><span class="nx">sid</span><span class="p">);</span><span class="w"></span>
</span><span class="code-line"><span class="p">};</span><span class="w"></span>
</span></div>

                

Send an SMS from a Twilio Phone Number

Send an SMS from a Twilio phone number. With just a few lines of code, you’ll see your phone light up with a Twilio SMS using Twilio Functions.

Send an SMS

Send OTP from Twilio Functions
  • Node.js

                    <div class="highlight"><span class="code-line"><span class="nx">exports</span><span class="p">.</span><span class="nx">handler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="p">(</span><span class="nx">context</span><span class="p">,</span><span class="w"> </span><span class="nx">event</span><span class="p">,</span><span class="w"> </span><span class="nx">callback</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">client</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">getTwilioClient</span><span class="p">();</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">service</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">VERIFY_SERVICE_SID</span><span class="p">;</span><span class="w"></span>
</span><span class="code-line">
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">verification</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">await</span><span class="w"> </span><span class="nx">client</span><span class="p">.</span><span class="nx">verify</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="p">.</span><span class="nx">services</span><span class="p">(</span><span class="nx">service</span><span class="p">)</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="p">.</span><span class="nx">verifications</span><span class="p">.</span><span class="nx">create</span><span class="p">({</span><span class="w"></span>
</span><span class="code-line"><span class="w">      </span><span class="nx">to</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;+15558675310&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">      </span><span class="nx">channel</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;sms&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">      </span><span class="nx">locale</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;en&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="p">});</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="s1">&#39;Verfication OTP sent&#39;</span><span class="p">);</span><span class="w"></span>
</span><span class="code-line"><span class="p">};</span><span class="w"></span>
</span></div>

                

Send a One-Time Passcode Verification (OTP)

Integrate Functions with existing Twilio services, like a one-time passcode (OTP) experience that uses SMS, WhatsApp, voice or email.

Send an OTP

Log Incoming SMS in Google Sheets
  • Node.js

                    <div class="highlight"><span class="code-line"><span class="kd">const</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="nx">google</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;googleapis&#39;</span><span class="p">);</span><span class="w"></span>
</span><span class="code-line"><span class="kd">const</span><span class="w"> </span><span class="nx">fs</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">require</span><span class="p">(</span><span class="s1">&#39;fs&#39;</span><span class="p">).</span><span class="nx">promises</span><span class="p">;</span><span class="w"></span>
</span><span class="code-line">
</span><span class="code-line"><span class="nx">exports</span><span class="p">.</span><span class="nx">handler</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">async</span><span class="w"> </span><span class="kd">function</span><span class="w"> </span><span class="p">(</span><span class="nx">context</span><span class="p">,</span><span class="w"> </span><span class="nx">event</span><span class="p">,</span><span class="w"> </span><span class="nx">callback</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">twiml</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">Twilio</span><span class="p">.</span><span class="nx">twiml</span><span class="p">.</span><span class="nx">MessagingResponse</span><span class="p">();</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">authJson</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">JSON</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="k">await</span><span class="w"> </span><span class="nx">fs</span><span class="p">.</span><span class="nx">readFile</span><span class="p">(</span><span class="nx">Runtime</span><span class="p">.</span><span class="nx">getAssets</span><span class="p">()[</span><span class="s1">&#39;/credentials.json&#39;</span><span class="p">].</span><span class="nx">path</span><span class="p">)</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="p">);</span><span class="w"></span>
</span><span class="code-line">
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">auth</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="ow">new</span><span class="w"> </span><span class="nx">google</span><span class="p">.</span><span class="nx">auth</span><span class="p">.</span><span class="nx">JWT</span><span class="p">({</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">email</span><span class="o">:</span><span class="w"> </span><span class="nx">authJson</span><span class="p">.</span><span class="nx">client_email</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">key</span><span class="o">:</span><span class="w"> </span><span class="nx">authJson</span><span class="p">.</span><span class="nx">private_key</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">scopes</span><span class="o">:</span><span class="w"> </span><span class="p">[</span><span class="s1">&#39;https://www.googleapis.com/auth/spreadsheets&#39;</span><span class="p">],</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="p">});</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="kd">const</span><span class="w"> </span><span class="nx">sheets</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">google</span><span class="p">.</span><span class="nx">sheets</span><span class="p">({</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">version</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;v4&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">auth</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="p">});</span><span class="w"></span>
</span><span class="code-line">
</span><span class="code-line"><span class="w">  </span><span class="k">await</span><span class="w"> </span><span class="nx">sheets</span><span class="p">.</span><span class="nx">spreadsheets</span><span class="p">.</span><span class="nx">values</span><span class="p">.</span><span class="nx">append</span><span class="p">({</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">spreadsheetId</span><span class="o">:</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">DOCUMENT_ID</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">range</span><span class="o">:</span><span class="w"> </span><span class="s2">&quot;&#39;&quot;</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">context</span><span class="p">.</span><span class="nx">SHEET_NAME</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="s2">&quot;&#39;&quot;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">valueInputOption</span><span class="o">:</span><span class="w"> </span><span class="s1">&#39;USER_ENTERED&#39;</span><span class="p">,</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="nx">requestBody</span><span class="o">:</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
</span><span class="code-line"><span class="w">      </span><span class="nx">values</span><span class="o">:</span><span class="w"> </span><span class="p">[[</span><span class="nx">event</span><span class="p">.</span><span class="nx">SmsSid</span><span class="p">,</span><span class="w"> </span><span class="nx">event</span><span class="p">.</span><span class="nx">From</span><span class="p">,</span><span class="w"> </span><span class="nx">event</span><span class="p">.</span><span class="nx">Body</span><span class="p">]],</span><span class="w"></span>
</span><span class="code-line"><span class="w">    </span><span class="p">},</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="p">});</span><span class="w"></span>
</span><span class="code-line">
</span><span class="code-line"><span class="w">  </span><span class="nx">twiml</span><span class="p">.</span><span class="nx">message</span><span class="p">(</span><span class="s1">&#39;The SMS was successfully saved.&#39;</span><span class="p">);</span><span class="w"></span>
</span><span class="code-line"><span class="w">  </span><span class="nx">callback</span><span class="p">(</span><span class="kc">null</span><span class="p">,</span><span class="w"> </span><span class="nx">twiml</span><span class="p">);</span><span class="w"></span>
</span><span class="code-line"><span class="p">};</span><span class="w"></span>
</span></div>

                

Log incoming text messages in Google Sheets

Add a new row to Google Sheets documents when your Twilio phone number receives a text message to track the message content, sender, and arrival time.

Log incoming messages

messaging-customer-quote-portrait.png

“You don’t have to comment out lines of code to explain what a function is doing. The Twilio code is really clear and easily configured.”

Nate Grieb, Product Owner

Try Serverless and Twilio Functions

Get your application up and running with Functions.