@molecule/api-ocr-textract
Provider bond · ocr · API (Node) · v1.1.0 · Apache-2.0
AWS Textract OCR provider for molecule.dev — text extraction from images through Amazon Textract's DetectDocumentText
npm install @molecule/api-ocr-textractnpm · Source on GitHub · Implements @molecule/api-ocr
How it works
@molecule/api-ocr-textract is a provider bond on the API (Node) side: it implements the ocr core interface (@molecule/api-ocr) with a concrete vendor or library behind it.
Your code calls the core; you wire this provider once at startup. Swapping vendors later is one line in that wiring, not a rewrite.
import { setProvider, requireProvider } from '@molecule/api-ocr'
import { provider } from '@molecule/api-ocr-textract'
setProvider(provider) // at startup — lazy; reads AWS_* config on first use
const result = await requireProvider().recognize({
data: new Uint8Array(imageBytes),
mimeType: 'image/png',
})
console.log(result.text, result.pages[0].confidence)Works with: @molecule/api-bond, @molecule/api-ocr, @molecule/api-secrets
Secrets: AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_TEXTRACT_REGION (optional)
Reference
AWS Textract OCR provider for molecule.dev.
Implements the @molecule/api-ocr contract with Amazon Textract's
synchronous DetectDocumentText — layout-grade OCR with per-line confidence
scoring on AWS's own models, no vision-language model in the loop.
Auto-generated, AI-first package reference for the molecule.dev ecosystem. It is written to be read by coding agents as much as by people, and is generated from this package's source — edit
src/index.tsJSDoc, not this file.
Quick Start
import { setProvider, requireProvider } from '@molecule/api-ocr'
import { provider } from '@molecule/api-ocr-textract'
setProvider(provider) // at startup — lazy; reads AWS_* config on first use
const result = await requireProvider().recognize({
data: new Uint8Array(imageBytes),
mimeType: 'image/png',
})
console.log(result.text, result.pages[0].confidence)
Type
provider
Installation
npm install @molecule/api-ocr-textract @aws-sdk/client-textract @aws-sdk/credential-provider-node @molecule/api-analytics @molecule/api-bond @molecule/api-ocr @molecule/api-proxy-agent @molecule/api-secrets
API
Interfaces
AwsCredentials
An explicit AWS credential pair. Omit to resolve credentials through the
AWS default chain (AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, shared
config files, or an instance role) at send time.
interface AwsCredentials {
/** The AWS access key ID. */
accessKeyId: string
/** The AWS secret access key. */
secretAccessKey: string
}
TextractOcrConfig
Configuration for the AWS Textract OCR provider.
interface TextractOcrConfig {
/** AWS region to call Textract in. Defaults to AWS_TEXTRACT_REGION, then AWS_REGION, then 'us-east-1'. */
region?: string
/** Endpoint URL override (e.g. a local stub). Defaults to the AWS regional endpoint. */
endpoint?: string
/** Explicit credentials. Omit to use the AWS default chain. */
credentials?: AwsCredentials
}
Functions
resolveRegion(config)
Resolves the region at CLIENT-CONSTRUCTION time, not import time, so a
region resolved into process.env after this module loads (late secrets
resolution via a secrets bond) is honored — the SES bond's lesson.
function resolveRegion(config?: TextractOcrConfig): string
config— Explicit config, when the provider was created with one.
Returns: The region to call Textract in.
buildTextractClient(config)
Builds a Textract client from a config (or the environment when omitted).
The AWS SDK v3 builds its own https.Agent and reads no proxy variable, so
on a host whose only egress path is a proxy every call fails with a bare
connection error unless the agent is passed in via requestHandler — the
SES bond's fix, mirrored here.
function buildTextractClient(config?: TextractOcrConfig): TextractClient
config— Explicit config. Omitted keys fall back to the environment (AWS_TEXTRACT_REGION→AWS_REGION→us-east-1;AWS_TEXTRACT_ENDPOINT).
Returns: The configured client.
createProvider(config)
Creates a Textract OCR provider instance.
function createProvider(config?: TextractOcrConfig): OcrProvider
config— AWS-specific configuration (region, endpoint, credentials).
Returns: An OcrProvider backed by Amazon Textract.
Constants
provider
The provider implementation.
const provider: OcrProvider
ocrTextractSecretDefinitions
Secret definitions required by the AWS Textract OCR bond.
const ocrTextractSecretDefinitions: SecretDefinition[]
Core Interface
Implements @molecule/api-ocr interface.
Bond Wiring
Setup function to register this provider with the core interface:
import { setProvider } from '@molecule/api-ocr'
import { provider } from '@molecule/api-ocr-textract'
export function setupOcrTextract(): void {
setProvider(provider)
}
Injection Notes
Requirements
Peer dependencies:
@molecule/api-bond^1.0.1@molecule/api-ocr>=1.0.0@molecule/api-secrets^1.0.1
Environment Variables
AWS_ACCESS_KEY_ID(required) — AWS access key ID- Setup: Create an IAM user with the needed policy (SES/S3/SQS) and create an access key under Security credentials.
- Get it here: https://console.aws.amazon.com/iam/
- Example:
AKIA...
AWS_SECRET_ACCESS_KEY(required) — AWS secret access key- Setup: Shown once when creating the IAM access key — store it immediately.
AWS_TEXTRACT_REGION(optional) — AWS Textract region — default:us-east-1- Setup: The AWS region to call Textract in (Textract is not in every region).
- Example:
us-east-1
Runtime Dependencies
-
@aws-sdk/client-textract -
@aws-sdk/credential-provider-node -
@molecule/api-analytics -
@molecule/api-bond -
@molecule/api-ocr -
@molecule/api-proxy-agent -
@molecule/api-secrets -
Wiring: bond the lazy
providerexport once —setProvider(provider)— orsetProvider(createProvider(config?))to pass explicit config. -
Config:
AWS_ACCESS_KEY_ID+AWS_SECRET_ACCESS_KEY(required — an IAM key withtextract:DetectDocumentText);AWS_TEXTRACT_REGION(optional — falls back toAWS_REGION, thenus-east-1; Textract is not in every region);AWS_TEXTRACT_ENDPOINT(optional endpoint override). -
PNG or JPEG only, up to 5 MB — the synchronous
Bytespath's own limits (AWS's Document type: "The document bytes must be in PNG or JPEG format", max 5 MB). TIFF/BMP/PDF need Textract's asynchronous APIs, which this bond does not cover; a wrong format or oversized image is refused locally, before any upstream call. -
options.languageis ignored. DetectDocumentText takes no language hint: it finds printed text in any language (handwriting support is limited). Pass a language to bond-specific providers that accept one. -
confidenceis real here — the mean of Textract's per-LINE scores (0–100 normalized to 0–1). Unlike a vision model, Textract scores itself; treat low scores as a scan-quality signal, not proof of correctness. -
The text is assembled from LINE blocks in Textract's reading order, joined with newlines — paragraph structure and column layout are NOT preserved. For tables/forms use Textract's AnalyzeDocument APIs directly (not in this bond).
-
Recognized text is untrusted input — see the
@molecule/api-ocrcore's remarks: escape before rendering, moderate before publishing. -
On hosts whose egress path is an HTTP proxy the SDK's agent is set from
@molecule/api-proxy-agentautomatically; the AWS SDK otherwise ignores proxy variables entirely.