Getting Started
The @ocriva/sdk package gives you a fully-typed TypeScript client for the Ocriva API. This guide walks you through installation, initial setup, and your first upload.
Requirements
- Node.js >= 18 — the SDK uses the native
fetchAPI built into Node 18+ - TypeScript >= 5.0 (recommended) — required to take full advantage of the type definitions
Installation
Install the SDK with your preferred package manager:
# npm
npm install @ocriva/sdk
# pnpm
pnpm add @ocriva/sdk
# yarn
yarn add @ocriva/sdkGetting Your API Key
API keys are created from the Ocriva Dashboard:
- Sign in at ocriva.com
- Navigate to Settings → API Tokens
- Click Create Token and choose a scope (organization or project)
- Copy the token — it will not be shown again
See Authentication for details on token types and scopes.
Quick Start
The following example shows the most common workflow: create a client, upload a file, poll until processing completes, then download the result.
import { OcrivaClient } from '@ocriva/sdk';
import { readFileSync } from 'node:fs';
const client = new OcrivaClient({
apiKey: process.env.OCRIVA_API_KEY!,
});
// 1. Read a local file into a Buffer
const buffer = readFileSync('./invoice.pdf');
// 2. Upload the file for processing
const upload = await client.upload.file(buffer, {
fileName: 'invoice.pdf',
fileType: 'pdf',
fileSize: String(buffer.byteLength),
mimeType: 'application/pdf',
projectId: 'proj_abc123',
templateId: 'tmpl_xyz789', // optional — apply an extraction template
});
console.log('Upload ID:', upload.id);
console.log('Status:', upload.status); // 'uploaded' | 'processing' | ...
// 3. Poll processing history until the job finishes
let record = await client.processingHistory.get(upload.id, 'proj_abc123');
while (record.status === 'pending' || record.status === 'queued' || record.status === 'in_progress') {
await new Promise((resolve) => setTimeout(resolve, 2000));
record = await client.processingHistory.get(upload.id, 'proj_abc123');
}
if (record.status === 'completed') {
// 4. Download the extracted result as JSON
const blob = await client.processingHistory.getResult(record.id, {
format: 'json',
projectId: 'proj_abc123',
});
const text = await blob.text();
console.log('Extracted data:', JSON.parse(text));
} else {
console.error('Processing failed:', record.errorMessage);
}Configuration
Pass a configuration object to OcrivaClient. Only apiKey is required; all other options have sensible defaults.
import { OcrivaClient } from '@ocriva/sdk';
const client = new OcrivaClient({
apiKey: 'ocr_live_...', // required
baseUrl: 'https://api.ocriva.com/v1', // default
timeout: 30_000, // 30 s, in milliseconds
maxRetries: 3, // retries on 429 and 5xx responses
});Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | — | Required. Your Ocriva API key |
baseUrl | string | https://api.ocriva.com/v1 | Base URL of the API |
timeout | number | 30000 | Request timeout in milliseconds |
maxRetries | number | 3 | Maximum retry attempts on transient failures (429, 5xx) |
The apiKey must be a non-empty string or the constructor will throw an OcrivaError immediately. The baseUrl, if provided, must be a valid URL with a scheme (e.g., https://).
