Upload assets using the Assets Plugin
The Assets Plugin is a Twilio CLI plugin that is part of the Serverless Toolkit. It allows you to create an Assets service that you can use as a bucket for static assets, like images or audio files, that you want to upload. You can then use these assets in your Twilio applications. For example, you can upload audio files that you then use in a
<Play> verb during a Twilio Voice call, or upload images that you can send to your users via MMS or WhatsApp media messages.
How to use the Assets Plugin
Install the Assets Plugin
Before we can get started using the Assets Plugin we need to install it. As the Assets Plugin is a Twilio CLI plugin, you will need to install the Twilio CLI first. If you have already installed the Twilio CLI, carry on, otherwise follow the instructions for installing the Twilio CLI.
Once the Twilio CLI is installed you can install the Assets Plugin with the following command:
twilio plugins:install @twilio-labs/plugin-assets
Initialize the plugin
The Assets Plugin has three commands:
list. The first thing you should do is run the
init command. Running
init will call the Twilio Functions and Assets API to create a new service and store the details about that service so that we can use it with the other commands.
The service will be given a random name which will feature in the domain name for the service. If you want to provide your own name, you can pass the
--service-name flag to the command.
twilio assets:init --service-name my-cool-assets-service
Upload an asset
With the plugin initialized you can now use it to upload static assets with the
upload command. Pass the path of the file you want to upload to the command and it will be deployed to your service.
twilio assets:upload path/to/file
If you only want an asset to be accessible by a Twilio request, you can upload it as a protected asset when you pass the
twilio assets:upload path/to/file --protected
List your assets
The last command allows you to list the assets you have uploaded to the service. Run it with:
Your assets, their SIDs, paths, URLs and visibility will be listed. If you want more, or fewer, properties you can select them by passing the
--properties flag to the command:
twilio assets:list --properties sid,url,date_created
Or you can output all the properties as JSON:
twilio assets:list -o json
Let's work together
The Assets Plugin is open source as part of the Serverless Toolkit. If you find any problems with this, please file an issue or even create a pull request to work together with us on the toolkit. We would love to hear your ideas and feedback!
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 by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.