> For the complete documentation index, see [llms.txt](https://docs.wproofreader.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.wproofreader.com/v6.12.0/api-reference/detect-language-api.md).

# Detect language API

`detect_language` takes a text and returns a list of the most likely languages.

It is built on top of the open-source [Compact Language Detector 2 (CLD2)](https://github.com/CLD2Owners/cld2) library.

**Command name:** `detect_language`\
**Success status:** `200 OK`

### On this page

* [Common parameters](#common-parameters)
* [Parameters](#parameters)
* [Behavior notes](#behavior-notes)
* [Response schema](#response-schema)
* [Request examples](#request-examples)
* [Errors](#errors)

**Related**: [Overview](/v6.12.0/api-reference/overview.md), [Supported languages](/v6.12.0/features/supported-languages.md), [Why WProofreader auto detect language doesn't work for small piece of texts?](/v6.12.0/faq/technical/languages/why-wproofreader-auto-detect-language-doesnt-work-for-small-piece-of-texts.md)

### Common parameters

`cmd` selects the command to run.

| Name  | Type   | Required | Description                |
| ----- | ------ | -------- | -------------------------- |
| `cmd` | string | Yes      | Must be `detect_language`. |

These parameters work with any `cmd`.

| Name        | Type   | Required   | Description                                                       |
| ----------- | ------ | ---------- | ----------------------------------------------------------------- |
| `serviceid` | string | Cloud only | Cloud API key. Required for Cloud requests. Not used on-premises. |
| `format`    | string | No         | Response format. Supported: `json` (default), `xml`.              |
| `callback`  | string | No         | JSONP wrapper function name. Use only with `format=json`.         |

See [Overview](/v6.12.0/api-reference/overview.md#common-parameters) for details.

### Parameters

These are parameters specific to `cmd=detect_language`.

{% hint style="warning" %}
`detect_language` does **not** support `short_answer`.
{% endhint %}

| Name     | Type   | Required | Description                                                            |
| -------- | ------ | -------- | ---------------------------------------------------------------------- |
| `text`   | string | Yes      | Text to detect the language for.                                       |
| `locale` | string | No       | Localization for `LangName`. Accepted formats: `es`, `es-ES`, `es_ES`. |

### Behavior notes

* `locale` affects only `LangName`. It does not affect detection.
* If the language cannot be detected, you still get `200 OK`.
  * The response contains a single item with `LangShortCode=Unknown`.
* For better accuracy on long texts, split content into sentences.
  * Run `detect_language` per sentence.
* At least 3 words in text is needed to detected the language. See the information [here](/v6.12.0/faq/technical/languages/why-wproofreader-auto-detect-language-doesnt-work-for-small-piece-of-texts.md).

### Response schema

The response contains language candidates.

Use `format` to switch between JSON and XML.

{% tabs %}
{% tab title="JSON" %}

```json
[
  {
    "LangShortCode": "en_US",
    "LangName": "English (US)",
    "Proportion": 0.98
  }
]
```

{% endtab %}

{% tab title="XML" %}

```xml
<LanguageDetection>
  <Language>
    <LangShortCode>en_US</LangShortCode>
    <LangName>English (US)</LangName>
    <Proportion>0.98</Proportion>
  </Language>
</LanguageDetection>
```

{% endtab %}
{% endtabs %}

* `LangShortCode` (string)\
  Detected language code. See [Supported languages](/v6.12.0/features/supported-languages.md). Can also be `Unknown`.
* `LangName` (string)\
  Human-readable language name. Localized using `locale` when provided.
* `Proportion` (number)\
  Confidence / proportion of this language in the text. Higher means “more likely”.

### Request examples

#### Example: single language detected

Request:

{% tabs %}
{% tab title="GET" %}

```http
GET https://endpoint/api?cmd=detect_language&text=this%20sampl%20text%20demonstrates%20the%20work%20of%20the%20Web%20API%20service.
```

{% endtab %}

{% tab title="POST" %}

```http
POST https://endpoint/api
Content-Type: application/json

{
  "cmd": "detect_language",
  "text": "this sampl text demonstrates the work of the Web API service."
}
```

{% endtab %}
{% endtabs %}

Response (`200 OK`):

{% tabs %}
{% tab title="JSON" %}

```json
[
  {
    "LangShortCode": "en_US",
    "LangName": "English (US)",
    "Proportion": 0.98
  }
]
```

{% endtab %}

{% tab title="XML" %}

```xml
<LanguageDetection>
  <Language>
    <LangShortCode>en_US</LangShortCode>
    <LangName>English (US)</LangName>
    <Proportion>0.98</Proportion>
  </Language>
</LanguageDetection>
```

{% endtab %}
{% endtabs %}

#### Example: localized language name (`locale=es-ES`)

Request:

{% tabs %}
{% tab title="GET" %}

```http
GET https://endpoint/api?cmd=detect_language&locale=es-ES&text=this%20sampl%20text%20demonstrates%20the%20work%20of%20the%20Web%20API%20service.
```

{% endtab %}

{% tab title="POST" %}

```http
POST https://endpoint/api
Content-Type: application/json

{
  "cmd": "detect_language",
  "locale": "es-ES",
  "text": "this sampl text demonstrates the work of the Web API service."
}
```

{% endtab %}
{% endtabs %}

Response (`200 OK`):

{% tabs %}
{% tab title="JSON" %}

```json
[
  {
    "LangShortCode": "en_US",
    "LangName": "Inglés (Estados Unidos)",
    "Proportion": 0.98
  }
]
```

{% endtab %}

{% tab title="XML" %}

```xml
<LanguageDetection>
  <Language>
    <LangShortCode>en_US</LangShortCode>
    <LangName>Inglés (Estados Unidos)</LangName>
    <Proportion>0.98</Proportion>
  </Language>
</LanguageDetection>
```

{% endtab %}
{% endtabs %}

#### Example: no language detected

Request:

{% tabs %}
{% tab title="GET" %}

```http
GET https://endpoint/api?cmd=detect_language&text=asdfghjkl
```

{% endtab %}

{% tab title="POST" %}

```http
POST https://endpoint/api
Content-Type: application/json

{
  "cmd": "detect_language",
  "text": "asdfghjkl"
}
```

{% endtab %}
{% endtabs %}

Response (`200 OK`):

{% tabs %}
{% tab title="JSON (default)" %}

```json
[
  {
    "LangShortCode": "Unknown",
    "LangName": "Unknown",
    "Proportion": 0.0
  }
]
```

{% endtab %}

{% tab title="XML" %}

```xml
<LanguageDetection>
  <Language>
    <LangShortCode>Unknown</LangShortCode>
    <LangName>Unknown</LangName>
    <Proportion>0</Proportion>
  </Language>
</LanguageDetection>
```

{% endtab %}
{% endtabs %}

### Errors

This command has no command-specific `4xx` errors.

When language detection fails, the response is still `200 OK` with `LangShortCode=Unknown`.

Common API errors (for example an invalid `serviceid`) use the shared error format from [Overview](/v6.12.0/api-reference/overview.md#errors).
