# Introduction to the Twingate Javascript CLI

The Twingate Javascript 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](/content/docs/introduction-to-the-python-cli/index.html).

### 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](https://github.com/Twingate-Labs/tg-cli/issues).

## Getting Started

Download binaries for Windows, Mac or Linux from our release page on [GitHub](https://github.com/Twingate-Labs/tg-cli/releases).

Unzip the package and insert the following in your terminal:

```text
# ./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.

```text
# ./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.

```text
# ./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**

```text
# ./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.

```text
# ./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**

```text
# ./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 │
└──────────────────┴───────────┴───────────┴──────────────┴──────────┴────────┘
```

... (omitted for brevity) ...

**Add Resources To A Service Account**

The resources must already exist in Twingate.

```text
# ./tg service add_resource "U2VydmljZUFjY291bnQ6MzYyNDNiMDQtZGE1ZC00Y2IyLThjOGYtYmExYzE2NWQxZTFj" "myResource1" "myResource2"
[INFO]    Using Twingate account: 'exampleAccount'
[SUCCESS] Added resources 'myResource1: UmVzb3VyY2U6MzU5NDg2' 'myResource2: UmVzb3VyY2U6MzU5NDg3' to service 'myNewServiceAccount: U2VydmljZUFjY291bnQ6Zjc3MDI4OTktMmJlNC00MTdlLWE5ZDctZGYyMmJlZDJiZmIx'
```
