Quickstart
Like the Console UI and the Serverless Toolkit, create and manage your Services, Functions, and Assets with the Serverless API. This API lets you create custom Build and Deployment automations that suit your specific needs.
Classic Functions and Assets unavailable through the API
This API can't access classic Functions and Assets. Nor can the classic Functions and Assets interface access API-generated Functions and Assets.
-
Create a Service that contains your Environments. The value of the
uniqueNameparameter becomes part of your hostname for your Function.For example: A Service with the unique name
Hootcreates a hostname ofhttps://hoot-3234.twil.io.View the Create Service example1// 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 createService() {14const service = await client.serverless.v1.services.create({15friendlyName: "testing",16includeCredentials: true,17uniqueName: "demo",18});1920console.log(service.sid);21}2223createService();Response
1{2"sid": "ZS00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"friendly_name": "testing",5"unique_name": "demo",6"include_credentials": true,7"ui_editable": false,8"domain_base": "service-unique-1234",9"date_created": "2018-11-10T20:00:00Z",10"date_updated": "2018-11-10T20:00:00Z",11"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000",12"links": {13"environments": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments",14"functions": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Functions",15"assets": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Assets",16"builds": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds"17}18}The response contains a Service SID, in the format
ZSXX..XX. -
Create an Environment using the Service SID.
View the Create Environment example1// 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 createEnvironment() {14const environment = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.environments.create({17domainSuffix: "dev",18uniqueName: "dev-Environment",19});2021console.log(environment.sid);22}2324createEnvironment();Response
1{2"sid": "ZE00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"build_sid": null,6"unique_name": "dev-Environment",7"domain_suffix": "dev",8"domain_name": "foobar-1234-stage.twil.io",9"date_created": "2018-11-10T20:00:00Z",10"date_updated": "2018-11-10T20:00:00Z",11"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000",12"links": {13"variables": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Variables",14"deployments": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Deployments",15"logs": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Logs"16}17}-
The request returns an empty Environment as a hostname like
demo-X4HX-dev.twil.io. -
To view the hostname, fetch the Environment for your Environment using the
ZEXX..XXSID.View the Fetch Environment Hostname example1// 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 fetchEnvironment() {14const environment = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.environments("ZE00000000000000000000000000000000")17.fetch();1819console.log(environment.domainName);20}2122fetchEnvironment();Response
1{2"sid": "ZE00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"build_sid": "ZB00000000000000000000000000000000",6"unique_name": "testing-environment",7"domain_suffix": "testing",8"domain_name": "foobar-1234-testing.twil.io",9"date_created": "2018-11-10T20:00:00Z",10"date_updated": "2018-11-10T20:00:00Z",11"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000",12"links": {13"variables": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Variables",14"deployments": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Deployments",15"logs": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Logs"16}17}
-
-
Create a Function with a
FriendlyNameparameter.- The response includes a Function SID in the format
ZHxxx. - Save this SID. To create the first version of this Function, you need this SID.
View the Create Function example1// 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("ZS00000000000000000000000000000000")16.functions.create({ friendlyName: "firstfunc" });1718console.log(func.sid);19}2021createFunction();Response
1{2"sid": "ZH00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"friendly_name": "firstfunc",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} - The response includes a Function SID in the format
-
Write the code for your Function and save it as
firstfunc.js.View the example Function codeThe following example randomly determines whether Thanos spares the universe:
1exports.handler = (context, event, callback) => {2const sparedByThanos = Math.random() > 0.5;34callback(null, {5sparedByThanos,6quote: 'You should have gone for the head.'7});8}; -
Create the first version of your Function using a
POSTrequest.- Function or Asset versions define the path, Visibility (public, protected, or private), and file content.
- Replace
ZSXX..XXwith your Service SID andZHXX..XXwith your Function SID. - The response returns a Version SID (
ZNXX..XX).
View the Upload Function example1const fs = require('fs');2// Before running this code, install "form-data" and "axios" using `npm install form-data axios`3const FormData = require('form-data');4const axios = require('axios');56// Provision API Keys at twilio.com/console/runtime/api-keys7// and set the environment variables. See https://twil.io/secure8const apiKey = process.env.TWILIO_API_KEY;9const apiSecret = process.env.TWILIO_API_SECRET;1011const serviceSid = 'ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';12const functionSid = 'ZHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';1314const serviceUrl = `https://serverless-upload.twilio.com/v1/Services/${serviceSid}`;15const uploadUrl = `${serviceUrl}/Functions/${functionSid}/Versions`;1617const form = new FormData();18form.append('Path', '/thanos');19form.append('Visibility', 'public');20form.append('Content', fs.createReadStream('firstfunc.js'), {21contentType: 'application/javascript',22});2324// Create a new Function Version25axios26.post(uploadUrl, form, {27auth: {28username: apiKey,29password: apiSecret,30},31headers: form.getHeaders(),32})33.then((response) => {34const newVersionSid = response.data.sid;35console.log(newVersionSid);36}); -
To compile your Function and Asset versions into a single, deployable package, create a Build.
- Twilio stores this Build on your behalf.
- The response returns a
statusproperty.
View the Create Build without dependencies example1// 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 createBuild() {14const build = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.builds.create({17functionVersions: ["ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],18});1920console.log(build.sid);21}2223createBuild();Response
1{2"sid": "ZB00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"asset_versions": [6{7"sid": "ZN00000000000000000000000000000000",8"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",9"service_sid": "ZS00000000000000000000000000000000",10"asset_sid": "ZH00000000000000000000000000000000",11"date_created": "2018-11-10T20:00:00Z",12"path": "/asset-path",13"visibility": "PUBLIC"14}15],16"function_versions": [17"ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"18],19"dependencies": [20{21"name": "twilio",22"version": "3.29.2"23},24{25"name": "@twilio/runtime-handler",26"version": "1.0.1"27}28],29"runtime": "node24",30"status": "building",31"date_created": "2018-11-10T20:00:00Z",32"date_updated": "2018-11-10T20:00:00Z",33"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000",34"links": {35"build_status": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000/Status"36}37}View the Create Build with dependencies exampleWhen you create a Build, specify dependencies that your code requires. Provide the
Dependenciesparameter as a JSON-stringified array of packagenameandversionpairs.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 createBuild() {14const build = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.builds.create({17dependencies: JSON.stringify([{ name: "randomcolor", version: "0.5.4" }]),18functionVersions: ["ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],19});2021console.log(build.sid);22}2324createBuild();Response
1{2"sid": "ZB00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"asset_versions": [6{7"sid": "ZN00000000000000000000000000000000",8"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",9"service_sid": "ZS00000000000000000000000000000000",10"asset_sid": "ZH00000000000000000000000000000000",11"date_created": "2018-11-10T20:00:00Z",12"path": "/asset-path",13"visibility": "PUBLIC"14}15],16"function_versions": [17"ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"18],19"dependencies": "[{\"name\": \"randomcolor\", \"version\": \"0.5.4\"}]",20"runtime": "node24",21"status": "building",22"date_created": "2018-11-10T20:00:00Z",23"date_updated": "2018-11-10T20:00:00Z",24"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000",25"links": {26"build_status": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000/Status"27}28} -
To check the status of the Build, repeat sending the following
GETrequest until"status": "completed".View the Fetch Build example1// 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 fetchBuild() {14const build = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.builds("ZB00000000000000000000000000000000")17.fetch();1819console.log(build.status);20}2122fetchBuild();Response
1{2"sid": "ZB00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"asset_versions": [6{7"sid": "ZN00000000000000000000000000000000",8"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",9"service_sid": "ZS00000000000000000000000000000000",10"asset_sid": "ZH00000000000000000000000000000000",11"date_created": "2018-11-10T20:00:00Z",12"path": "/asset-path",13"visibility": "PUBLIC"14}15],16"function_versions": [17{18"sid": "ZN00000000000000000000000000000001",19"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",20"service_sid": "ZS00000000000000000000000000000000",21"function_sid": "ZH00000000000000000000000000000001",22"date_created": "2018-11-10T20:00:00Z",23"path": "/function-path",24"visibility": "PUBLIC"25}26],27"dependencies": [28{29"name": "twilio",30"version": "3.29.2"31},32{33"name": "@twilio/runtime-handler",34"version": "1.0.1"35}36],37"runtime": "node24",38"status": "building",39"date_created": "2018-11-10T20:00:00Z",40"date_updated": "2018-11-10T20:00:00Z",41"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000",42"links": {43"build_status": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000/Status"44}45} -
Associate the Build with the Environment you created. Twilio calls this association a Deployment.
View the Create Deployment example1// 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 createDeployment() {14const deployment = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.environments("ZE00000000000000000000000000000000")17.deployments.create({ buildSid: "ZBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" });1819console.log(deployment.sid);20}2122createDeployment();Response
1{2"sid": "ZD00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"environment_sid": "ZE00000000000000000000000000000000",6"build_sid": "ZBXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",7"date_created": "2018-11-10T20:00:00Z",8"date_updated": "2018-11-10T20:00:00Z",9"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Deployments/ZD00000000000000000000000000000000"10}After the Deployment completes, your Function begins serving at your Environment's hostname.
-
To access your function, go to a URL that resembles
https://demo-X4HX-dev.twil.io/thanos. Replacedemo-X4HX-dev.twil.iowith your Environment's hostname.
Upload Assets by following the same pattern you used for Functions:
-
Create an
Asset. -
Create the
Asset Versionwith aPOSTrequest toserverless-uploads.twilio.com. -
When you create the Build, include the
AssetVersionSIDs, anyFunctionVersions, and dependencies.View the example1// 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 createBuild() {14const build = await client.serverless.v115.services("ZS00000000000000000000000000000000")16.builds.create({17assetVersions: ["ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],18functionVersions: ["ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"],19});2021console.log(build.sid);22}2324createBuild();Response
1{2"sid": "ZB00000000000000000000000000000000",3"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",4"service_sid": "ZS00000000000000000000000000000000",5"asset_versions": [6"ZNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"7],8"function_versions": [9"ZNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"10],11"dependencies": [12{13"name": "twilio",14"version": "3.29.2"15},16{17"name": "@twilio/runtime-handler",18"version": "1.0.1"19}20],21"runtime": "node24",22"status": "building",23"date_created": "2018-11-10T20:00:00Z",24"date_updated": "2018-11-10T20:00:00Z",25"url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000",26"links": {27"build_status": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Builds/ZB00000000000000000000000000000000/Status"28}29}
To learn more about Twilio Functions, see the API reference for all Serverless resources. Consider Variables. These let you define conditional values for a given Environment.