Function
Functions are JavaScript Node.js code that execute at a particular domain.
The steps to create Functions are as follows:
- Create a Function (this resource)
- Create a Function Version via
serverless.twilio.com
We will need the Function Version SID to include this Function in a Build.
The unique string that we created to identify the Function resource.
^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 34The SID of the Account that created the Function resource.
^AC[0-9a-fA-F]{32}$Min length: 34Max length: 34The SID of the Service that the Function resource is associated with.
^ZS[0-9a-fA-F]{32}$Min length: 34Max length: 34The string that you assigned to describe the Function resource. It can be a maximum of 255 characters.
The date and time in GMT when the Function resource was created specified in ISO 8601 format.
The date and time in GMT when the Function resource was last updated specified in ISO 8601 format.
The URLs of nested resources of the Function resource.
POST https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions
The SID of the Service to create the Function resource under.
application/x-www-form-urlencodedA 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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function createFunction() {14const func = await client.serverless.v115.services("ServiceSid")16.functions.create({ friendlyName: "FriendlyName" });1718console.log(func.sid);19}2021createFunction();
Response
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}
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}
The SID of the Function resource to fetch.
^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 341// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function fetchFunction() {14const func = await client.serverless.v115.services("ServiceSid")16.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")17.fetch();1819console.log(func.sid);20}2122fetchFunction();
Response
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}
GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions
The SID of the Service to read the Function resources from.
How many resources to return in each list page. The default is 50, and the maximum is 1000.
1Maximum: 1000The page token. This is provided by the API.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function listFunction() {14const functions = await client.serverless.v115.services("ServiceSid")16.functions.list({ limit: 20 });1718functions.forEach((f) => console.log(f.sid));19}2021listFunction();
Response
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}
POST https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}
application/x-www-form-urlencodedA 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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function updateFunction() {14const func = await client.serverless.v115.services("ServiceSid")16.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")17.update({ friendlyName: "FriendlyName" });1819console.log(func.sid);20}2122updateFunction();
Response
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 https://serverless.twilio.com/v1/Services/{ServiceSid}/Functions/{Sid}
The SID of the Function resource to delete.
^ZH[0-9a-fA-F]{32}$Min length: 34Max length: 341// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID at twilio.com/console5// Provision API Keys at twilio.com/console/runtime/api-keys6// and set the environment variables. See http://twil.io/secure7// For local testing, you can use your Account SID and Auth token8const accountSid = process.env.TWILIO_ACCOUNT_SID;9const apiKey = process.env.TWILIO_API_KEY;10const apiSecret = process.env.TWILIO_API_SECRET;11const client = twilio(apiKey, apiSecret, { accountSid: accountSid });1213async function deleteFunction() {14await client.serverless.v115.services("ServiceSid")16.functions("ZHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")17.remove();18}1920deleteFunction();