Twilio binding for Azure Functions

This article explains how to send text messages by using Twilio bindings in Azure Functions. Azure Functions supports output bindings for Twilio.

This is reference information for Azure Functions developers. If you're new to Azure Functions, start with the following resources:

Packages - Functions 1.x

The Twilio bindings are provided in the Microsoft.Azure.WebJobs.Extensions.Twilio NuGet package, version 1.x. Source code for the package is in the azure-webjobs-sdk GitHub repository.

The following table tells how to add support for this binding in each development environment.

Development environment To add support in
Functions 1.x
Local development - C# class library Install the package
Local development - C# script, JavaScript, F# Automatic
Portal development Automatic

Packages - Functions 2.x and higher

The Twilio bindings are provided in the Microsoft.Azure.WebJobs.Extensions.Twilio NuGet package, version 3.x. Source code for the package is in the azure-webjobs-sdk GitHub repository.

Add support in you preferred development environment using the following methods.

Development environment Application type To add support
Visual Studio C# class library Install the NuGet package
Visual Studio Code Based on core tools Register the extension bundle

Installing the Azure Tools extension is recommended.
Any other editor/IDE Based on core tools Register the extension bundle
Azure Portal Online only in portal Installs when adding a binding

See Update your extensions to update existing binding extensions without having to republish your function app.

Example - Functions 2.x and higher

The following example shows a C# function that sends a text message when triggered by a queue message.

using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
namespace TwilioQueueOutput
{
    public static class QueueTwilio
    {
        [FunctionName("QueueTwilio")]
        [return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
        public static CreateMessageOptions Run(
        [QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] JObject order,
        ILogger log)
        {
            log.LogInformation($"C# Queue trigger function processed: {order}");

            var message = new CreateMessageOptions(new PhoneNumber(order["mobileNumber"].ToString()))
            {
                Body = $"Hello {order["name"]}, thanks for your order!"
            };

            return message;
        }
    }
}

This example uses the TwilioSms attribute with the method return value. An alternative is to use the attribute with an out CreateMessageOptions parameter or an ICollector<CreateMessageOptions> or IAsyncCollector<CreateMessageOptions> parameter.

Attributes and annotations

In C# class libraries, use the TwilioSms attribute.

For information about attribute properties that you can configure, see Configuration. Here's a TwilioSms attribute example in a method signature:

[FunctionName("QueueTwilio")]
[return: TwilioSms(AccountSidSetting = "TwilioAccountSid", AuthTokenSetting = "TwilioAuthToken", From = "+1425XXXXXXX")]
public static CreateMessageOptions Run(
[QueueTrigger("myqueue-items", Connection = "AzureWebJobsStorage")] JObject order, ILogger log)
{
    ...
}

For a complete example, see C# example.

Configuration

The following table explains the binding configuration properties that you set in the function.json file and the TwilioSms attribute.

v1 function.json property v2 function.json property Attribute property Description
type type must be set to twilioSms.
direction direction must be set to out.
name name Variable name used in function code for the Twilio SMS text message.
accountSid accountSidSetting AccountSidSetting This value must be set to the name of an app setting that holds your Twilio Account Sid (TwilioAccountSid). If not set, the default app setting name is "AzureWebJobsTwilioAccountSid".
authToken authTokenSetting AuthTokenSetting This value must be set to the name of an app setting that holds your Twilio authentication token (TwilioAccountAuthToken). If not set, the default app setting name is "AzureWebJobsTwilioAuthToken".
to N/A - specify in code To This value is set to the phone number that the SMS text is sent to.
from from From This value is set to the phone number that the SMS text is sent from.
body body Body This value can be used to hard code the SMS text message if you don't need to set it dynamically in the code for your function.

When you're developing locally, app settings go into the local.settings.json file.

Next steps