---
title: "JSON contracts"
description: "The versioned JSON output of the CLI, shared by the LSP, the UI and MCP."
canonical_url: "https://x18n.dev/references/json-contracts"
---
# JSON contracts

> The versioned JSON output of the CLI, shared by the LSP, the UI and MCP.

<!-- Generated by docs/scripts/generate.ts from the source code. Do not edit. -->

x18n has one versioned JSON contract. The CLI prints it with `--json`. The LSP `x18n/*` requests, the UI RPC functions and the MCP tools use the same shapes.

The current contract version is `2`. A breaking change to any shape increases it. A new optional field does not.

## Envelope

With `--json`, every command prints one JSON document to stdout:

```ts
export interface Envelope<T> {
  readonly x18n: number
  readonly command: string
  readonly data: T
  readonly diagnostics: readonly WireDiagnostic[]
  readonly errors: readonly ErrorInfo[]
}
```

- `x18n` is the contract version.
- `command` is the command name, for example `keys` or `prompt translate`.
- `data` is the result of the command. It is `null` when the command failed.
- `errors` holds the errors. In JSON mode, errors appear only here, so stdout is always valid JSON.

```ts
/** JSON form of an error: the `toJSON()` of a nostics diagnostic, without `stack`, `cause` and `data`. */
export interface ErrorInfo {
  /** The code, such as `X18N2002`, or the JS error name of an error without a code. */
  readonly name: string
  readonly why: string
  readonly fix?: string | undefined
  readonly docs?: string | undefined
}
```

`docs` is the URL of the [error page](https://x18n.dev/errors) of the code.

## Diagnostics

Checks report diagnostics. `x18n check --json` puts them in `diagnostics` of the envelope, and its `data` holds a summary and the coverage per project, `baseline` (`suppressed` and `fixed` counts) when a [baseline](https://x18n.dev/surfaces/cli#baseline) applies, and `fix` per project with [`--fix`](https://x18n.dev/surfaces/cli#fixes): `applied`, the `fixed` and `skipped` counts, the `files` written (relative to the project root) and the plan `warnings`. `x18n get --json` puts the diagnostics of one key in `data`. Both use this shape:

```ts
export interface WireDiagnostic {
  readonly project: string
  readonly code: string
  readonly severity: Severity
  readonly message: string
  readonly key: KeyString | undefined
  readonly locale: LocaleCode | undefined
  /** Path relative to the project root. */
  readonly file: string | undefined
  readonly range: WireRange | undefined
  readonly fixes: readonly Fix[]
}

export interface WireRange {
  /** 1-based. */
  readonly line: number
  /** 1-based, UTF-16 code units. */
  readonly column: number
  readonly endLine: number
  readonly endColumn: number
  /** 0-based UTF-16 offsets. */
  readonly start: number
  readonly end: number
}

export type Severity = 'error' | 'warning' | 'info' | 'hint'

export type FixLevel = 'safe' | 'unsafe'

export interface Fix {
  readonly title: string
  readonly ops: readonly EditOp[]
  readonly level?: FixLevel | undefined
}

/** A `set` with `replace: false` is skipped, with a warning, when it would replace other keys or a message on its path. */
export type EditOp
  = | {
    readonly type: 'set'
    readonly key: KeyString
    readonly locale: LocaleCode
    readonly value: MessageLeaf
    readonly replace?: boolean
    /** The value comes from a translator: recorded in the review file as machine-translated. `time` is filled in when planned. */
    readonly machine?: (MachineSource & { readonly time?: string | undefined }) | undefined
  }
  | { readonly type: 'rename', readonly from: KeyString, readonly to: KeyString, readonly usages?: boolean }
  | { readonly type: 'rename-prefix', readonly from: KeyString, readonly to: KeyString, readonly usages?: boolean }
  | { readonly type: 'delete', readonly key: KeyString, readonly locales?: 'all' | readonly LocaleCode[] }
  | { readonly type: 'extract', readonly items: readonly ExtractItem[] }
    /** Re-write resources with the configured sort and indent. */
  | { readonly type: 'format', readonly paths?: readonly string[] }
    /** Adds a comment to the review thread of a message. `id` and `time` are filled in when planned. */
  | {
    readonly type: 'review'
    readonly key: KeyString
    readonly locale: LocaleCode
    readonly review: ReviewType
    readonly text?: string | undefined
    /** The proposed value, required for `suggestion`. */
    readonly suggestion?: string | undefined
    readonly author?: ReviewAuthor | undefined
    readonly id?: string | undefined
    readonly time?: string | undefined
  }
  | { readonly type: 'resolve-review', readonly id: string }
    /** Sets the value of a suggestion and resolves it. */
  | { readonly type: 'apply-suggestion', readonly id: string }
    /** Records that translations are up to date with the current source value. Default: every translated locale. */
  | { readonly type: 'mark-fresh', readonly key: KeyString, readonly locales?: readonly LocaleCode[] | undefined }
```

`code` is the check id, for example `missing-key`. See [Checks](https://x18n.dev/checks).

## Input of x18n apply

`x18n apply` and the MCP tool `x18n_set_messages` read messages per locale:

```json
{
  "de": {
    "app.title": "Meine App",
    "nav.home": "Start"
  }
}
```

## Extraction plan

`x18n extract --plan <file>` writes this file, and `x18n extract --apply <file>` reads it back after you edit it:

```ts
export interface ExtractPlanFile {
  readonly x18n: 1
  readonly project: string
  readonly items: readonly ExtractCandidate[]
}

export interface ExtractCandidate {
  readonly path: string
  /** 1-based line, for display. */
  readonly line: number
  readonly kind: string
  readonly text: string
  /** The text in the file at `range`, used to detect stale plans. */
  readonly original: string
  readonly range: TextRange
  readonly attribute: string | undefined
  readonly key: KeyString
  /** True when an existing key with the same source value is reused. */
  readonly reused: boolean
  /** The message to store: template arguments become placeholders. */
  readonly value: string
  /** Code inserted as written. If you change `key`, also update the corresponding message reference here. */
  readonly replacement: string
  readonly imports: readonly string[]
  /** Other templates that also apply, for surfaces that let users choose. */
  readonly alternatives: readonly string[]
}
```

`key` names the resource message. `replacement` is the code to insert into the source file. If you change `key`, also update the corresponding message reference in `replacement`. Applying the plan does not regenerate replacement code from the key. See [Edit and apply a saved plan](https://x18n.dev/surfaces/cli#edit-and-apply-a-saved-plan).

## Exit codes

| Exit code | Meaning                                                                                                                                           | Error codes                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| --------- | ------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `0`       | Success. For `x18n check`: no errors, and no more warnings than `--max-warnings`.                                                                 |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| `1`       | `x18n check` found errors or too many warnings, `x18n apply` or `x18n translate` rejected values, or the command failed with one of these errors. | [X18N2001](https://x18n.dev/errors/x18n2001), [X18N2002](https://x18n.dev/errors/x18n2002), [X18N2003](https://x18n.dev/errors/x18n2003), [X18N2004](https://x18n.dev/errors/x18n2004), [X18N2005](https://x18n.dev/errors/x18n2005), [X18N3001](https://x18n.dev/errors/x18n3001), [X18N4001](https://x18n.dev/errors/x18n4001), [X18N4002](https://x18n.dev/errors/x18n4002), [X18N4005](https://x18n.dev/errors/x18n4005), [X18N4006](https://x18n.dev/errors/x18n4006), [X18N4007](https://x18n.dev/errors/x18n4007), [X18N4008](https://x18n.dev/errors/x18n4008), [X18N5001](https://x18n.dev/errors/x18n5001), [X18N5003](https://x18n.dev/errors/x18n5003), [X18N5004](https://x18n.dev/errors/x18n5004), [X18N5005](https://x18n.dev/errors/x18n5005), [X18N5006](https://x18n.dev/errors/x18n5006), [X18N6001](https://x18n.dev/errors/x18n6001), [X18N6002](https://x18n.dev/errors/x18n6002) |
| `2`       | Usage, config or plugin error. Errors without an x18n code also exit with 2.                                                                      | [X18N0001](https://x18n.dev/errors/x18n0001), [X18N0002](https://x18n.dev/errors/x18n0002), [X18N0003](https://x18n.dev/errors/x18n0003), [X18N1001](https://x18n.dev/errors/x18n1001), [X18N1002](https://x18n.dev/errors/x18n1002), [X18N1003](https://x18n.dev/errors/x18n1003), [X18N1004](https://x18n.dev/errors/x18n1004)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| `3`       | A decision is needed and no answer was given. Run again with `--yes`.                                                                             | [X18N4003](https://x18n.dev/errors/x18n4003)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| `4`       | Files changed while the command ran. Run it again.                                                                                                | [X18N4004](https://x18n.dev/errors/x18n4004)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |


## Sitemap

See the full [sitemap](https://x18n.dev/sitemap.md) for all pages.
