Objective
Learn how to debug Twilio Functions using live logs in the console, retrieve historical logs via the Twilio CLI, and set up local real-time debugging. This guide helps resolve execution errors and unexpected behavior in your Serverless environment.
Product
Twilio Functions
Environment
Twilio Console
Procedure
Method 1: Debug from the Twilio Console
- Open your Function in the Twilio Console editor.
- Toggle on the Live logs option in the editor user interface.
- Invoke your Function (for example, by sending an HTTP request via browser or curl).
- Watch the log statements from your code display in real time as the Function runs.
Method 2: Retrieve Historical Logs via Twilio CLI
If you have closed your console session, you can still fetch your logs through the Serverless API using the Twilio CLI.
- Option A: Using the standard Serverless API command Run the following command in your terminal:
twilio api:serverless:v1:services:environments:logs:list \
--service-sid ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
--environment-sid ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
- Option B: Using the Serverless Toolkit plugin (Recommended) For a simplified, less verbose command, run:
twilio serverless:logs \
--service-sid=ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
--environment=ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXMethod 3: Debug Locally in Real-Time
To step through your code line-by-line using a developer tool like Visual Studio Code or Chrome Developer Tools, you must run the Function locally.
- Open your project locally (Note: This method only works if your project was initially created using the Serverless Toolkit, not the Console UI).
- Start your local development server with a Node.js debugger attached using the Serverless Toolkit.
- Set your breakpoints and execute the local Function to begin debugging.