# Mask data in text

Source: [Mask data in text](<https://docs.gptunnel.ru/en/api/obfuscate/>)

<a id="_top"></a>
`POST https://gptunnel.ru/v1/obfuscate`

Send the document in `text`. The endpoint replaces detected fragments and returns the masked text in `obfuscated_text` without calling a language model. For automatic restoration of masked values in model responses, use [PII masking in Chat Completions](<https://docs.gptunnel.ru/en/api/pii-obfuscation/>).

<a id="request-parameters"></a>
## Request parameters

Send your [API key](<https://docs.gptunnel.ru/en/#get-an-api-key>) in the `Authorization` header and use a JSON request body.

| Parameter | Type | Required? | Description |
| --- | --- | --- | --- |
| `text`<a id="request-text"></a> | `string` | Required | Nonempty text to process. The default limit is 500,000 characters. |
| `custom_patterns`<a id="request-custom_patterns"></a> | `array` | Optional | Additional rules for your own identifiers. Each rule has a regular expression in pattern and may include a label. |
| `entity_types`<a id="request-entity_types"></a> | `array` | Optional | Built-in classes: PER, ORG, LOC, MSC and IDS. Omit this field to detect all classes. To detect selected classes, pass a nonempty array containing the desired classes. |

Pass up to 20 rules in `custom_patterns`. Each regular expression can be up to 500 characters and each label up to 16 characters. See [rule syntax and capture groups](<https://docs.gptunnel.ru/en/api/pii-obfuscation/#custom-patterns-obfuscate_patterns>).

<a id="example-request"></a>
## Example request

Set the `GPTUNNEL_API_KEY` environment variable. The sample text contains the same test identifier twice. The rule assigns it the label `DOCREF`.

<a id="tab-panel-0-0"></a>
**Python**

```python
import json
import os
from urllib.request import Request, urlopen

body = {
    'text': 'DOCREF-27183 / DOCREF-27183',
    'custom_patterns': [{'pattern': 'DOCREF-[0-9]{5}', 'label': 'DOCREF'}],
}
request = Request(
    'https://gptunnel.ru/v1/obfuscate',
    method='POST',
    headers={
        'Authorization': os.environ['GPTUNNEL_API_KEY'],
        'Content-Type': 'application/json',
    },
    data=json.dumps(body).encode('utf-8'),
)
with urlopen(request, timeout=30) as response:
    print(json.load(response))
```

<a id="tab-panel-0-1"></a>
**cURL**

```bash
curl --fail-with-body --silent --show-error \
  --url 'https://gptunnel.ru/v1/obfuscate' \
  --header "Authorization: ${GPTUNNEL_API_KEY:?Set GPTUNNEL_API_KEY}" \
  --json '{
  "text": "DOCREF-27183 / DOCREF-27183",
  "custom_patterns": [
    {
      "pattern": "DOCREF-[0-9]{5}",
      "label": "DOCREF"
    }
  ]
}'
```

<a id="tab-panel-0-2"></a>
**JavaScript (Node.js)**

```javascript
const response = await fetch('https://gptunnel.ru/v1/obfuscate', {
  method: 'POST',
  headers: {
    Authorization: process.env.GPTUNNEL_API_KEY,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
  "text": "DOCREF-27183 / DOCREF-27183",
  "custom_patterns": [
    {
      "pattern": "DOCREF-[0-9]{5}",
      "label": "DOCREF"
    }
  ]
}),
})
if (!response.ok) throw new Error(await response.text())
console.log(await response.json())
```

<a id="response"></a>
## Response

The response below corresponds to the example request. Its `id` is illustrative.

```json
{
  "id": "6720f0c1a9e4b3d2c1f0e9a8",
  "obfuscated_text": "<<DOCREF_0001>> / <<DOCREF_0001>>",
  "mapping": [
    {
      "id": "<<DOCREF_0001>>",
      "text": "DOCREF-27183"
    }
  ],
  "usage": {
    "input_tokens": 11,
    "cost_rub": 0.00308
  }
}
```

Both copies of the identifier use the same placeholder in `obfuscated_text`. To restore the original value, use the corresponding `id` and `text` from `mapping`. If an external system needs masked text only, send `obfuscated_text` and keep `mapping` private because it contains the original values.

 <a id="value"></a>
 
<a id="pricing"></a>
## Pricing

You are charged for all text scanned, even when there are no matches. `usage.input_tokens` is the number of tokens processed. `usage.cost_rub` is the price in rubles at the listed rate. In the sample response, 11 tokens cost ₽0.00308. Check your transaction history for the amount actually charged after discounts. Include the top-level `id` when contacting support about the request.

<a id="errors"></a>
## Errors

Check the HTTP status and `error.code`. For example, an empty `text` returns HTTP 400 with `invalid_value`.

| HTTP | `error.code` | What to Do |
| --- | --- | --- |
| 400 | `invalid_value` | Check the request body. Pass a nonempty string in `text`. To detect all classes, omit `entity_types`. |
| 400 | `invalid_obfuscate_patterns` | Fix the rule in `custom_patterns`. See `error.message` for the reason. |
| 402 | `insufficient_balance_error` | Check the balance of the account paying for the request. |
| 402 | `api_key_limit_exceeded` | Check the API key’s usage and spending limit. |
| 403 | `model_blocked` | Contact support about the API access restriction. |
| 413 | `obfuscation_input_too_large` | Split the document into smaller parts. The text may exceed the size limit or take too long to scan. |
| 503 | `obfuscation_unavailable` | Retry later. There is no processing charge while the service is unavailable. |
