Twilio CLI

repository·main·Indexed 41 days ago

https://github.com/twilio/twilio-cli

A command-line interface for interacting with Twilio APIs, allowing developers to manage resources, automate tasks, and extend functionality via plugins. Version 6.2.4 supports Node.js 22+ and features a multi-command architecture for managing profiles, credentials, and API resources. It includes built-in support for output formats like JSON and TSV, localhost proxying for webhooks, and dynamic command generation from the Twilio Open API Specification.

Tokens
10K
Snippets
38
Records
59
Agent score
51%

What's inside Twilio CLI

  1. Handle command logs and errors

    main

    The Twilio CLI separates data from metadata:

    • stdout: Contains the actual command output (e.g., the list of phone numbers).
    • stderr: Contains all debug, informational, warning, and error messages. This allows you to pipe or process stdout without interference from logs.

    Log Levels You can set a log level to control the verbosity of messages sent to stderr. The default level is info.

  2. Manage multiple Twilio accounts using Profiles

    main

    The Twilio CLI uses profiles to store credentials for different accounts. The first profile created becomes the 'active' profile used for all subsequent commands.

    • Create a profile: Run twilio login (alias for twilio profiles:create).
    • Switch active profile: Use twilio profiles:use <profile_id>.
    • List profiles: Use twilio profiles:list to see all local profiles and identify the active one.
    • Use a specific profile for a command: Use the -p flag (e.g., twilio phone-numbers:list -p my_other_profile).
    • Specify Region: Use the --region flag during login to target a specific Twilio Region (e.g., twilio login --region au1).
    twilio login
    twilio profiles:use my_other_profile
    twilio phone-numbers:list -p my_other_profile
  3. Understand the Twilio CLI architecture and components

    main

    The Twilio CLI is built on the oclif (v2) framework for Node.js (v14+). Its architecture is divided into several key layers:

    Command Hierarchy

    • BaseCommand: The root class providing shared functionality like logging, output formatting, and configuration.
    • TwilioClientCommand: Inherits from BaseCommand and adds support for Twilio "profiles" and credential management.
    • Sendgrid Commands: Inherit directly from BaseCommand.

    Services

    Internal concerns are handled by services (located in src/services), which can be injected into command constructors for testing or customization. Key services include:

    • Config: Manages profiles and credentials.
    • Logger: Standardizes output (debug, info, warning, error) to stderr.
    • SecureStorage: Uses keytar to abstract platform-level encryption for credentials.
    • Output Formats: Handles writing data to stdout in columns, JSON, or TSV formats.

    API Integration

    The CLI uses the Twilio Open API Specification (Open API v3) to dynamically generate the majority of its commands. This ensures full coverage of Twilio APIs and allows for automatic mapping of resources to topics and actions to commands.

  4. How Twilio CLI commands and topics are structured

    main

    The Twilio CLI uses a "multi-command" architecture similar to git. Commands are organized into topics to represent API resources, while the commands themselves represent actions performed on those resources.

    • Single commands: e.g., twilio help.
    • Topic + Command: e.g., twilio profile:add or twilio incoming-phone-number:list.
    • Nesting: Topics and commands can be separated by a colon (:) or a space. Deeply nested resources follow the pattern twilio <topic>:<sub-topic>:<action>.

    Example Mapping: If the resource is api:messaging:v1:services, the available actions (commands) would be create, fetch, list, update, and delete. The full command would look like: twilio api:messaging:v1:services:create.

    # Example of a nested command structure
    twilio api:messaging:v1:services:create
  5. Understand the twilio-cli versioning strategy

    main

    The twilio-cli follows a modified Semantic Versioning (MAJOR.MINOR.PATCH) pattern. To ensure stability in your automation or development environments, it is strongly recommended to pin at least the major version (and ideally the minor version) to prevent unexpected breaking changes.

    • PATCH (MAJOR.MINOR.PATCH): Incremented for backwards-compatible bug fixes. These are generally safe to upgrade.
    • MINOR (MAJOR.MINOR.PATCH): Incremented for new features or small backwards-incompatible changes (e.g., changes to specific commands). Upgrading may require manual code adjustments.
    • MAJOR (MAJOR.MINOR.PATCH): Incremented for significant breaking changes or new technology families. These require extensive reworking of code and are communicated in advance via Release Candidates.
  6. Identify supported versions of twilio-cli

    main
    Twilio only provides active support for the current MAJOR version of the twilio-cli. New features, bug fixes, and security updates are exclusively applied to the most recent major release. If you are using an older major version, you will not receive updates.
  7. CLI Limitations: Timeouts and Result Limits

    main

    Be aware of the following default constraints:

    • API Timeout: The default timeout for Twilio API requests is 30 seconds.
    • List Limits: By default, list commands only display the first 50 records. Use the --limit flag to increase this number. You can also use available filters (e.g., date, to, from) to narrow down results.
  8. Command Base Classes

    main

    When adding commands to src/commands, you must inherit from one of the following base classes to ensure proper integration with the CLI:

    TwilioClientCommand

    Use this if your command needs to make Twilio API calls.

    • this.twilioClient: An instance of the Twilio Node.js helper library, pre-configured with necessary credentials and account SID.
    • this.httpClient: Provided for making requests to non-Twilio APIs (you must manage credentials for these manually).

    TwilioBaseCommand

    Use this if your command does not need to make any Twilio API calls.

  9. Install and Setup the Twilio CLI

    main

    To use the Twilio CLI, ensure you meet the runtime requirements and follow the official setup documentation.

    Requirements

    • Node.js: Node 22+ is currently supported. The CLI supports LTS versions of Node.
    • Note on Node 20: As of version 7.0.0, Node 20 is no longer supported. If you are on Node 20, you must upgrade to Node 22 or later.

    Setup

    For detailed installation instructions, refer to the Twilio CLI documentation.

  10. Use command piping and parameters

    main

    The Twilio CLI follows traditional Unix-style command-line parameter conventions:

    • Shorthand: Single dash and single letter (e.g., -f).
    • Kebab-case: Two dashes and kebab-case names (e.g., --phone-number).

    The CLI supports piping, allowing you to chain commands together. For example, you can find a phone number and then pipe that result into a command to purchase it.

  11. Publish and Install Twilio CLI Plugins

    main

    Once your plugin is ready, you can distribute it in two ways:

    Via NPM (Public)

    1. Publish your package to npmjs.org using npm publish.
    2. Users can install it using:
      twilio plugins:install <npm-package-name>
    
    ### Via Tarball (Private)
    If you want to keep the package private:
    1. Run `npm pack` to create a `.tgz` tarball.
    2. Host the tarball (e.g., using Twilio Assets).
    3. Users can install it using the URL:
       ```bash
    twilio plugins:install <url_to_package.tar.gz>