> ## Documentation Index
> Fetch the complete documentation index at: https://docs.formal.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# CLI Reference

> Authenticate, list resources, and connect with the Formal CLI

See [Configuration](https://docs.formal.ai/docs/guides/client-apps/configuration.md) for `~/.formal/config.toml`.

### Authentication

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Log in
formal auth login

# Show the logged-in user
formal auth whoami

# Log out
formal auth logout
```

### List Resources

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal ls
```

Interactive TUI with:

* `↑/k` - Move up
* `↓/j` - Move down
* `enter` - Connect to resource
* `/` - Filter resources
* `q` - Quit
* `?` - Show help

### Connect to Resources

#### Databases (PostgreSQL, MySQL, MongoDB)

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Connect to a database
formal connect production-postgres

# Output:
# Connected to production-postgres on localhost:6432
```

Then connect with your database client:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# PostgreSQL
psql -h localhost -p 6432 -d postgres

# MySQL
mysql -h localhost -P 4306 -D mysql

# MongoDB
mongosh "mongodb://localhost:37017/mydb"
```

**Formal automatically injects your credentials** - no username/password needed!

Note that the database name is required for the connection to be successful.

You can also use the `--launch` argument to launch the appropriate CLI program after connecting (for example `psql` or `ssh`), e.g.

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# uses the database name from the config file in ~/.formal/config.toml
formal connect --launch production-postgres
```

#### Specify Native User

To request a Native User instead of the Resource's assigned default, append its label using `formal@<native_user_label>`.

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# PostgreSQL with the "readonly" Native User label
psql -h localhost -p 6432 -U formal@readonly
```

See [Native Users](https://docs.formal.ai/docs/guides/core-concepts/resources/native-users.md) for label selection and policy controls.

#### Specify Resource Hostname

If a resource has multiple [Resource Hostnames](https://docs.formal.ai/docs/guides/core-concepts/resources/tls.md#resource-hostnames) (for example, separate reader and writer endpoints), target one by appending `@<hostname-name>` to the resource name:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Connect to the "postgres-reader" hostname on the production-postgres resource
formal connect production-postgres@postgres-reader
```

> [!NOTE]
>
> Resource hostname targeting is currently supported for PostgreSQL and MySQL resources.

Connect with your database client as usual, using the port from the `formal connect` output:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# PostgreSQL
psql -h localhost -p 6432 -d postgres

# MySQL
mysql -h localhost -P 4306 -D mysql
```

Use the same `RESOURCE_NAME@HOSTNAME_NAME` target to disconnect:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal disconnect production-postgres@postgres-reader
```

#### SSH

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# List resources
formal ls
# Select an SSH resource and press enter
```

This updates your `~/.ssh/config` automatically.

#### Configure SSH without write access

Formal adds the following block to the top of `~/.ssh/config` on your first SSH connection:

```ssh-config theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# START FORMAL-MANAGED BLOCK
# Do not hand-edit this section.
Include ~/.formal/ssh_config
# END FORMAL-MANAGED BLOCK
```

If Formal cannot modify this file, add the block manually before any `Host` entries. Then run `formal connect <resource-name>` again.

Formal writes subsequent SSH resource changes to `~/.formal/ssh_config`.

#### Kubernetes

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Configure kubectl
formal ls
# Select a Kubernetes resource and press enter
```

This updates your `~/.kube/config` to route through the Connector.

#### HTTP

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal connect elasticsearch
# Connected to elasticsearch on localhost:9000

curl http://localhost:9000/_cluster/health
```

The local listener serves plain HTTP and adds Formal credentials to each request before forwarding it to the Connector over TLS. Any HTTP client works as long as its base URL can point to the local listener, with no Transparent Mode or `X-Formal-User-*` headers required.

#### Snowflake

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal connect snowflake
# Connected to snowflake on localhost:9000
```

Point your Snowflake driver at the local listener over plain HTTP, for example `host="localhost"`, `port=9000` and `protocol="http"` with the Python connector. The Endpoint attaches your device information to each request before forwarding it to the Connector over TLS. The driver logs in with the same credentials it would use against the Connector directly.

### Disconnect

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
# Disconnect from a resource
formal disconnect production-postgres
```

### Run a Command Under a Scope

Use `formal run` to give an application its own scope, separate from your other tools:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal run --as my-app -- npm start
```

`formal run` registers itself with the Formal Desktop app, then replaces itself with the command, which keeps its process. Traffic from the command and its child processes carries the `my-app` scope. [Network rules](https://docs.formal.ai/docs/guides/network-rules.md) read it as `scope`, for example to forward the application's traffic to its own resource:

```cel theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
{
  "condition": {
    "pre_tcp": scope == "my-app",
    "pre_tls": scope == "my-app",
    "post_tls": technology == "llm"
  },
  "outputs": {
    "forward_to_connector": true,
    "resource_name": "my_app_llm"
  }
}
```

Policies and Native User hooks read it as `input.device.scope`, for example to inject the application's API key instead of yours:

```ts theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
export default async function credentials(input, env) {
  const token = input.device?.scope === "my-app" ? env.MY_APP_TOKEN : env.EMPLOYEE_TOKEN;
  return { header: "Authorization", token };
}
```

> [!WARNING]
>
> Any process you run can register its own scope, so `input.device.scope` does not prove which application sent a request. Use it to pick between credentials the caller is already allowed to use. For a privileged credential, also check the caller's identity, such as `input.user.groups`.

The Formal Desktop app must be running when the command starts. The scope survives a restart of the app. Scopes work on macOS and Linux. A child process loses the scope once the command is no longer its ancestor, for example when it detaches to run in the background, or when another app starts it, as `open` does.

### Decrypt Data

Decrypt Formal-encrypted data:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal decrypt --ciphertext "formalencrypt:second:third:fourth:fifth"
```

### S3 Operations

The Formal `s3` command automatically routes traffic to the right Connector so your users don't have to worry about formatting the right parameters for S3.

Interact with S3 through Formal:

```bash theme={"languages":{"custom":["/languages/cel.json","/languages/rego.json"]}}
formal s3 ls s3://my-bucket
```

See [AWS CLI S3 commands](https://docs.aws.amazon.com/cli/latest/userguide/cli-services-s3-commands.html) for more operations.


## Related topics

- [Install Formal Endpoint on Windows](https://docs.formal.ai/docs/guides/client-apps/windows.md)
- [Formal Endpoint](https://docs.formal.ai/docs/guides/client-apps/desktop-app.md)
- [Connect Formal MCP](https://docs.formal.ai/docs/api/mcp-server.md)
- [ListOAuthClients](https://docs.formal.ai/api-reference/corev1oauthservice/listoauthclients.md)
- [GetOAuthClient](https://docs.formal.ai/api-reference/corev1oauthservice/getoauthclient.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.