Introducing a new way to review your PRs.

Read the announcement

zipstack / unstract

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

View on GitHub

28 documents · Updated Nov 23, 2026

docs / x2text-service

X2Text document conversion service

The X2Text service is a standalone HTTP bridge between Unstract and the Unstructured API. It accepts a document and connection details, asks the upstream service to extract text, and returns the extracted content as a downloadable text file. It also protects conversion operations with a platform key and records their outcome for the owning organization. See the document-processing page for the wider document flow.

Service boundary

Conversion boundary

X2Text keeps the document conversion boundary small. The caller supplies the upstream URL, an optional Unstructured API key, and a file. X2Text forwards the request and changes the upstream text result into the response format expected by the platform.

The service does not choose a document parser. The configured Unstructured endpoint remains responsible for interpreting the file and producing text. This lets the platform use either a hosted service or a compatible community deployment without changing the X2Text API.

References: x2text-service/README.md• L1-4 x2text-service/…/controller.py• L76-126

API surface

The API is grouped below the configurable /api/v1 prefix and the /x2text resource path. With the default prefix, clients use /api/v1/x2text/....

Health endpoint

GET /api/v1/x2text/health provides a light availability check. It returns OK without checking the upstream converter or the database, so it shows that the Flask process can answer requests rather than that document conversion is ready.

References: x2text-service/…/controller.py• L27-31 x2text-service/…/config.py• L12-16

Connection test

POST /api/v1/x2text/test-connection checks the configured upstream URL and credential path without processing a user document. The caller sends multipart form data with unstructured-url and unstructured-api-key, plus a platform key in the Authorization header.

The endpoint treats the upstream 400 response used for a request without a real file as a successful connection test. Other upstream responses pass back their status and content. A missing URL or a connection error returns a client-visible failure.

References: x2text-service/README.md• L5-31 x2text-service/…/controller.py• L33-73

Document processing

POST /api/v1/x2text/process converts one uploaded file. The request includes the same upstream settings and a multipart file field. A valid conversion returns an attachment named infile.txt; an upstream error returns the upstream status and a normalized error body.

The request moves through the service as shown here.

flowchart LR
    Client[Platform client] --> Auth[Platform key check]
    Auth -->|valid| Input[Read URL, API key, and file]
    Input --> Upstream[Unstructured API]
    Upstream -->|text items| Combine[Combine text in order]
    Combine --> Download[Text file response]
    Input --> Audit[Create audit record]
    Download --> AuditResult[Mark conversion success]
    Upstream -->|failure| Failure[Mark conversion failure]

References: x2text-service/README.md• L34-45 x2text-service/…/controller.py• L76-138

Authentication boundary

Platform key authentication

The test and process endpoints require a platform key in the Authorization header. The middleware extracts the token and checks that it exists and is active in the configured platform-key store. A missing, unknown, or inactive key stops the request with 401 Unauthorized.

The platform key is separate from unstructured-api-key. The first key grants access to X2Text. The second key is forwarded to the upstream Unstructured service and may be empty for a community deployment.

References: x2text-service/…/authentication_middleware.py• L10-21 x2text-service/…/authentication_middleware.py• L23-61 x2text-service/…/controller.py• L33-35

Organization context

After authentication, document processing maps the platform key to an organization. X2Text uses that organization context when it creates the conversion audit record, which links file activity to the correct tenant.

References: x2text-service/…/authentication_middleware.py• L75-107 x2text-service/…/controller.py• L92-102

Runtime configuration

Service settings

The service loads settings from the process environment before it creates the application. Database connection settings are required. The API prefix is optional and defaults to /api/v1.

Setting groupPurpose
DB_SCHEMASelects the database schema for platform-key, organization, and audit data.
DB_HOST, DB_PORTLocate the PostgreSQL service.
DB_USERNAME, DB_PASSWORD, DB_NAMEOpen the PostgreSQL database.
API_URL_PREFIXChanges the root path for the HTTP API.

Missing required settings stop startup with an error that names the missing values. This makes an incomplete deployment fail early rather than run without its database contract.

References: x2text-service/…/env.py• L8-44 x2text-service/…/env.py• L47-56 x2text-service/…/config.py• L9-20

Startup state

Application creation registers the API and prepares the audit model against PostgreSQL. The service therefore expects the configured database to be reachable when it starts, not only when the first document arrives.

References: x2text-service/run.py• L1-6 x2text-service/…/config.py• L12-21 x2text-service/…/models.py• L8-14

Request and conversion model

Multipart request

The process contract combines form fields with one uploaded file. X2Text removes the service-control fields before it forwards the remaining form data to the upstream endpoint. This keeps upstream options available while reserving the URL and credential fields for routing and authentication.

The main input rules are:

  • unstructured-url is required.
  • file is required and must have a selected filename.
  • unstructured-api-key is forwarded as the upstream credential and can be empty for community use.

References: x2text-service/…/controller.py• L76-117 x2text-service/…/util.py• L6-10

Text response

The upstream response is treated as an ordered collection of text-bearing items. The conversion utility joins their text values with newline separators, so the returned file preserves the upstream item order while presenting one plain-text document.

For an upstream failure, the utility keeps a JSON response as structured data when the content type is exactly JSON. Other responses become a simple message containing the upstream response text.

References: x2text-service/…/util.py• L13-25 x2text-service/…/controller.py• L127-138

Conversion audit

Audit record

Each processing request creates an x2text_audit record after basic input checks and before the upstream call. The record carries the organization, file name, media type, file size in kilobytes, creation time, and conversion status.

The service marks the record Success only when the upstream response is usable and the text file is returned. A non-success upstream response marks it Failed. This record gives platform operators a small history of document conversion activity without storing the document contents.

References: x2text-service/…/models.py• L17-29 x2text-service/…/controller.py• L92-102 x2text-service/…/controller.py• L127-138

Change checklist

When these files or sections are changed, remember to consider these:

  • Changes to the API prefix or /x2text route group must stay aligned with clients that call the health, connection-test, and process endpoints.
  • Changes to platform-key lookup must preserve the link between authentication and organization ownership in x2text_audit.
  • Changes to the upstream response shape must update the text conversion contract and the downloadable response behavior together.
  • Changes to required database settings or schema names must stay aligned with the platform database and the audit-table migration path.
  • Changes to multipart field names must be reflected in both the client contract and the upstream request mapping.

Important failure modes

TriggerConsequenceGuard
A required database setting is missingThe application stops during startup.Provide the full database setting set before starting the service.
The platform key is absent, unknown, or inactiveThe protected endpoint returns 401 Unauthorized.Use an active platform key in the Authorization header.
The URL or file is missing from a process requestThe service returns 400 and does not call the converter.Send the required multipart fields.
The upstream service returns a non-success responseThe audit record becomes Failed and the upstream status is returned.Check the upstream URL, credential, and service health.
The upstream success body does not contain the expected text itemsText assembly can fail before a file is returned.Keep the upstream response contract as an ordered collection of text values.
A connection test receives the upstream no-file 400 responseThe service reports the connection test as successful by design.Use document processing to verify a real file conversion.

Key files

FileWhy to read it
x2text-service/README.mdDescribes the public endpoints, request examples, and audit-table migration context.
x2text-service/…/controller.pyShows the end-to-end HTTP behavior for health checks, connection tests, and document conversion.
x2text-service/…/authentication_middleware.pyDefines platform-key validation and organization lookup.
x2text-service/…/config.pyShows application setup, route registration, and audit-table preparation.
x2text-service/…/env.pyDefines the required runtime settings and startup validation.
x2text-service/…/models.pyDefines the audit data captured for each conversion.
x2text-service/…/util.pyDefines the shared form-field and upstream-response handling used by conversion requests.
x2text-service/run.pyProvides the application entry point used to start the service.