Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Make a request to an external API


At some point in your application development process, you may find yourself wanting to include dynamic or external data in your responses to users or as part of your application logic. A common way to incorporate this into your application is by making requests to APIs and processing their responses.

There are as many potential use cases as there are developers, so we can't possibly document every possible situation. Instead, we'll provide you with some examples and useful strategies that we've found over the years for making API calls in your Functions.

There are a wide variety of npm modules available for making HTTP requests to external APIs, including but not limited to:

For the sake of consistency, all examples will use axios, but the same principles will apply to any HTTP request library. These examples are written assuming that a customer is calling your Twilio phone number and expecting a voice response, but these same concepts apply to any application type.


Create and host a Function

create-and-host-a-function page anchor

In order to run any of the following examples, you will first need to create a Function into which you can paste the example code. You can create a Function using the Twilio Console or the Serverless Toolkit as explained below:

ConsoleServerless Toolkit

If you prefer a UI-driven approach, creating and deploying a Function can be done entirely using the Twilio Console and the following steps:

  1. Log in to the Twilio Console and navigate to the Functions tab(link takes you to an external page) . If you need an account, you can sign up for a free Twilio account here(link takes you to an external page) !
  2. Functions are contained within Services . Create a Service by clicking the Create Service(link takes you to an external page) button and providing a name such as test-function .
  3. Once you've been redirected to the new Service, click the Add + button and select Add Function from the dropdown.
  4. This will create a new Protected Function for you with the option to rename it. The name of the file will be path it is accessed from.
  5. Copy any one of the example code snippets from this page that you want to experiment with, and paste the code into your newly created Function. You can quickly switch examples by using the dropdown menu of the code rail.
  6. Click Save to save your Function's contents.
  7. Click Deploy All to build and deploy the Function. After a short delay, your Function will be accessible from: https://<service-name>-<random-characters>-<optional-domain-suffix>.twil.io/<function-path>
    For example: test-function-3548.twil.io/hello-world .

Your Function is now ready to be invoked by HTTP requests, set as the webhook of a Twilio phone number, invoked by a Twilio Studio Run Function Widget, and more!


Set a Function as a webhook

set-a-function-as-a-webhook page anchor

In order for your Function to react to incoming SMS and/or voice calls, it must be set as a webhook for your Twilio number. There are a variety of methods to set a Function as a webhook, as detailed below:

Twilio ConsoleTwilio CLITwilio SDKs

You can use the Twilio Console(link takes you to an external page) UI as a straightforward way of connecting your Function as a webhook:

  1. Log in to the Twilio Console's Phone Numbers page(link takes you to an external page) .
  2. Click on the phone number you'd like to have connected to your Function.
  3. If you want the Function to respond to incoming SMS, find the A Message Comes In option under Messaging . If you want the Function to respond to Voice, find the A Call Comes In option under Voice & Fax .
  4. Select Function from the A Message Comes In or A Call Comes In dropdown.
  5. Select the Service that you are using, then the Environment (this will default to ui unless you have created custom domains ), and finally Function Path of your Function from the respective dropdown menus.
    Connect a Function as a Messaging webhook using the Function dropdowns.
  • Alternatively, you could select Webhook instead of Function, and directly paste in the full URL of the Function.
    Setting a Function as a Messaging webhook using the webhook dropdown option.
  1. Click the Save button.

Make a single API request

make-a-single-api-request page anchor

Before you can make an API request, you'll need to install axios as a Dependency for your Function. Once axios is installed, copy the following code snippet and paste it as the body of a new, public Function, such as /astro-info. Be sure to use the instructions above to connect this Function as the webhook for incoming calls to your Twilio phone number.

Make a single API request

make-a-single-api-request-1 page anchor
1
const axios = require('axios');
2
3
exports.handler = async (context, event, callback) => {
4
// Create a new voice response object
5
const twiml = new Twilio.twiml.VoiceResponse();
6
7
try {
8
// Open APIs From Space: http://open-notify.org
9
// Number of people in space
10
const response = await axios.get(`http://api.open-notify.org/astros.json`);
11
const { number, people } = response.data;
12
13
const names = people.map((astronaut) => astronaut.name).sort();
14
// Create a list formatter to join the names with commas and 'and'
15
// so that the played speech sounds more natural
16
const listFormatter = new Intl.ListFormat('en');
17
18
twiml.say(`There are ${number} people in space.`);
19
twiml.pause({ length: 1 });
20
twiml.say(`Their names are: ${listFormatter.format(names)}`);
21
// Return the final TwiML as the second argument to `callback`
22
// This will render the response as XML in reply to the webhook request
23
// and result in the message being played back to the user
24
return callback(null, twiml);
25
} catch (error) {
26
// In the event of an error, return a 500 error and the error message
27
console.error(error);
28
return callback(error);
29
}
30
};

This code is:

  • Initializing a TwiML Voice Response, for speaking back to the incoming caller
  • Using axios to perform an API request to the astros endpoint, which will return the number of people currently in space, and a list of their names
  • Performing some operations on the data returned by the API, such as isolating the names into a sorted list, and preparing an Intl.ListFormat(link takes you to an external page) object for formatting the names
  • Generating the message to be returned to the caller, and returning it
  • All of this is wrapped in a try/catch block to handle any exceptions, such as a connection error that could be thrown by the API

There are some key points to keep in mind.

Making an HTTP request to an API is what we call an asynchronous operation, meaning the response from the API will come back to us at a later point in time, and we're free to use computing resources on other tasks in the meantime. Calling axios in this code sample creates a Promise, which will ultimately resolve as the data we want, or reject and throw an exception.

The MDN has an excellent series that introduces asynchronous JavaScript(link takes you to an external page) and related concepts.

Note that we've declared this Function as async. This means that we can leverage the await keyword and structure our code in a very readable, sequential manner. The request is still fundamentally a Promise, but we can treat it almost like synchronous code without the need for callback hell(link takes you to an external page) or lengthy then chains. You can learn more about this async/await syntax(link takes you to an external page) at the MDN.

The other key point is that our code only ever calls callback once our code has successfully completed or if an error has occurred. If the await keyword was removed, or we otherwise didn't wait for the API call to complete before invoking the callback method, this would result in incorrect behavior. If we never invoke callback, the Function will run until the 10-second execution limit is reached, resulting in an error and the customer never receiving a response.


Make sequential API requests

make-sequential-api-requests page anchor

Another common situation you may encounter is the need to make one API request, and then a subsequent request which is dependent on having data from the first request. By properly handling Promises in order, and ensuring that callback is not invoked before our requests have finished, you can make any number of sequential requests in your Function as necessary for your use case (while also keeping in mind that the Function has 10 seconds to complete all requests).

Make sequential API requests

make-sequential-api-requests-1 page anchor
1
const axios = require('axios');
2
const qs = require('qs');
3
4
exports.handler = async (context, event, callback) => {
5
// Create a new voice response object
6
const twiml = new Twilio.twiml.VoiceResponse();
7
// A pre-initialized Twilio client is available from the `context` object
8
const twilioClient = context.getTwilioClient();
9
10
try {
11
// Open APIs From Space: http://open-notify.org
12
// Number of people in space
13
const response = await axios.get(`http://api.open-notify.org/astros.json`);
14
const { number, people } = response.data;
15
16
const names = people.map((astronaut) => astronaut.name).sort();
17
18
// Select a random astronaut
19
const astronautName = names[Math.floor(Math.random() * names.length)];
20
// Search Wikipedia for any article's about the astronaut
21
const { data: wikipediaResult } = await axios.get(
22
`https://en.wikipedia.org/w/api.php?${qs.stringify({
23
origin: '*',
24
action: 'opensearch',
25
search: astronautName,
26
})}`
27
);
28
// Attempt to select the first relevant article from the nested result
29
const article = wikipediaResult[3] ? wikipediaResult[3][0] : undefined;
30
// Create a list formatter to join the names with commas and 'and'
31
// so that the played speech sounds more natural
32
const listFormatter = new Intl.ListFormat('en');
33
34
twiml.say(`There are ${number} people in space.`);
35
twiml.pause({ length: 1 });
36
twiml.say(`Their names are: ${listFormatter.format(names)}`);
37
// If there's a defined article for the astronaut, message the link to the user
38
// and tell them they've been sent a message
39
if (article) {
40
// Use `messages.create` to send a text message to the user that
41
// is separate from this call and includes the article
42
await twilioClient.messages.create({
43
to: event.From,
44
from: context.TWILIO_PHONE_NUMBER,
45
body: `Learn more about ${astronautName} on Wikipedia at: ${article}`,
46
});
47
48
twiml.pause({ length: 1 });
49
twiml.say(
50
`We've just sent you a message with a Wikipedia article about
51
${astronautName}, enjoy!`
52
);
53
}
54
55
// Return the final TwiML as the second argument to `callback`
56
// This will render the response as XML in reply to the webhook request
57
// and result in the message being played back to the user
58
return callback(null, twiml);
59
} catch (error) {
60
// In the event of an error, return a 500 error and the error message
61
console.error(error);
62
return callback(error);
63
}
64
};

Similar to the previous example, copy the following code snippet and paste it as the body of a new, public Function, such as /detailed-astro-info. In addition, you will need to install the qs module as a Dependency so that we can make an API request that includes search parameters(link takes you to an external page). Also, for the text messaging to work, you'll need to set your Twilio phone number as an environment variable titled TWILIO_PHONE_NUMBER.

This code is:

  • Initializing a TwiML Voice Response, for speaking back to the incoming caller
  • Using axios to perform an API request to the astros endpoint, which will return the number of people currently in space and a list of their names
  • Performing some operations on the data, and randomly selecting one of the astronaut names
  • Performing a second API request. This time, querying Wikipedia for any articles about the astronaut, with their name as the search term.
  • Generating the message to be returned to the caller, sending the caller a text message containing a Wikipedia article (if one is found), and returning the voice message to the caller
  • All of this is wrapped in a try/catch block to handle any exceptions, such as a connection error that could be thrown by either API

Make parallel API requests

make-parallel-api-requests page anchor

Frequently in applications, we also run into situations where we could make a series of requests one after another, but we can deliver a better and faster experience to users if we perform some requests at the same time.

We can accomplish this in JavaScript by initiating multiple requests, and awaiting their results in parallel using a built-in method, such as Promise.all(link takes you to an external page).

To get started, copy the following code snippet and paste it as the body of a new, public Function, such as /space-info.

In addition to the axios and qs dependencies installed for previous examples, you will want to get a free API key from positionstack(link takes you to an external page). This will enable you to perform reverse geolocation on the International Space Station(link takes you to an external page) or ISS. Set the value of the API key to an environment variable named POSITIONSTACK_API_KEY.

1
const axios = require('axios');
2
const qs = require('qs');
3
4
exports.handler = async (context, event, callback) => {
5
// Create a new voice response object
6
const twiml = new Twilio.twiml.VoiceResponse();
7
8
try {
9
// Open APIs From Space: http://open-notify.org
10
const openNotifyUri = 'http://api.open-notify.org';
11
// Create a promise for each API call which can be made
12
// independently of each other
13
14
// Number of people in space
15
const getAstronauts = axios.get(`${openNotifyUri}/astros.json`);
16
// The current position of the ISS
17
const getIss = axios.get(`${openNotifyUri}/iss-now.json`);
18
19
// Wait for both requests to be completed in parallel instead of sequentially
20
const [astronauts, iss] = await Promise.all([getAstronauts, getIss]);
21
22
const { number, people } = astronauts.data;
23
const { latitude, longitude } = iss.data.iss_position;
24
25
const names = people.map((astronaut) => astronaut.name).sort();
26
27
// We can use reverse geocoding to convert the latitude and longitude
28
// of the ISS to a human-readable location. We'll use positionstack.com
29
// since they provide a free API.
30
// Be sure to set your positionstack API key as an environment variable!
31
const { data: issLocation } = await axios.get(
32
`http://api.positionstack.com/v1/reverse?${qs.stringify({
33
access_key: context.POSITIONSTACK_API_KEY,
34
query: `${latitude},${longitude}`,
35
})}`
36
);
37
38
const { label: location } = issLocation.data[0] || 'an unknown location';
39
40
// Create a list formatter to join the names with commas and 'and'
41
// so that the played speech sounds more natural
42
const listFormatter = new Intl.ListFormat('en');
43
44
twiml.say(`There are ${number} people in space.`);
45
twiml.pause({ length: 1 });
46
twiml.say(`Their names are: ${listFormatter.format(names)}`);
47
twiml.pause({ length: 1 });
48
twiml.say(
49
`Also, the International Space Station is currently above ${location}`
50
);
51
// Return the final TwiML as the second argument to `callback`
52
// This will render the response as XML in reply to the webhook request
53
// and result in the message being played back to the user
54
return callback(null, twiml);
55
} catch (error) {
56
// In the event of an error, return a 500 error and the error message
57
console.error(error);
58
return callback(error);
59
}
60
};

This code is:

  • Initializing a TwiML Voice Response, for speaking back to the incoming caller
  • Using axios to create two requests to the Open Notify API, one for astronaut information and the other for information about the ISS's location, and storing references to the resulting Promises.
  • await ing the result of both requests simultaneously using Promise.all
  • Performing a second API request, this time to convert the latitude and longitude of the ISS into a human-readable location on Earth, such as "North Pacific Ocean"
  • Generating the message to be returned to the caller based on all of the data gathered so far, and returning the voice message to the caller
  • All of this is wrapped in a try/catch block to handle any exceptions, such as a connection error that could be thrown by any of the APIs

Make a write request to an external API

make-a-write-request-to-an-external-api page anchor

Just as you may need to request data in your Serverless applications, there are numerous reasons why you may want to send data to external APIs. Perhaps your Function responds to incoming text messages from customers and attempts to update an internal record about that customer by sending data to an API that manages customer records. Maybe your Function serves as a means to push messages onto a queue, like SQS(link takes you to an external page) so that some other microservice can handle clearing out that queue.

Regardless of the use case, you are free to make write requests to external APIs from your Functions (assuming you have permission to do so from the API). There are no restrictions imposed on this by Runtime itself.

Depending on the scenario, making a write request will mostly consist of using the same principles from the above examples, but using HTTP verbs such as POST and PUT instead of GET.

The below example demonstrates a simple use case where a Function:

  • Receives an incoming text message
  • Derives an identifier from the incoming phone number
  • Retrieves a record from an API, based on the derived ID
  • Writes an update to that API, and responds to the sender once all operations are complete

Make a write request to an external API

make-a-write-request-to-an-external-api-1 page anchor
1
const axios = require('axios');
2
3
exports.handler = async (context, event, callback) => {
4
// Create a new message response object
5
const twiml = new Twilio.twiml.MessagingResponse();
6
7
// Just for this example, we'll use the first digit of the incoming phone
8
// number to identify the call. You'll want to use a more robust mechanism
9
// for your own Functions, such as the full phone number.
10
const postId = event.From[1];
11
12
// Since we're making multiple requests, we'll create an instance of axios
13
// that includes our API's base URL and any custom headers we might want to
14
// send with each request. This will simplify the GET and POST request paths.
15
// JSONPlaceholder is a fake REST API that you can use for testing and prototyping
16
const instance = axios.create({
17
baseURL: 'https://jsonplaceholder.typicode.com',
18
headers: { 'X-Custom-Header': 'Twilio' },
19
});
20
21
try {
22
// Get the post based on the derived postId
23
// If the postId was 1, this is effectively making a GET request to:
24
// https://jsonplaceholder.typicode.com/posts/1
25
const { data: post } = await instance.get(`/posts/${postId}`);
26
27
const newCount = (post.messageCount || 0) + 1;
28
29
// Use a POST request to "save" the update to the API
30
// In this case, we're merging the new count and message into the
31
// existing post object.
32
const update = await instance.post('/posts/', {
33
...post,
34
messageCount: newCount,
35
latestMessage: event.Body,
36
});
37
38
console.log(update.data);
39
40
// Add a message to the response to let the user know that everything worked
41
twiml.message(
42
`Message received! This was message ${newCount} from your phone number. 🎉`
43
);
44
return callback(null, twiml);
45
} catch (error) {
46
// As always with async functions, you need to be sure to handle errors
47
console.error(error);
48
// Add a message to the response to let the user know that something went wrong
49
twiml.message(`We received your message, but something went wrong 😭`);
50
return callback(error);
51
}
52
};

Make a write request in other formats

make-a-write-request-in-other-formats page anchor

Some APIs may not accept write requests that are formatted using JSON (Content-Type: application/json). The approach to handling this situation varies depending on the expected Content-Type and which HTTP library you are using.

One example of a common alternative, which you'll encounter when using some of Twilio's APIs without the aid of a helper library, is Content-Type: application/x-www-form-urlencoded. As detailed in the axios documentation(link takes you to an external page) and shown in the example below, this requires some slight modifications to the data that you send, the Headers attached to the request, or a combination of both.

Make a write request using x-www-form-urlencoded format

make-a-write-request-using-x-www-form-urlencoded-format page anchor
1
const axios = require('axios');
2
const qs = require('qs');
3
4
exports.handler = async (context, event, callback) => {
5
// Create a new message response object
6
const twiml = new Twilio.twiml.MessagingResponse();
7
8
// Just for this example, we'll use the first digit of the incoming phone
9
// number to identify the call. You'll want to use a more robust mechanism
10
// for your own Functions, such as the full phone number.
11
const postId = event.From[1];
12
13
// Since we're making multiple requests, we'll create an instance of axios
14
// that includes our API's base URL and any custom headers we might want to
15
// send with each request. This will simply be our GET and POST request paths.
16
// JSONPlaceholder is a fake REST API that you can use for testing and prototyping
17
const instance = axios.create({
18
baseURL: 'https://jsonplaceholder.typicode.com',
19
headers: { 'X-Custom-Header': 'Twilio' },
20
});
21
22
try {
23
// Get the post based on the derived postId
24
// If the postId was 1, this is effectively making a GET request to:
25
// https://jsonplaceholder.typicode.com/posts/1
26
const { data: post } = await instance.get(`/posts/${postId}`);
27
28
const newCount = (post.messageCount || 0) + 1;
29
30
// Like before, we're merging the new count and message into the
31
// existing post object
32
// In order to send this data in the application/x-www-form-urlencoded
33
// format, the payload must be encoded via a utility such as qs
34
const data = qs.stringify({
35
...post,
36
messageCount: newCount,
37
latestMessage: event.Body,
38
});
39
40
// Use a POST request to "save" the update to the API
41
const update = await instance.post('/posts/', data);
42
43
console.log(update.data);
44
45
// Add a message to the response to let the user know that everything worked
46
twiml.message(
47
`Message received! This was message ${newCount} from your phone number. 🎉`
48
);
49
return callback(null, twiml);
50
} catch (error) {
51
// As always with async functions, you need to be sure to handle errors
52
console.error(error);
53
// Add a message to the response to let the user know that something went wrong
54
twiml.message(`We received your message, but something went wrong 😭`);
55
return callback(error);
56
}
57
};

API requests aren't always successful. Sometimes the API's server may be under too much load and unable to handle your request, or something simply happened to go wrong with the connection between your server and the API.

A standard approach to this situation is to retry the same request but after a delay. If that fails, subsequent retries are performed but with increasing amounts of delay (also known as exponential backoff(link takes you to an external page)). You could implement this yourself, or use a module such as p-retry(link takes you to an external page) to handle this logic for you. This behavior is also built into got by default.

To see this more explicitly configured, the following code examples implement some previous examples, but while utilizing an unstable API that only sometimes successfully returns the desired data.

(warning)

Warning

V5 and newer versions of p-retry are exported as ES Modules(link takes you to an external page), and Functions currently do not support the necessary import syntax. To utilize p-retry (or any other ES Module package) in the meantime, you will need to import it using dynamic import syntax(link takes you to an external page) inside of your handler method, as highlighted in the following examples.

Configure retries for an API request

configure-retries-for-an-api-request page anchor
1
const axios = require('axios');
2
3
const getAstronauts = () => axios.get('https://unstable-5604.twil.io/astros');
4
5
exports.handler = async (context, event, callback) => {
6
// We need to asynchronously import p-retry since it is an ESM module
7
const { default: pRetry } = await import('p-retry');
8
// Create a new voice response object
9
const twiml = new Twilio.twiml.VoiceResponse();
10
11
try {
12
let attempts = 1;
13
// Open APIs From Space: http://open-notify.org
14
// Number of people in space
15
const response = await pRetry(getAstronauts, {
16
retries: 3,
17
onFailedAttempt: ({ attemptNumber, retriesLeft }) => {
18
attempts = attemptNumber;
19
console.log(
20
`Attempt ${attemptNumber} failed. There are ${retriesLeft} retries left.`
21
);
22
// 1st request => "Attempt 1 failed. There are 3 retries left."
23
// 2nd request => "Attempt 2 failed. There are 2 retries left."
24
// …
25
},
26
});
27
const { number, people } = response.data;
28
29
const names = people.map((astronaut) => astronaut.name).sort();
30
// Create a list formatter to join the names with commas and 'and'
31
// so that the played speech sounds more natural
32
const listFormatter = new Intl.ListFormat('en');
33
34
twiml.say(`There are ${number} people in space.`);
35
twiml.pause({ length: 1 });
36
twiml.say(`Their names are: ${listFormatter.format(names)}`);
37
// If retries were necessary, add that information to the response
38
if (attempts > 1) {
39
twiml.pause({ length: 1 });
40
twiml.say(`It took ${attempts} attempts to retrieve this information.`);
41
}
42
// Return the final TwiML as the second argument to `callback`
43
// This will render the response as XML in reply to the webhook request
44
// and result in the message being played back to the user
45
return callback(null, twiml);
46
} catch (error) {
47
// In the event of an error, return a 500 error and the error message
48
console.error(error);
49
return callback(error);
50
}
51
};

Configure retries for parallel API requests

configure-retries-for-parallel-api-requests page anchor
1
const axios = require('axios');
2
const qs = require('qs');
3
4
// The root URL for an API which is known to fail on occasion
5
const unstableSpaceUri = 'https://unstable-5604.twil.io';
6
// We'll declare these functions outside of the handler since they have no
7
// dependencies on other values, and this will tidy up our pRetry calls.
8
const astronautRequest = () => axios.get(`${unstableSpaceUri}/astros`);
9
const issRequest = () => axios.get(`${unstableSpaceUri}/iss`);
10
// Use a common object for retry configuration to DRY up our code :)
11
const retryConfig = (reqName) => ({
12
retries: 3,
13
onFailedAttempt: () => console.log(`Retrying ${reqName}...`),
14
});
15
16
exports.handler = async (context, event, callback) => {
17
// We need to asynchronously import p-retry since it is an ESM module
18
const { default: pRetry } = await import('p-retry');
19
// Create a new voice response object
20
const twiml = new Twilio.twiml.VoiceResponse();
21
22
try {
23
// Create a promise with retry for each API call that can be made
24
// independently of each other
25
const getAstronauts = pRetry(astronautRequest, retryConfig('astros'));
26
const getIss = pRetry(issRequest, retryConfig('iss'));
27
// pRetry returns a promise, so we can still use Promise.all to await
28
// the result of both requests in parallel with retry and backoff enabled!
29
const [astronauts, iss] = await Promise.all([getAstronauts, getIss]);
30
31
const { number, people } = astronauts.data;
32
const { latitude, longitude } = iss.data.iss_position;
33
34
const names = people.map((astronaut) => astronaut.name).sort();
35
36
// We can use reverse geocoding to convert the latitude and longitude
37
// of the ISS to a human-readable location. We'll use positionstack.com
38
// since they provide a free API.
39
// Be sure to set your positionstack API key as an environment variable!
40
const { data: issLocation } = await pRetry(
41
() =>
42
axios.get(
43
`http://api.positionstack.com/v1/reverse?${qs.stringify({
44
access_key: context.POSITIONSTACK_API_KEY,
45
query: `${latitude},${longitude}`,
46
})}`
47
),
48
retryConfig('iss location')
49
);
50
51
const { label } = issLocation.data[0] || 'an unknown location';
52
53
// Create a list formatter to join the names with commas and 'and'
54
// so that the played speech sounds more natural
55
const listFormatter = new Intl.ListFormat('en');
56
57
twiml.say(`There are ${number} people in space.`);
58
twiml.pause({ length: 1 });
59
twiml.say(`Their names are: ${listFormatter.format(names)}`);
60
twiml.pause({ length: 1 });
61
twiml.say(
62
`Also, the International Space Station is currently above ${label}`
63
);
64
// Return the final TwiML as the second argument to `callback`
65
// This will render the response as XML in reply to the webhook request
66
// and result in the message being played back to the user
67
return callback(null, twiml);
68
} catch (error) {
69
// In the event of an error, return a 500 error and the error message
70
console.error(error);
71
return callback(error);
72
}
73
};

Rate this page: