Client Reference
The Structurify client provides access to all API resources.
Initialization
import { Structurify } from '@structurify/sdk';
const client = new Structurify({
apiKey: 'sk_live_your_api_key',
baseUrl: 'https://app.structurify.ai/api', // optional
timeout: 30000, // optional, milliseconds
});
Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your Structurify API key |
baseUrl | string | https://app.structurify.ai/api | API base URL |
timeout | number | 30000 | Request timeout in milliseconds |
Resources
Templates
// List all project templates
const templates = await client.templates.list();
// Returns array with id, name, description, columns
Projects
// List all projects
const projects = await client.projects.list();
// Create a project
const project = await client.projects.create({
name: 'My Project',
templateId: 'tpl_invoice',
});
// Get a project
const project = await client.projects.get('proj_xxx');
// Delete a project
await client.projects.delete('proj_xxx');
Documents
import * as fs from 'fs';
// Upload a document
const doc = await client.documents.upload({
projectId: 'proj_xxx',
file: new Blob([fs.readFileSync('invoice.pdf')]),
name: 'invoice.pdf',
});
// Get document
const doc = await client.documents.get('doc_xxx');
// Get document content
const content = await client.documents.getContent('doc_xxx');
// Delete document
await client.documents.delete('doc_xxx');
Extraction
// Run extraction
const job = await client.extraction.run({
projectId: 'proj_xxx',
});
// Get job status
const job = await client.extraction.get('job_xxx');
// Wait for completion (blocking)
const completed = await client.extraction.waitForCompletion('job_xxx', {
pollInterval: 2000, // milliseconds
timeout: 300000, // milliseconds
});
// Cancel job
await client.extraction.cancel('job_xxx');
Exports
// Create export
const exportResult = await client.exports.create({
projectId: 'proj_xxx',
format: 'csv', // or 'json'
documentIds: ['doc_1', 'doc_2'], // optional, default all
});
// Download export
const data = await client.exports.download('exp_xxx');
Error Handling
import {
StructurifyError,
AuthenticationError,
NotFoundError,
InsufficientCreditsError,
RateLimitError,
ValidationError,
} from '@structurify/sdk';
try {
const project = await client.projects.get('proj_invalid');
} catch (error) {
if (error instanceof AuthenticationError) {
console.error('Invalid API key');
} else if (error instanceof NotFoundError) {
console.error('Project not found');
} else if (error instanceof InsufficientCreditsError) {
console.error('Not enough credits');
} else if (error instanceof RateLimitError) {
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
} else if (error instanceof ValidationError) {
console.error(`Validation error: ${error.message}`);
} else if (error instanceof StructurifyError) {
console.error(`API error: ${error.message}`);
}
}
Webhook Verification
import { verifyWebhookSignature } from '@structurify/sdk';
// Express.js example
app.post('/webhook', (req, res) => {
const isValid = verifyWebhookSignature({
payload: req.body, // raw body string
signature: req.headers['x-structurify-signature'],
secret: 'your_webhook_secret',
});
if (!isValid) {
return res.status(401).json({ error: 'Invalid signature' });
}
const event = JSON.parse(req.body);
console.log(`Event: ${event.event}`);
res.json({ status: 'ok' });
});