FAQ - Twilio
Register for SIGNAL by 8/31 for $250 off. Register now.

FAQ

Why does my Function return 'runtime application timed out'?

There are two possiable reasons for why your Function has completed with the error: 'runtime application timed out'. 

The most common reason is that your Function has exceeded the 5 second execution time limit. You can determine this by looking at the execution logs on the Function instance page. The last log line after execution will tell you how many milliseconds the Function took to execute. If the processing time is greater than 5,000 milliseconds then your Function was terminated by Twilio.

The other more subtle reason your Function ended with an application timeout is because of an incorrect invocation of callback(). If the callback() method is not being called or is unreachable your Function will continue executing until it reaches the timelimit and ultimately fail. The Function Execution documentation provides extensive details on the funcitonality and usage of the callback() method. Below are several examples of how to correctly use callback() to complete execution and emit a response.

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
	// Fetch already initialized Twilio REST client
	const twilioClient = context.getTwilioClient();

	twilioClient.messages.create({
		from: from,
		to: to,
		body: 'create using callback'
	}, function(err, result) {
		console.log('Created message using callback');
		console.log(result.sid);
		callback();
	});
}
Example of how to appropriately use callback() with an asynchronous HTTP request
Complete Execution with Asynchronous HTTP Request

Example of how to appropriately use callback() with an asynchronous HTTP request

Why isn't my code running?

The most common reason we have seen that a Function appears not to run is misue of callback(). If the callback() method is not being called or is unreachable your Function will continue executing until it reaches the timelimit and ultimately fail. This makes it appear as if your Function is not executing because there is no response emitted. The Function Execution documentation provides extensive details on the funcitonality and usage of the callback() method. Below are several examples of how to correctly use callback() to complete execution and emit a response.

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
	// Providing neither error or response will result in a 200 OK
	callback();
}
Example of how to return an empty HTTP 200 OK
Return a Simple Successful Response

Example of how to return an empty HTTP 200 OK

How do I construct Voice TwiML?

You can generate Voice TwiML using the Twilio Node library which comes packaged within your Function. 

Loading Code Samples...
Language
Format:
  • TwiML
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse()
  twiml.dial().sip("sip:jack@example.com")
  callback(null, twiml);
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Sip>sip:jack@example.com</Sip>
  </Dial
</Response>
Example of how to construct a Voice TwiML Response
Construct Voice TwiML

Example of how to construct a Voice TwiML Response

How do I construct Messaging TwiML?

You can generate Messaging TwiML using the Twilio Node library which comes packaged within your Function. 

Loading Code Samples...
Language
Format:
  • TwiML
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  twiml.message('The Robots are coming! Head for the hills!');
  callback(null, twiml);
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Message>The Robots are coming! Head for the hills!</Message>
</Response>
Example of how to construct Messaging TwiML
Construct Messaging TwiML

Example of how to construct Messaging TwiML

How do I return JSON?

Simply return your object as outlined in the Function Execution documentation.

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {

	// Construct an Object
	let responseObject = { "result": "winner winner!" };

	// Twilio Functions will detect an object, serialize to JSON and set the content-type header to application/json automatically
	callback(null, responseObject);
}
Example of how to return JSON in HTTP 200 OK
Return a Successful JSON Response

Example of how to return JSON in HTTP 200 OK

How do I send CORS headers?

You can send CORS headers by using the Twilio Response object described in the Function Execution documentation.

Loading Code Samples...
Language
exports.handler = function(context, event, callback) {
	let response = new Twilio.Response();
	
	// Build list of headers
	let headers = {
    "Access-Control-Allow-Origin": "example.com",
    "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE",
    "Access-Control-Allow-Headers": "Content-Type"
	};
	
	// Set headers in response
	response.setHeaders(headers);

	callback(null, response);
}
Example of setting multiple headers using Twilio Response
Set Multiple HTTP Headers in a Response

Example of setting multiple headers using Twilio Response

Is there a Functions or Assets REST API?

Not yet, currently, Functions and Assets can only be accessed through the Twilio Console. But keep and eye on this space for updates. Please reach out to us via support or our Google Group to express interest in this feature. 

Can I execute my Functions on a schedule?

Not today, currently, Functions are event driven and can only be invoked by HTTP. 

Can I see examples of existing Functions?

Absolutely! In addition to our Programmable SMS and Programmable Voice quickstarts, you can find a variety of templates within our catalogue within the Create Function page.

How many Functions can I create?

During the beta period, we currently have a limit of 50 functions per account.

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

Loading Code Samples...
exports.handler = function(context, event, callback) {
	// Fetch already initialized Twilio REST client
	const twilioClient = context.getTwilioClient();

	twilioClient.messages.create({
		from: from,
		to: to,
		body: 'create using callback'
	}, function(err, result) {
		console.log('Created message using callback');
		console.log(result.sid);
		callback();
	});
}
exports.handler = function(context, event, callback) {
	// Providing neither error or response will result in a 200 OK
	callback();
}
Format:
  • TwiML
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse()
  twiml.dial().sip("sip:jack@example.com")
  callback(null, twiml);
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Dial>
    <Sip>sip:jack@example.com</Sip>
  </Dial
</Response>
Format:
  • TwiML
exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.MessagingResponse();
  twiml.message('The Robots are coming! Head for the hills!');
  callback(null, twiml);
}
<?xml version="1.0" encoding="UTF-8"?>
<Response>
  <Message>The Robots are coming! Head for the hills!</Message>
</Response>
exports.handler = function(context, event, callback) {

	// Construct an Object
	let responseObject = { "result": "winner winner!" };

	// Twilio Functions will detect an object, serialize to JSON and set the content-type header to application/json automatically
	callback(null, responseObject);
}
exports.handler = function(context, event, callback) {
	let response = new Twilio.Response();
	
	// Build list of headers
	let headers = {
    "Access-Control-Allow-Origin": "example.com",
    "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE",
    "Access-Control-Allow-Headers": "Content-Type"
	};
	
	// Set headers in response
	response.setHeaders(headers);

	callback(null, response);
}