Skip to content

Mask data in text

POSThttps://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.

Send your API key in the Authorization header and use a JSON request body.

ParameterTypeRequired?Description
text#stringRequiredNonempty text to process. The default limit is 500,000 characters.
custom_patterns#arrayOptionalAdditional rules for your own identifiers. Each rule has a regular expression in pattern and may include a label.
entity_types#arrayOptionalBuilt-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.

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

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))

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

{
"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.

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.

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.