Skip to contentSkip to navigationSkip to topbar
On this page

Function


Functions are JavaScript Node.js code that execute at a particular domain.

The steps to create Functions are as follows:

  1. Create a Function (this resource)
  2. Create a Function Version via serverless.twilio.com

We will need the Function Version SID to include this Function in a Build.


Function Properties

function-properties page anchor
Property nameTypeRequiredDescriptionChild properties
sidSID<ZH>Optional
Not PII

The unique string that we created to identify the Function resource.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34

account_sidSID<AC>Optional

The SID of the Account that created the Function resource.

Pattern: ^AC[0-9a-fA-F]{32}$Min length: 34Max length: 34

service_sidSID<ZS>Optional

The SID of the Service that the Function resource is associated with.

Pattern: ^ZS[0-9a-fA-F]{32}$Min length: 34Max length: 34

friendly_namestringOptional
PII MTL: 7 days

The string that you assigned to describe the Function resource. It can be a maximum of 255 characters.


date_updatedstring<date-time>Optional

The date and time in GMT when the Function resource was last updated specified in ISO 8601(link takes you to an external page) format.


urlstring<uri>Optional

The absolute URL of the Function resource.


linksobject<uri-map>Optional

The URLs of nested resources of the Function resource.


Create a Function resource

create-a-function-resource page anchor
POST https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions

Path parameters

path-parameters page anchor
Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to create the Function resource under.

Encoding type:application/x-www-form-urlencoded
SchemaExample
Property nameTypeRequiredDescriptionChild properties
FriendlyNamestringrequired

A descriptive string that you create to describe the Function resource. It can be a maximum of 255 characters.

Create a FunctionLink to code sample: Create a Function
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createFunction() {
11
const func = await client.serverless.v1
12
.services("ServiceSid")
13
.functions.create({ friendlyName: "FriendlyName" });
14
15
console.log(func.sid);
16
}
17
18
createFunction();

Output

1
{
2
"sid": "ZH00000000000000000000000000000000",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ServiceSid",
5
"friendly_name": "FriendlyName",
6
"date_created": "2018-11-10T20:00:00Z",
7
"date_updated": "2018-11-10T20:00:00Z",
8
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000",
9
"links": {
10
"function_versions": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions"
11
}
12
}

Fetch a Function resource

fetch-a-function-resource page anchor
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}

Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to fetch the Function resource from.


SidSID<ZH>required

The SID of the Function resource to fetch.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function fetchFunction() {
11
const func = await client.serverless.v1
12
.services("ServiceSid")
13
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.fetch();
15
16
console.log(func.sid);
17
}
18
19
fetchFunction();

Output

1
{
2
"sid": "ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ServiceSid",
5
"friendly_name": "test-function",
6
"date_created": "2018-11-10T20:00:00Z",
7
"date_updated": "2018-11-10T20:00:00Z",
8
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000",
9
"links": {
10
"function_versions": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions"
11
}
12
}

Read multiple Function resources

read-multiple-function-resources page anchor
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions

Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to read the Function resources from.

Property nameTypeRequiredPIIDescription
PageSizeintegerOptional

How many resources to return in each list page. The default is 50, and the maximum is 1000.

Minimum: 1Maximum: 1000

PageintegerOptional

The page index. This value is simply for client state.

Minimum: 0

PageTokenstringOptional

The page token. This is provided by the API.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function listFunction() {
11
const funcs = await client.serverless.v1
12
.services("ServiceSid")
13
.functions.list({ limit: 20 });
14
15
funcs.forEach((f) => console.log(f.sid));
16
}
17
18
listFunction();

Output

1
{
2
"functions": [],
3
"meta": {
4
"first_page_url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions?PageSize=50&Page=0",
5
"key": "functions",
6
"next_page_url": null,
7
"page": 0,
8
"page_size": 50,
9
"previous_page_url": null,
10
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions?PageSize=50&Page=0"
11
}
12
}

Update a Function resource

update-a-function-resource page anchor
POST https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}

Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to update the Function resource from.


SidSID<ZH>required

The SID of the Function resource to update.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34
Encoding type:application/x-www-form-urlencoded
SchemaExample
Property nameTypeRequiredDescriptionChild properties
FriendlyNamestringrequired

A descriptive string that you create to describe the Function resource. It can be a maximum of 255 characters.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function updateFunction() {
11
const func = await client.serverless.v1
12
.services("ServiceSid")
13
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.update({ friendlyName: "FriendlyName" });
15
16
console.log(func.sid);
17
}
18
19
updateFunction();

Output

1
{
2
"sid": "ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ServiceSid",
5
"friendly_name": "FriendlyName",
6
"date_created": "2018-11-10T20:00:00Z",
7
"date_updated": "2018-11-10T20:00:00Z",
8
"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000",
9
"links": {
10
"function_versions": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions/ZH00000000000000000000000000000000/Versions"
11
}
12
}

Delete a Function resource

delete-a-function-resource page anchor
DELETE https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}

(information)

Info

A Function will fail to delete if any of its Versions are referenced by an active Build.

If necessary, create a new Build that does not include the Function to be deleted, and delete the Function only once the Build has completed.

Property nameTypeRequiredPIIDescription
ServiceSidstringrequired

The SID of the Service to delete the Function resource from.


SidSID<ZH>required

The SID of the Function resource to delete.

Pattern: ^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function deleteFunction() {
11
await client.serverless.v1
12
.services("ServiceSid")
13
.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.remove();
15
}
16
17
deleteFunction();

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.