# Scripting


## JSON output and exit codes

```sh
line whoami --json
line contacts --json
line contacts --mid USER_MID --json
line chats --search "Family" --json
line messages CHAT_ID --json
line send CHAT_ID --stdin --json < message.txt
```

- Use full chat IDs, and keep message IDs as strings.
- `--json`, `--error-format=json`, and `--stdin` turn prompts off, so pass every
  required argument.
- JSON and help go to stdout; diagnostics go to stderr.
- Empty result lists are `[]`.

| Exit code | Meaning |
| --- | --- |
| `0` | Success, or help was shown. |
| `1` | A CLI or network error. |
| `65`–`78` | A storage error; see [storage exit codes](https://kongesque.github.io/line-cli/guides/linux-servers/#storage-exit-codes). |
| `130`, `143` | Stopped by SIGINT or SIGTERM. |

| Command | Useful JSON fields |
| --- | --- |
| `whoami` | Profile and account ID. |
| `contacts` | Friend names and IDs. |
| `contacts --mid MID` | One record per unique requested MID, with a lookup status. |
| `contacts --userid HANDLE`, `contacts --phone +CCNUMBER` | One resolved contact lookup object (exits 1 with `not_found` when unmatched). |
| `contacts --blocked` | Array of blocked contacts and blocked recommendations. |
| `contacts block`, `contacts unblock` | Action (`block_contact` or `unblock_contact`), `chat_id`, and `request_sequence`. |
| `chats` | ID, type, unread count, and optional activity time. |
| `messages` | ID, sender, time, content, encryption, and status. |
| `send` | Message ID, chat ID, encryption, group-key registration, and sequence. |
| `download` | Output path, byte count, and message ID. |
| `read`, `react`, `unsend` | Action, chat ID, message ID, and sequence. |
| `invites` | `schema_version: 1` and `invitations` (`chat_id`, nullable `name`, `name_status`). |
| `invites accept`, `invites reject` | Action (`accept_invitation` or `reject_invitation`), chat ID, and sequence. |
| `watch` | One JSON event per line. |
| `update` | Installed/latest versions, status, installation method, and next step. Use `--check --json` to only check. |
| `doctor` | `schema_version: 1`, `checks` (`id`, `status`, `message`, optional `next_step` and string `details`), and a `summary` count per status. |

If some messages can't be decrypted, `messages --json` still writes the full
array with a status for each message, then exits 1. It never prints encrypted
chunks or raw server responses.

## Structured errors

Put `--error-format=json` before the command to get one structured error on
stderr, while success and partial results still go to stdout as JSON:

```sh
line --error-format=json send CHAT_ID --stdin --json < message.txt
```

It can go before or after `--read-only`, and it turns prompts off. It works with
the core commands and `auth status`. Login, logout, migration, watch, and update
reject it before doing anything; use their normal diagnostics instead.

Each error has a stable code and stage. Message actions also report the chat and
message IDs they know, the request sequence, whether the action was attempted,
its outcome, and any preparation or upload steps. A file message can be
confirmed even if its upload fails, and a send stays confirmed if writing the
result fails.

Never retry an `unknown` action automatically, even after a network error: check
LINE first. Storage and signal exit codes don't change. See the
[error fields, codes, and examples](https://kongesque.github.io/line-cli/reference/json/#errors).

## Read-only mode

Put `--read-only` before the command, or set `LINE_CLI_READ_ONLY=1`, when a
script or agent should only read:

```sh
line --read-only messages CHAT_ID --json
LINE_CLI_READ_ONLY=1 line chats --json
line --read-only --error-format=json send CHAT_ID --text "hi"   # refused
```

**Refused:** `send`, `read`, `react`, `unsend`, `invites accept`,
`invites reject`, `contacts block`, `contacts unblock`, `login`, `logout`,
`auth migrate`, and installing with `update`. They stop before prompts, stdin,
attachments, the session lock, credential storage, the network, or a request
sequence.

**Still allowed:** reads (including `contacts --blocked`, `--userid`, and
`--phone`), `watch` (its resume position is local), `download`, `auth status`,
`doctor`, `update --check`, `help`, and `version`. Reads may still refresh the
access token.

`LINE_CLI_READ_ONLY` accepts `1` or `true` (on) and `0`, `false`, or unset
(off). Any other value fails every command with `invalid_arguments`, so a typo
never quietly turns the guard off. `--read-only=false` can't override the
environment variable. A refused command exits 1 with the JSON error code
`read_only` (stage `preparation`); message actions report `attempted: false` and
`outcome: "not_attempted"`. Command help still works in read-only mode.


## Contact lookup JSON

`contacts --mid MID --json` returns an array, even for one ID. Each record has
the usual contact fields `mid`, `displayName`, `displayNameOverridden`,
`statusMessage`, and `picturePath`, plus:

| Field | Meaning |
| --- | --- |
| `effectiveDisplayName` | Your custom name, or the profile name if you haven't set one. Empty when no name is available. |
| `status` | `resolved` if LINE returned a profile; `unavailable` if it left that MID out. |

A missing profile still gets a row with its requested `mid` and empty name and
profile fields. A returned profile can also have an empty name, so fall back to
the MID when displaying it. Match rows by `mid`, because duplicate input IDs
appear only once.

If LINE returns every profile, the command exits 0. If any are unavailable, it
writes the complete array, reports the count on stderr, and exits 1; the array
is still usable as a partial result. A network, sign-in, or malformed-response
error produces no array, even if an earlier batch succeeded.


---
Source: https://kongesque.github.io/line-cli/guides/scripts/
