Introduction to tg CLI (JavaScript)
The tg CLI is a fully featured CLI tool utilising the Twingate GraphQL APIs.
It is open-source and written in JavaScript so Node or Deno developers can extend or customise for their own use cases. Pre-built binaries are provided for all major platforms. Users familiar with Python development may prefer the Python CLI.
Open Source contribution
The tg CLI tool is an Open Source project developed and maintained outside of our product engineering teams. For support regarding this tool please visit the Github issues page.
Getting Started
Download binaries for Windows, Mac or Linux from our release page on Github.
Unzip the package and insert the following in your terminal:
# ./tg --help
Usage: tg Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
CLI for Twingate
Options:
-h, --help - Show this help. -V, --version - Show the version number for this program. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO", Values: "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "SEVERE", "FATAL", "QUIET", "SILENT")
Commands:
export - Export from account to various formats import - Import from excel file to a Twingate account resource - Twingate resources group - Twingate groups user - Twingate users network - Twingate networks connector - Twingate connectors device - Twingate devices service - Twingate services
The tg CLI tool asks for Twingate Account and API Key whenever a command is called and the option to save the Twingate Account and API key is also given. See the example below.
# ./tg export ? Enter Twingate account: › exampleAccount ? Enter API key: › ************************************************************************************************************************************** ? Save account and API key to file? › Yes[INFO] Configuration file saved.[SUCCESS] Export to 'exampleAccount-2022-06-08_16-57-01.xlsx' completed.
User Command
The user command provides the ability to list all users.
# ./tg user --help
Usage: tg user Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
Twingate users
Options:
-h, --help - Show this help. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO")
Commands:
list - Get list of users
List All Users
# ./tg user list[INFO] Using Twingate account: 'exampleAccount'┌──────────────────┬───────────┬───────────┬───────────┬──────────┬───────────────────────┬────────────────────────────────────────────────────────────────── ──────────────────────┬─────────┬─────────┬─────────────────┐│ id │ createdAt │ updatedAt │ firstName │ lastName │ email │ avatarUrl │ isAdmin │ state │ groups │├──────────────────┼───────────┼───────────┼───────────┼──────────┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────────────┼─────────┼─────────┼─────────────────┤│ VXNlcjoxMzY3Ng== │ 6/7/22 │ 6/9/22 │ │ │ xxxx.xxxxxx@gmail.com │ │ FALSE │ Pending │ Everyone, test1 │├──────────────────┼───────────┼───────────┼───────────┼──────────┼───────────────────────┼────────────────────────────────────────────────────────────────────────────────────────┼─────────┼─────────┼─────────────────┤│ VXNlcjoxMzYxMA== │ 5/23/22 │ 6/10/22 │ aaaa │ bbbbbb │ xxxx@twingate.com │ │ TRUE │ Active │ Everyone, test1 │└──────────────────┴───────────┴───────────┴───────────┴──────────┴───────────────────────┴────────────────────────────────────────────────────────────────────────────────────────┴─────────┴─────────┴─────────────────┘
Group Command
The group command provides the ability to list, create, remove and copy groups as well as add users and resources to a group.
# ./tg group --help
Usage: tg group Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
Twingate groups
Options:
-h, --help - Show this help. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO")
Commands:
list - Get list of groups create <name> [UserIds...] - Create a group remove <id> - Remove a group remove_bulk [groupIds...] - Remove groups bulk add_user <groupNameOrId> [userIds...] - Add users to a group remove_user <groupNameOrId> [userIds...] - Remove users to a group remove_resource <groupNameOrId> [resourceNamesOrIds...] - Add resources to a group add_resource <groupNameOrId> [resourceNamesOrIds...] - Add resources to a group set_policy <groupNameOrId> <securityPolicyNameOrId> - Set group security policy copy <source> <destination> - Copy a group
List All Groups
# ./tg group list[INFO] Using Twingate account: 'exampleAccount'┌──────────────────┬───────────┬───────────┬──────────────┬──────────┬────────┐│ id │ createdAt │ updatedAt │ name │ isActive │ type │├──────────────────┼───────────┼───────────┼──────────────┼──────────┼────────┤│ R3JvdXA6MzE1OTY= │ 5/4/22 │ 5/4/22 │ Everyone │ TRUE │ System │├──────────────────┼───────────┼───────────┼──────────────┼──────────┼────────┤│ R3JvdXA6MzQ2NTE= │ 6/8/22 │ 6/8/22 │ exampleGroup │ TRUE │ Manual │├──────────────────┼───────────┼───────────┼──────────────┼──────────┼────────┤│ R3JvdXA6MzI4ODM= │ 5/17/22 │ 5/17/22 │ test_group │ TRUE │ Manual │└──────────────────┴───────────┴───────────┴──────────────┴──────────┴────────┘
Create A New Group
# ./tg group create "exampleGroup"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] New group named 'exampleGroup' created with id 'R3JvdXA6MzQ2NTM='.
Create A New Group With Added Users
The users must already exist in Twingate. User ID must be used instead email address.
# ./tg group create "exampleGroup2" "VXNlcjoxMzY3Ng==" "VXNlcjoxMzYxMA=="[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] New group named 'exampleGroup2' created with id 'R3JvdXA6MjI4MDc=' with added users 'xxxx@twingate.com: VXNlcjoxMzY3Ng==' 'xxxx@gmail.com: VXNlcjoxMzYxMA=='
Remove A Group
#. /tg group remove "R3JvdXA6MzQ2NTM="[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Removed group with id 'R3JvdXA6MzQ2NTM='
Add Users To A Group
The users must already exist in Twingate. User ID must be used instead email address.
# ./tg group add_user "exampleGroup" "VXNlcjoxMzY3Ng==" "VXNlcjoxMzYxMA=="[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Added users 'xxxx@gmail.com: VXNlcjoxMzY3Ng==' 'xxxx@twingate.com: VXNlcjoxMzYxMA==' to group 'exampleGroup: R3JvdXA6MzQ2NTM='
Remove Users From A Group
# ./tg group remove_user exampleGroup "VXNlcjoxMzY3Ng==" "VXNlcjoxMzYxMA=="[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Removed users 'xxxx@gmail.com: VXNlcjoxMzY3Ng==' 'xxxx@twingate.com: VXNlcjoxMzYxMA==' from group 'exampleGroup: R3JvdXA6MzQ2NTM='
Add Groups To A Resource
The resource must already exist in Twingate.
# ./tg group add_resource "exampleGroup" "exampleResource1" "exampleResource2"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Added resources 'exampleResource1: UmVzb3VyY2U6MzU5NDg2' 'exampleResource2: UmVzb3VyY2U6MzU5NDg3' to group 'exampleGroup: R3JvdXA6MjI3NTI='
Remove Groups From A Resource
# ./tg group remove_resource "exampleGroup" "exampleResource1" "exampleResource2"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Removed resources 'exampleResource1: UmVzb3VyY2U6MzU5NDg2' 'exampleResource2: UmVzb3VyY2U6MzU5NDg3' from group 'exampleGroup: R3JvdXA6MjI3NTI='
Set Group Policy
# ./tg group set_policy exampleGroup examplePolicy[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] Set group 'exampleGroup: R3JvdXA6MjI3NTI=' security policy to 'examplePolicy: U2VjdXJpdHlQb2xpY3k6NjIwMQ=='
Copy A Group
All users of the source group is copied to the destination group.
# ./tg group copy "exampleGroup2" "exampleGroup5"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] New group named 'exampleGroup9' created as a copy of 'exampleGroup2'
Network Command
The network command provides the ability to list and create remote networks.
# ./tg network --help
Usage: tg network Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
Twingate networks
Options:
-h, --help - Show this help. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO")
Commands:
list - Get list of networks create <name> - Create a network
List All Remote Networks
# ./tg network list[INFO] Using Twingate account: 'exampleAccount'┌──────────────────────────┬──────┬──────────┬───────────┬───────────┬───────────────────────────────┐│ id │ name │ isActive │ createdAt │ updatedAt │ connectors │├──────────────────────────┼──────┼──────────┼───────────┼───────────┼───────────────────────────────┤│ UmVtb3RlTmV0d29yazo4MDEw │ 123 │ TRUE │ 6/6/22 │ 6/6/22 │ tourmaline-crow, maroon-rhino │├──────────────────────────┼──────┼──────────┼───────────┼───────────┼───────────────────────────────┤│ UmVtb3RlTmV0d29yazo4MDEy │ 234 │ TRUE │ 6/9/22 │ 6/9/22 │ │└──────────────────────────┴──────┴──────────┴───────────┴───────────┴───────────────────────────────┘
Create A New Remote Network
# ./tg network create "myNewRemoteNetwork"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] New network named 'myNewRemoteNetwork' created with id 'UmVtb3RlTmV0d29yazo4MDEz'
Connector Command
The connector command provides the ability to list and create connectors.
# ./tg connector --help
Usage: tg connector Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
Twingate connectors
Options:
-h, --help - Show this help. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO")
Commands:
list - Get list of connectors create <remoteNetworkNameOrId> [name] - Create a connector
List All Connectors
# ./tg connector list[INFO] Using Twingate account: 'exampleAccount'┌──────────────────────┬─────────────────────┬───────────┬───────────┬──────────────── ─┬────────────────────────┬────────────────────┐│ id │ name │ createdAt │ updatedAt │ lastHeartbeatAt │ state │ remoteNetworkLabel │├──────────────────────┼─────────────────────┼───────────┼───────────┼─────────────────┼────────────────────────┼────────────────────┤│ Q29ubmVjdG9yOjUxOQ== │ lavender-pheasant │ 6/13/22 │ 6/13/22 │ │ Offline - No Heartbeat │ 456 │├──────────────────────┼─────────────────────┼───────────┼───────────┼─────────────────┼────────────────────────┼────────────────────┤│ Q29ubmVjdG9yOjUxNQ== │ maroon-rhino │ 6/6/22 │ 6/6/22 │ │ Offline - No Heartbeat │ 123 │├──────────────────────┼── ───────────────────┼───────────┼───────────┼─────────────────┼────────────────────────┼────────────────────┤│ Q29ubmVjdG9yOjUyMQ== │ mysterious-barnacle │ 6/13/22 │ 6/13/22 │ │ Offline - No Heartbeat │ 234 │├──────────────────────┼─────────────────────┼───────────┼───────────┼─────────────────┼────────────────────────┼────────────────────┤│ Q29ubmVjdG9yOjUyMA== │ smooth-auk │ 6/13/22 │ 6/13/22 │ │ Offline - No Heartbeat │ 456 │├──────────────────────┼─────────────────────┼───────────┼───────────┼─────────────────┼────────────────────────┼────────────────────┤│ Q29ubmVjdG9yOjUxNA== │ tourmaline-crow │ 6/6/22 │ 6/6/22 │ │ Offline - No Heartbeat │ 123 │└──────────────────────┴─────────────────────┴───────────┴───────────┴─────────────────┴────────────────────────┴────────────────────┘
Create A New Connector
The remote network must already exist in Twingate.
# ./tg connector create "myRemoteNetwork" "myNewConnector"[INFO] Using Twingate account: 'exampleAccount'[SUCCESS] New Connector named 'myNewConnector' created with id 'UmVtb3RlTmV0d29yazo4MDEz' in network 'myRemoteNetwork' with tokens:ACCESS_TOKEN=xxxxxxxxxxREFRESH_TOKEN=xxxxxxxxxx
Resource Command
The resource command provides the ability to list, create, remove resources as well as add groups to a resource.
# ./tg resource --help
Usage: tg resource Version: CLI Version: LATEST | TwingateApiClient Version: 0.1.0
Description:
Twingate resources
Options:
-h, --help - Show this help. -a, --account-name <string> - Twingate account name -l, --log-level [logLevel] - Log level (Default: "INFO")
Commands:
list - Get list of resources create <remoteNetworkNameOrId> <name> <address> - Create a resource [groupNamesOrIds...] remove <id> - Remove a resource remove_bulk [resourceIds...] - Remove resources bulk add_group <resourceNameOrId> [groupNamesOrIds...] - Add groups to a resource
List All Resources
# ./tg resource list[INFO] Using Twingate account: 'exampleAccount'┌──────────────────────┬────────────────────────┬───────────┬───────────┬──────────┬────────────────────┬─────────────┬───────────────────────┬────────────────────┬────────────────────┬───────────────────┬────────────────────┬───────────────────┐│ id │ name │ createdAt │ updatedAt │ isActive │ remoteNetworkLabel │ addressType │ addressValue │ protocolsAllowIcmp │ protocolsTcpPolicy │ protocolsTcpPorts │ protocolsUdpPolicy │ protocolsUdpPorts │├──────────────────────┼────────────────────────┼───────────┼───────────┼──────────┼────────────────────┼─────────────┼ ───────────────────────┼────────────────────┼────────────────────┼───────────────────┼────────────────────┼───────────────────┤│ UmVzb3VyY2U6MzU5NDM3 │ r8888 │ 6/10/22 │ 6/10/22 │ TRUE │ 123 │ DNS │ 2.2.2.2 │ TRUE │ ALLOW_ALL │ │ ALLOW_ALL │ │├──────────────────────┼────────────────────────┼───────────┼───────────┼──────────┼────────────────────┼─────────────┼───────────────────────┼────────────────────┼────────────────────┼───────────────────┼────────────────────┼───────────────────┤│ UmVzb3VyY2U6MzU5NDQ3 │ tg test │ 6/13/22 │ 6/13/22 │ TRUE │ 123 │ IP │ 1.1.1.1 │ TRUE │ ALLOW_ALL │ │ ALLOW_ALL │ │