Skip to content

PHP SDK

Updated

Install#

composer require docxtract/php-sdk

PHP 8.1+ with curl and json. No other dependencies — the SDK uses plain cURL rather than Guzzle, so it installs cleanly on shared hosting and cannot conflict with an HTTP client already in your project.

Extract a document#

use DocXtract\DocXtract;

$dx = new DocXtract('sk_your_api_key');
$result = $dx->extract('invoice.pdf', ['model' => 'invoice']);

echo $result['vendor'];
echo $result->get('line_items.0.hsn');
echo $result->pages();
Note
DocXtract keys use an underscore (sk_). A hyphen means the key is from a different API provider; the SDK rejects it immediately rather than letting you debug a 401.

Large PDFs#

This is the main reason to use the SDK rather than raw HTTP. A PDF over 3 pages returns 202 and requires the multi-page flow — split, process each chunk, collect, with retries and a 2-hour TTL to respect.

extract() handles all of it:

$result = $dx->extract('500-page-statement.pdf', ['model' => 'bank_statement']);

Same call, any page count. Add a progress callback for long jobs:

$dx->extract('big.pdf', ['model' => 'invoice'], fn($done, $total) => print("$done/$total\n"));

Chunks are processed sequentially by design. The default rate limit is 10 requests per minute, so parallel chunk calls do not finish sooner — they just produce 429s.

Error handling#

Each API error code maps to a typed exception:

use DocXtract\Exception\{RateLimitException, QuotaException, DocXtractException};

try {
    $dx->extract('invoice.pdf', ['model' => 'invoice']);
} catch (RateLimitException $e) {
    sleep($e->getRetryAfter() ?? 30);
} catch (QuotaException $e) {
    // out of credits — do not retry
} catch (DocXtractException $e) {
    if ($e->isRetryable()) { /* requeue */ }
}

isRetryable() is deliberately conservative: input errors, expired jobs, and exhausted credits all return false. See Error codes for the full mapping.

Discovering document types#

$dx->models();   // costs no credits — safe to call freely

Returns only the types your key may use.

Configuration#

OptionDefaultNotes
api_keyRequired
base_urlhttps://api.docxtract.ioNo /api prefix
base_path/v3.1Set /v3 only if pinned to the old version
timeout120Seconds per request
max_retries3Retryable errors only
chunk_pause_ms0Pace chunk calls on tight rate limits

Pre-flight validation runs locally: an unsupported file type or an oversized file fails before any request, costing no round trip and no credits.

Full reference#

sdk/php/README.md in the platform repository.